Creating instances of untrusted new-style classes

2006-05-25 Thread Devan L
Is there any safe way to create an instance of an untrusted class
without consulting the class in any way? With old-style classes, I can
recreate an instance from another one without worrying about malicious
code (ignoring, for now, malicious code involving attribute access) as
shown below.

 import types
 class Foo:
... def __init__(self, who, knows, what, args):
... self.mystery_args = (who, knows, what, args)
... print Your code didn't expect the Spanish inquisition!
...
 f = Foo('spam','eggs','ham','bacon') # This would be in a restricted 
 environment, though.
Your code didn't expect the Spanish inquisition!
 types.InstanceType(Foo, f.__dict__) # This wouldn't, but we never run that 
 code, anyways.
__main__.Foo instance at 0x008B5FD0


I'm not sure how to do the same for new-style classes, if it's at all
possible to do from within Python. Is there any way to accomplish this,
or is there no practical way to do so?

Thanks,
- Devan

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


Re: Creating instances of untrusted new-style classes

2006-05-25 Thread Devan L
Ben Finney wrote:
 Devan L [EMAIL PROTECTED] writes:

  Is there any safe way to create an instance of an untrusted class

 Why are you instantiating classes you don't trust?

  without consulting the class in any way?
 If you don't consult the class, how can the instance be created
 properly?


When my program runs (CGI), the following happens:
* User enters source, which is executed in a restricted environment,
which unserializes a previously serialized environment if there is one.

* The restricted environment is serialized, including any instances
they may have instantiated.

So when I unserialize their instances, I have to recreate them, but
without calling any of their code (I can't run the unserializing code
in a restricted environment). Instances of old-style classes can be
created without touching the actual old-style class code, but I'm not
sure how, if it's possible, to do the same with new-style classes


- Devan

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


Re: Creating instances of untrusted new-style classes

2006-05-25 Thread Devan L

Michael Spencer wrote:
 Devan L wrote:
  Is there any safe way to create an instance of an untrusted class
  without consulting the class in any way? With old-style classes, I can
  recreate an instance from another one without worrying about malicious
  code (ignoring, for now, malicious code involving attribute access) as
  shown below.
 
[snip my example]
 
  I'm not sure how to do the same for new-style classes, if it's at all
  possible to do from within Python. Is there any way to accomplish this,
  or is there no practical way to do so?
 
  Thanks,
  - Devan
 
   class A(object):
 ... def __init__(self, *args):
 ... self.args = args
 ... print Calling __init__
 ...
   a = A(new,style)
 Calling __init__
   b = object.__new__(A)
   b.__dict__ = a.__dict__.copy()
   b.args
 ('new', 'style')
   type(a) is type(b)
 True
  

 HTH

 Michael

Thanks, now I just have to figure out all the meddling small details I
put off before!

-Devan

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


Re: Safe Python Execution

2006-02-15 Thread Devan L

Graham wrote:
 I've been messing around with trying to get a small sandbox like
 environment where i could execute python code in a safe way.
 Basically what the old restricted execution module attempted to do.
 I've written a small amount of code to get custom interpreter running,
 but i'm not really sure if its safe.

 The way i'm controlling functionality is with some games and exec, so
 if 'code' was the text code you wanted to execute i run:

 exec code in {'__builtins__':None}

 obviously this doesn't give you much to play with, but it does remove
 file access and importing as far as i can tell. Can anyone think of a
 hack around this? I assume if it was this easy it would be a module
 already but i figured i would ask.

You need to remove reload, replace __import__, disable __subclasses__
(not convenient nor portable because you need to do it in the source.
Shouldn't it be restricted in restricted mode?). That removes most
glaring security holes, I think. If you need to touch any of the
attributes of the objects in the sandbox, you might want to remove
properties. I wouldn't recommend exposing any objects outside of the
sandbox to the sandbox, either.

Zope also has some cool viral proxy thing that I don't understand that
you might want to look into.

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


(Different) Try Python Update

2006-01-14 Thread Devan L
It's been a while since I've done anything, and I have finals, so if
anyone wants to look at some of the source, here's the somewhat cleaned
up source for bastille and modjelly. Bastille is just a
sort-of-more-secure equivalent of what the code module is, in case you
have no clue what it does since I haven't really mentioned it.

Bastille: http://www.datamech.com/devan/bastille.py.txt
Modjelly: http://www.datamech.com/devan/modjelly.py.txt

On a side note, I just realized that due to some bug, any function
definition after the first function is defined simply binds the name to
the first one, which is a very bad thing! So if you can find anything
related to this in the source, send me an email.

Oh, right, and in case you have no clue what I'm talking about, I'm
trying to replicate an interactive session online. My Try Python is at
http://www.datamech.com/devan/trypython/trypython.py and Mike Meyer's
cooler one is at http://www.mired.org/home/mwm/try_python/.

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


Re: Try Python update

2006-01-07 Thread Devan L
Mike Meyer wrote:
 Xavier Morel [EMAIL PROTECTED] writes:
[Old message and Xavier's question]
[Mike's reply to Xavier]

  Since Python doesn't have any way to secure the interface built-in,
  i'd be interrested in that.

 Devan apparently doesn't have as cooperative an ISP, and is working on
 securing the interpreter. What he's done may be more interesting.

It's not particularily interesting. The C code simply has more
attributes restricted in restricted mode, disabling in particular
__subclasses__. The rest is creating safe __builtins__ and only giving
them some modules (but again, importing is still largely broken,
although one liners are still possible).

In any case, I don't know how secure it actually is, since nobody seems
to go further than import os or import sys. So if you're bored, you can
try to break into it. I haven't secured modjelly entirely, and it might
be possible to trick modjelly into executing code by using descriptors
when it tries to pull out all of the information. Then again, I haven't
even added support for properties in it yet. Plus it has no support for
if you delete critical attributes which are needed to recreate the
object. Still, I think it's good enough to deter any random person from
trying to wipe the server for now.

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


Re: Try Python update

2006-01-03 Thread Devan L

[EMAIL PROTECTED] wrote:
 I like the form, no matter what its limitations may be. Three notes:

 It might be a good way to catch newbi mistakes (those are the kind I
 make :P, thereby providing a feedback loop to improved error messages.

 I had no trouble with from math import *  followed by print pi, but
 there was no  prompt after the result appeared .. is that part of
 the 'closures' thing mentioned earlier?

 Then I followed you type 'help' suggestion, which had me in stitches
 (sorry, it's been a long day and the Knob Creek was kicking in) -- I
 got exactly the message you'd expect, about help not being recognized
 and maybe help() would work. That's what triggered the idea for
 trapping errors.

 Nice work!

 George

Uh, I'm messing with import right now, but essentially, I wouldn't
recommend importing (one line __import__ hacks only!) or printing.

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


Re: Try Python update

2006-01-03 Thread Devan L
Mike Meyer wrote:
 [EMAIL PROTECTED] writes:
[comments about Mike Meyer's try python, I think]
  I had no trouble with from math import *  followed by print pi, but
  there was no  prompt after the result appeared .. is that part of
  the 'closures' thing mentioned earlier?

 Hmm. Are you looking at mine URL:
 http://www.mired.org/home/mwm/try_python/ , or Devans URL:
 http://www.datamech.com/devan/trypython/trypython.py ? Mine doesn't
 handle code (though he's provide pointers I plan on using to make it
 do so). I think he said his has problems with closures.

I'm renaming mine to the Python Online REPL or Online Python REPL,
whichever you prefer, to help differentiate from Mike's.

From the actual recipe:
http://www.onlamp.com/python/pythoncook2/solution.csp?day=2
(The url changes every day as they move it one day back)
if co.co_freevars or co.co_cellvars:
raise ValueError, Sorry, cannot pickle code objects from
closures

I think that the code constructor (types.CodeType) doesn't take
co_freevars or co_cellvars as an arg, so I can't directly create a new
code object from the attribute of the old one with co_freevars and
co_cellvars. I might be able to set the attributes after,but I'll have
to test it out to see.

  Then I followed you type 'help' suggestion, which had me in stitches
  (sorry, it's been a long day and the Knob Creek was kicking in) -- I
  got exactly the message you'd expect, about help not being recognized
  and maybe help() would work. That's what triggered the idea for
  trapping errors.

 Hmm. help seems to work fine in both of them. license() fails the same
 way in both of them (it tries to read from stdin, so), which means
 that you can probalby make help fail on them both if you work at it.


Printing and most things not explicitly called by my code isn't done
correctly (strings to lines to strings again!), so most any printing
non-single statements or input will probably break it.

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


Re: Try Python update

2006-01-01 Thread Devan L

Mike Meyer wrote:
 After spending time I should have been sleeping working on it, the try
 python site is much more functional. It now allows statements,
 including multi-line statements and expressions. You can't create code
 objects yet, so it's still more a programmable calculator than
 anything real.

 I've got some of the tutorial text (literally) up as well. I hope to
 make it easier to read the tutorial and interact with python at the
 same time in the near future.

 The url is http://www.mired.org/home/mwm/try_python/. Reports of
 problems would appreciated.

 If you want to try an online P{ython tool that lets you save code, try
 Devan L's at http://www.datamech.com/devan/trypython/trypython.py.

My code uses one of the recipes from the Python Cookbook, 7.6 Pickling
Code Objects. It's limited to closures though, just like in the recipe.
So uh, you can't write closures in mine.

On a side note, my brother has tinkered with the C internals and now
__subclasses__ is restricted and many, many os and posix commands are
restricted (not that you can get them anyways, since importing is
broken!)

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


Re: Try Python update

2006-01-01 Thread Devan L
Mike Meyer wrote:
 Devan L [EMAIL PROTECTED] writes:
  If you want to try an online P{ython tool that lets you save code, try
  Devan L's at http://www.datamech.com/devan/trypython/trypython.py.
  My code uses one of the recipes from the Python Cookbook, 7.6 Pickling
  Code Objects. It's limited to closures though, just like in the recipe.
  So uh, you can't write closures in mine.

 I don't have the dead trees version, and the online version doesn't
 have chapter numbers. Is that URL:
 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/212565?

It was one of the recipes of the day, actually. They don't keep them up
for more than a week, though, I think. But more or less it has all of
the attributes used for creating an object from types.CodeType in
correct order.

  On a side note, my brother has tinkered with the C internals and now
  __subclasses__ is restricted and many, many os and posix commands are
  restricted (not that you can get them anyways, since importing is
  broken!)

 I got import to work by pickling pairs of names - the variable name
 that references the module, and the name of the module. When it
 unpickles the list, it reimports them and points the appropriate
 variable at them. This had unwanted effects if you did an import
 this. It also doesn't work for objects that contain references to
 modules.

My general method of storing is to store everything possible. See a
function? Store the function object itself. See a class? Store the
class object. Unfortunately, I can't store builtin methods or
functions, so this breaks on most modules. Don't try to reference the
__builtins__, by the way, otherwise it won't be removed and modjelly
will break.


 I tried for a bit to restrict things, then gave up and did it
 externally. There's no access to any files but those required to run
 the script that deals with thing, and some other python modules that I
 decided would be nice to have. Normally, nothing in the tree is
 writable, either. Once I'm happy with it, I'll save a copy of the tree
 somewhere and set up a cron job to refresh it at regular intervals.

I don't have enough control on the server to effectively restrict it
externally (no running as an unprivelleged user!), so I have to lock it
down as tightly as possible internally.

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


ANN: (Different) Try Python Beta

2005-12-28 Thread Devan L
I've spent a while putting together a partially working Try Python
which handles class and function definitions. It also (used to) work
with imports, but my hacked version of jelly doesn't work with it
anymore, so only import this works as far as I know. It won't play nice
if you store the id of an object because the objects are recreated
every time, but references still work. It also won't let you maintain
generators or iterators, but you can still put them inside functions to
use. Printing also adds unnecessary newlines which I haven't fixed yet.
And no, it doesn't use AJA(X|T), only some javascript to focus the
input box.

So here's the link for it:
 http://www.datamech.com/devan/trypython/trypython.py 

And if you want to see Mike Meyers' nicer, original one:
 http://www.mired.org/home/mwm/try_python/ 

If you want to see the source, send me an email, although you may have
to gouge your eyes out after reading it.

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


Re: ANN: (Different) Try Python Beta

2005-12-28 Thread Devan L

Steve Holden wrote:
 Devan L wrote:
[what I said]
 At first I thought 'the cgitb TypeError message from import os is
 impressively drastic :-)'. Then I realised in a later session that
 import os only gave an error message after I'd run import this.
 Thereafter, unfortunately, almost any input - legal or not - appears to
 result in a cgitb trace.

Try reloading the page, otherwise it will use the old session id. It
likes to break one step input after the offending action. It will also
screw up the state of the session. Incidentally, import this doesn't
seem to work, either. But importing is broken in general anyways, so I
wouldn't recommend using it.

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


Re: ANNOUNCE; Try python beta

2005-12-19 Thread Devan L
Mike Meyer wrote:
 Ok, I've given it the interface I want, and made it less of an
 attractive nuisance.

 http://www.mired.org/home/mwm/try_python/ is now ready for people to
 play with. There's no tutorial information on it yet, that's the next
 thing to do. However, I won't be able to work on it for a while, so if
 you want to make suggestions about what that should look like, all
 such suggestions will be given proper consideration.

I was bored so I wrote some code that takes input one line at a time
and spits out the a tuple representing the state and a message to be
echoed. It handles statements on multiple lines, too. So it might be
helpful, since your tutorial doesn't  seem to handle multiple lines or
statements.

You can see it at http://www.datamech.com/devan/bastille.txt

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


Re: CGI module does not parse data

2005-12-16 Thread Devan L

amfr wrote:
 I am writing a webserver, and I want it to be able to run python
 scripts.  But when I set sys.stdin to self.rfile (using the
 BaseHTTPServer class, self.rfile is a input stream containing the
 request), the cgi module does not parse the data.
 Example script:
 import cgi
 form = cgi.FieldStorage()
 print form[testfield]

 The above script would print nothing.  Should i be setting sys.stdin to
 something else, or is there anthoer solution entirely?
try this before trying to print:
print Content-Type: text/html\n

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


Re: slice notation as values?

2005-12-10 Thread Devan L

Antoon Pardon wrote:
 On 2005-12-10, Duncan Booth [EMAIL PROTECTED] wrote:
[snip]
  I also think that other functions could benefit. For instance suppose
  you want to iterate over every second element in a list. Sure you
  can use an extended slice or use some kind of while. But why not
  extend enumerate to include an optional slice parameter, so you could
  do it as follows:
 
for el in enumerate(lst,::2)
 
  'Why not'? Because it makes for a more complicated interface for something
  you can already do quite easily.

 Do you think so? This IMO should provide (0,lst[0]), (2,lst[2]),
 (4,lst[4]) ...

 I haven't found a way to do this easily. Except for something like:

 start = 0:
 while start  len(lst):
   yield start, lst[start]
   start += 2

 But if you accept this, then there was no need for enumerate in the
 first place. So eager to learn something new, how do you do this
 quite easily?

 lst = ['ham','eggs','bacon','spam','foo','bar','baz']
 list(enumerate(lst))[::2]
[(0, 'ham'), (2, 'bacon'), (4, 'foo'), (6, 'baz')]

No changes to the language necessary.

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


Re: join dictionaries using keys from one values

2005-12-05 Thread Devan L

ProvoWallis wrote:
 Thanks so much. I never would have been able to figure this out on my
 own.

 def dictionary_join(one, two):

  dict2x = dict( ((dict2[k], k) for k in dict2.iterkeys()))
  dict3 = dict(((k, dict2x[v]) for k,v in dict1.iteritems()))
  print dict3

 dict1 = {1:'bbb', 2:'aaa', 3:'ccc'}

 dict2 = {'5.01':'bbb', '6.01':'ccc', '7.01':'aaa'}

 dictionary_join(dict1, dict2)

You might want to make a working function.

def join_dicts(d1,d2):
temp = dict(((d2[k], k) for k in d2.iterkeys()))
joined = dict(((k, temp[v]) for k,v in d1.iteritems()))
return joined

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


Re: what's wrong with lambda x : print x/60,x%60

2005-12-05 Thread Devan L
Steve Holden wrote:
 [EMAIL PROTECTED] wrote:
  Gary Herron [EMAIL PROTECTED] wrote in message
  news:[EMAIL PROTECTED]
 
 --snip--
 So just use a def.  It is constantly pointed out on
 this list that the lambda provides no extra expressive power, it is
 merely a shortcut
 
 
  No, it is not merely a shortcut.  It often allows one to avoid
  polluting
  the namespace with a completely superfluous function name, thus
  reducing code smell.  It can also avoid a multi-line function defintion
  which often pushes other relevant code off the current page and
  out of view, and thus lambda can increase program readability.
 
 I'm getting a little tired of namespace pollution being trotted out as
 a reason to use lambda. Defining a function, and giving it a name, isn't
 polluting the namespace, any more than assigning sub-expressions to
 temporary variables is polluting the namespace. Why use temporary
 variables when all you have to do is make your expressions three lines
 long to avoid polluting the namespace?

I agree, if I were to name my function `shuffle', then I wouldn't be
able to name any of my intermediate functions `shuffle'! Oh the
humanity! Therefore I will instead use adhere to my 0 namespace
pollution policy and use this instead:
__nopollution_at__all_portion = range(50)
__nopollution_at__all_word = 'mint white chocolate chip'
if __name__ == '__main__':
print (lambda portion,word:(lambda x,word:[x.pop() for i in
range(len(word))]+x)((lambda x,word:[x.pop() for i in
range(len(word))]+x)((lambda x,word:[x.pop() for i in
range(len(word))]+x)((lambda x,word:[x.pop() for i in
range(len(word))]+x)(portion[:],word),word),word),word))(__nopollution_at__all_portion,__nopollution_at__all_word)

;-)

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


Re: Recursion bug...

2005-11-30 Thread Devan L
ex_ottoyuhr wrote:
 To start with, I'm new at Python, so if this is something relatively
 ordinary or a symptom of thinking in C++, I apologize...

 Anyhow, I'm currently trying to write a means of generating
 genetic-programming functions in Python; the details would be a little
 much for a Usenet post, but suffice it to say that it would involve
 trees of objects with an opcode and a variable number, between in this
 case 0 and 3, of 'arguments' -- also objects of the same sort. As a
 function to implement them, I'm doing something to the effect of this:

 [ex_ottoyuhr's code]

 Sorry to have given such a long example... But anyways, the problem is
 that, while I think this should generate a number of children for each
 toRet equivalent to the required number of children, it's actually
 appending all children generated in the function to the 'root' toRet
 rather than to child functions -- so that if I ask for a tree with
 depth 2, and the root would have 2 arguments, the first child would
 have 1, and the second child would have 2, the result is a root with a
 5-argument-long children and two child functions with 0-argument ones.
 There's some other strange stuff going on here, too; in particular,
 with one particular opcode, toRet is assigned a member 'value' which is
 randomly generated between 0 and 10,000. All toRets assigned value seem
 to be ending up with the same result...

 Could anyone explain what I'm doing wrong? I'm beginning to suspect
 that Python scope rules must not work like my native C++; have I made a
 common mistake?

Well, for one, in your __init__ method, you never do anything with
anOpcode. You simply assign the name 'opcode' to anOpcode. The reason
why everything is the same is that you're accessing
TreeCommand.children or Treecommand.opcode, which is shared by all
instances unless you assign to it. And you never increment generated,
so I don't see why the while loop would ever end, unless you
intentionally wanted that. Try this code instead:

max_opcode = 20
max_with_args = 15

class TreeCommand:
def __init__(self, opcode) :
self.opcode = opcode
self.children = []

def MakeTreeCommand(depth, maxdepth) :
if depth == 0:
command = TreeCommand(random.randint(0, max_with_args)
elif depth == maxdepth:
command = TreeCommand(random.randint(max_with_args+1,
max_opcode))
else:
command = TreeCommand(random.randint(0, max_opcode))

if command.opcode = max_with_args:
children_required = something_greater_than_0
else:
children_required = 0

generated = 0

for i in range(children_required):
command.children.append(MakeTreeCommand(depth+1, maxdepth))

return command

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


Re: XUL behavior in Python via XPCOM, Mozilla

2005-11-12 Thread Devan L
Terry Hancock wrote:
 I recently saw a claim that Mozilla XUL behaviors (normally
 scripted in Javascript) can (or perhaps will) be scriptable
 in Python.

 Also, other languages such as Java or Python are supported
 through XPCOM, said about Mozilla (from Luxor website).

 Yes, I know several ways to *generate* XUL from Python, and
 at least one way to use XUL to create interfaces for Python
 programs, but in this case, I'm talking about defining
 button action behavior in XUL by calling Python scripts.

 I know that Javascript is the preferred language, but I've
 seen several references to being able to do this in Python,
 including a claim that a release was targeted for early
 November (2005), to provide this.

 Now I can't find it again.  Anyway, I was hoping someone
 on c.l.p / python.org would have a reliable reference on
 this.

 Thanks,
 Terry

 --
 Terry Hancock ([EMAIL PROTECTED])
 Anansi Spaceworks http://www.AnansiSpaceworks.com

Nufox?
http://trac.nunatak.com.au/projects/nufox

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


Re: Command-line tool able to take multiple commands at one time?

2005-11-11 Thread Devan L
Peter A. Schott wrote:
 OK - I justed tested and may be doing something wrong, but it didn't work 
 when I
 just tried it.

 I have something like this:

 X = Value1
 Y = Value2
 Z = Value3

 etc at the top of my script.  When I copy/paste those three lines all at once
 into IDLE's interactive window, X is defined, Y and Z are not.

 That's more the behaviour I was hoping for - the ability to run parts of my 
 code
 at a time in order to work through issues without too much trouble in
 mostly-working code.

 TIA,

 -Pete Schott

Oh, that kind of multi-line input. I thought you were talking about
 def foo(bar):
...long_statement_here
...even_longer_statement_here
...return something

I would just put it into a .py and run it through IDLE, change it, and
rerun it. There are probably other IDEs which can already do that, but
I just use IDLE.

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


Re: Command-line tool able to take multiple commands at one time?

2005-11-10 Thread Devan L

Peter A. Schott wrote:
 Per subject - I realize I can copy/paste a line at a time into an interactive
 session when I'm trying to debug, but was wondering if there is any tool out
 there that allows me to copy sections of working Python scripts to paste into 
 my
 interactive console and let those run so I don't have to copy line-by-line.

 Not sure if iPython would meet that criteria or not or even if such a beast
 exists.  It would be helpful for debugging some of my simpler, yet still 
 lengthy
 at times, scripts.

 Thanks in advance.

 -Pete Schott

It's called IDLE (At least, the interactive session with it). Some
people dislike IDLE, though.

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


Re: Underscores in Python numbers

2005-11-07 Thread Devan L
Gustav Hållberg wrote:
 I tried finding a discussion around adding the possibility to have
 optional underscores inside numbers in Python. This is a popular option
 available in several competing scripting langauges, that I would love
 to see in Python.

 Examples:
   1_234_567
   0xdead_beef
   3.141_592

 Would appreciate if someone could find a pointer to a previous
 discussion on this topic, or add it to a Python-feature-wishlist.

 - Gustav

I'm not sure what the _s are for, but I'm guessing they serve as
separators (. or , depending on where you're from). I think the _s
look ugly to me, besides, underscores look more like spaces than
separators.

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


Re: Cheapest pocket device to code python on

2005-11-03 Thread Devan L

[EMAIL PROTECTED] wrote:
 What is the cheapest/affordable pocket device that I can code python
 on? I think the closest I have seen is pocketpc from this page:

 http://www.murkworks.com/Research/Python/PocketPCPython/Overview

I would not recommend trying to code on a handheld device. Small screen
size and [usually] small keyboards make it less-than-practical. Stick
with a laptop, or write it in a notebook, if you must.

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


Re: Importing Modules

2005-11-02 Thread Devan L

Sam Pointon wrote:
 On the second point, a combination of sys.path, os.listdir and
 __import__ should do what you're after, although sifting through the
 whole of sys.path and subfolders from Python, rather than the
 interpreter itself, could be slow.  (And it'll be redundant as well -
 __import__ will have do the same thing, though you could fix that by
 using the imp module).

 -Should- work, but not tested, so don't blame me if it doesn't:

[code]

__import__(modulename) is not equivalent to import modulename;
__import__ returns a module object

 __import__('pickle')
module 'pickle' from 'E:\Python23\lib\pickle.py'
 pickle

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


Re: Importing at runtime

2005-10-24 Thread Devan L
http://www.python.org/doc/2.4.2/lib/built-in-funcs.html

or, if you want an answer in code now and don't want to read the docs

def my_import(name):
module = __import__(name)
globals()[name] = module #not a good idea

Or, seeing as how you won't be directly accessing them by name, anyways

modules = [__import__(name) for name in module_names]

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


Re: execution order in list/generator expression

2005-10-23 Thread Devan L
[EMAIL PROTECTED] wrote:
 Hi,

 I am wondering how this is evaluated.

 a=(x for x in [1,2,3,4])
 p=[4,5]

 c=[x for x in p if x in list(a)]

 c is []

 but if I expand a first, like a = list(a)

 c is [4]

 So it seems that the if part don't get expanded ?

Well, for every element in p it recalculates list(a). Since the
generator is exhausted after making a list from it, it gives you
nothing afterwards. So after it checks the first element, it's
equivalent to [x for x in p if x in []].

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


Re: Character Sequence Generation

2005-09-22 Thread Devan L

Jeff Schwab wrote:
 [EMAIL PROTECTED] wrote:
  Jeff Schwab wrote:
 
 What's the best way to generate a sequence of characters in Python?  I'm
 looking for something like this Perl code: 'a' .. 'z' .
 
 
 import string
 
 
 print string.ascii_lowercase
 
  abcdefghijklmnopqrstuvwxyz

 Thanks.  Is there a good way to generate the characters dynamically?

''.join([chr(i) for i in range(97,123)])

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


Re: Newbie - instanciating classes from other files

2005-09-14 Thread Devan L

[EMAIL PROTECTED] wrote:
 Hey guys, i just started learning python (i usually use java/C).

 this has got me stumped as its not mentioned in the documentation
 (unless im skimming it every time).

 How does one instanciate a class from another file

import somefile
foo = somefile.class(__init__arguments-should-go-here)

Don't include the .py, it's won't work.

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


Re: infinite loop

2005-09-06 Thread Devan L

LOPEZ GARCIA DE LOMANA, ADRIAN wrote:
 Hi all,

 I have a question with some code I'm writting:


 def main():

 if option == 1:

 function_a()

 elif option == 2:

 function_b()

 else:

 raise 'option has to be either 1 or 2'

 if iteration == True:

 main()

 def function_a():

 print 'hello from function a'

 return None

 def function_b():

 print 'hello from function b'

 return None

 iteration = True

 option = 1

 main()


 I want an infinite loop, but after some iterations (996) it breaks:


 [EMAIL PROTECTED] tmp]$ python test.py
 hello from function a
 hello from function a
 hello from function a
 .
 .
 .
 hello from function a
 hello from function a
 Traceback (most recent call last):
   File test.py, line 35, in ?
 main()
   File test.py, line 17, in main
 main()
   File test.py, line 17, in main

 .
 .
 .
 .
   File test.py, line 17, in main
 main()
   File test.py, line 17, in main
 main()
   File test.py, line 5, in main
 function_a()
 RuntimeError: maximum recursion depth exceeded


 I don't understand it. Why am I not allowed to iterate infinitely? Something 
 about the functions? What should I do for having an infinite loop?

 Thanks in advance for your help,

 Adrián.

You've written a recursive function-you're not iterating. The recursion
limit is there to keep you from making something which will do
something bad, like recurse cyclically.

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


Re: Python compiled?

2005-09-05 Thread Devan L
billiejoex wrote:
 Hi all. I'm sorry for a noob question like this but I'll try to ask it
 anyway.
 One of the greatest problem that may discourage a new user to choose Python
 language is it's interpreted nature.

What? The instant gratification of immediate results is not
discouraging.

 Another important problem is that no interpreter is installed on Windows
 machine by default and this makes harder to distribute the software.

Python isn't that big to install, and people don't have problems with
small downloads

 Does it is planned that, in a far future, Python will implement a form of
 compilation?
 This would be awesome.

Again, I don't see how compiling would make Python better.

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


Re: 'isa' keyword

2005-09-03 Thread Devan L
talin at acm dot org wrote:
 Thanks for all the respones :) I realized up front that this suggestion
 is unlikely to gain approval, for reasons eloquently stated above.
 However, there are still some interesting issues raised that I would
 like to discuss.

 Let me first respond to a few of the comments:

 What's the difference between this and ``isinstance`` ?

 What's the difference between 'in' and 'has_key()? 1) Its shorter and
 more readable, 2) it can be overridden to mean different things for
 different container types.

Your analogy doesn't apply to non dictionaries. In any case, nothing
stops you from writing your own has_key() method for a different
container type. Likewise, if you made an isa keyword, it would just
call a method to have the traits you described above. You could write
your own method to see if it was an instance of the class, but it would
end up being more or less similar to isinstance().

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


Re: python logo

2005-09-03 Thread Devan L

Xah Lee wrote:
 what's the decision? any reference to the discussion?

 i thought it is better for Python to have one single recognizable logo.
 Perhaps python doesn't have a logo and the official python people
 decided it shouldn't or just doesn't have one currently?

 of course, a logo helps in identity and as well as advertisement.

  Xah
  [EMAIL PROTECTED]
 ∑ http://xahlee.org/

 Steve Holden wrote:
  Xah Lee wrote:
   i noticed that Python uses various logos:
  
   http://python.org/pics/pythonHi.gif
   http://python.org/pics/PyBanner038.gif
   http://python.org/pics/PyBanner037.gif
   http://python.org/pics/PythonPoweredSmall.gif
   http://wiki.python.org/pics/PyBanner057.gif
  
is this some decision that python should use various different logos?
  
Xah
[EMAIL PROTECTED]
   ∑ http://xahlee.org/
  
  Yes.
  --
  Steve Holden   +44 150 684 7255  +1 800 494 3119
  Holden Web LLC http://www.holdenweb.com/

There should only be one obvious way to do it, but there are many
creative ways to do it too.

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

Re: Adding bound methods dynamically... CORRECTED

2005-08-30 Thread Devan L
Kevin Little wrote:
 I want to dynamically add or replace bound methods in a class.  I want
 the modifications to be immediately effective across all instances,
 whether created before or after the class was modified.  I need this
 to work for both old ('classic') and new style classes, at both 2.3
 and 2.4.  I of course want to avoid side effects, and to make the
 solution as light-weight as possible.

 Question for the experts: Is the solution coded in AddBoundMethod()
 acceptable to the Pythonian Gods? :) It does seem to work -- tested
 at 2.3.5 (RH Linux) and 2.4.1 (WinXP)

 [Code]

I'm not an expert, but why do you need to dynamically add or replace
bound methods?

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


Re: aproximate a number

2005-08-29 Thread Devan L
Thomas Bartkus wrote:
 On Sun, 28 Aug 2005 23:11:09 +0200, billiejoex wrote:

  Hi all. I'd need to aproximate a given float number into the next (int)
  bigger one. Because of my bad english I try to explain it with some example:
 
  5.7 -- 6
  52.987 -- 53
  3.34 -- 4
  2.1 -- 3
 

 The standard way to do this is thus:

 def RoundToInt(x):
  Round the float x to the nearest integer 
 return int(round(x+0.5))

 x = 5.7
 print x, '--', RoundToInt(x)
 x = 52.987
 print x, '--', RoundToInt(x)
 x = 3.34
 print x, '--', RoundToInt(x)
 x = 2.1
 print x, '--', RoundToInt(x)

 5.7 -- 6
 52.987 -- 53
 3.34 -- 4
 2.1 -- 3

RoundToInt(2.0) will give you 3.

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


Re: aproximate a number

2005-08-29 Thread Devan L
Grant Edwards wrote:
 On 2005-08-30, Devan L [EMAIL PROTECTED] wrote:
 
  RoundToInt(2.0) will give you 3.

 That's what the OP said he wanted.  The next bigger integer
 after 2.0 is 3.

 --
 Grant Edwards   grante Yow!  I'd like TRAINED
   at   SEALS and a CONVERTIBLE on
visi.commy doorstep by NOON!!

It's not really clear whether he wanted it to round up or to go to the
next biggest integer because he says he has bad english. I can't think
of a particular use of returning the next bigger integer.

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


Re: Dictionary inheritance

2005-08-12 Thread Devan L
Talin wrote:
 I want to make a dictionary that acts like a class, in other words,
 supports inheritance: If you attempt to find a key that isn't present,
 it searches a base dictionary, which in turn searches its base, and so on.

 Now, I realize its fairly trivial to code something like this using
 UserDict, but given that classes and modules already have this behavior,
 is there some built-in type that already does this?

 (This is for doing nested symbol tables and such.)

 ---

 Also, on a completely different subject: Has there been much discussion
 about extending the use of the 'is' keyword to do type comparisons a la
 C# (e.g. if x is list:) ?

 -- Talin

Dictionaries aren't classes? I wasn't aware of that. Anyways, what
you're looking for, I think is a class that emulates a dictionary.
Probably you should just have some attribute that references a bigger
dictionary.

bigger_dict =
{'foo':1,'baz':2,'bar':3,'foobar':4,'foobaz':5,'foobazbar':6}
smaller_dict = {'spam':1,'ham':2,'bacon':3,'eggs':4}
smaller_dict.fallback = bigger_dict

and then the __getitem__ method might look something like

def __getitem__(self, key):
if self.has_key(key):
return self[key]
else:
return self.fallback[key]

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


Re: Regular expression to match a #

2005-08-11 Thread Devan L
John Machin wrote:
 Aahz wrote:
  In article [EMAIL PROTECTED],
  John Machin  [EMAIL PROTECTED] wrote:
 
 Search for r'^something' can never be better/faster than match for
 r'something', and with a dopey implementation of search [which Python's
 re is NOT] it could be much worse. So please don't tell newbies to
 search for r'^something'.
 
 
  You're somehow getting mixed up in thinking that ^ is some kind of
  not operator -- it's the start of line anchor in this context.

 I can't imagine where you got that idea from.

 If I change [which Python's re is NOT] to [Python's re's search() is
 not dopey], does that help you?

 The point was made in a context where the OP appeared to be reading a
 line at a time and parsing it, and re.compile(r'something').match()
 would do the job; re.compile(r'^something').search() will do the job too
 -- BECAUSE ^ means start of line anchor -- but somewhat redundantly, and
 very inefficiently in the failing case with dopey implementations of
 search() (which apply match() at offsets 0, 1, 2, .).

I don't see much difference.
Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)]
on win32
Type copyright, credits or license() for more information.


Personal firewall software may warn about the connection IDLE
makes to its subprocess using this computer's internal loopback
interface.  This connection is not visible on any external
interface and no data is sent to or received from the Internet.


IDLE 1.1.1
 import timeit
 t1 = timeit.Timer('re.search(^\w, will not work)','import re')
 t1.timeit()
34.938577109660628
 t2 = timeit.Timer('re.match(\w, will not work)','import re')
 t2.timeit()
31.381461330979164
 3.0/100
3.0001e-006
 t1.timeit()
35.282282524734228
 t2.timeit()
31.403153752781463

~4 second difference after a million times through seems to be trivial.
Then again, I haven't tested it for larger patterns and strings.

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


Re: Regular expression to match a #

2005-08-11 Thread Devan L

John Machin wrote:
 Devan L wrote:
  John Machin wrote:
 
 Aahz wrote:
 
 In article [EMAIL PROTECTED],
 John Machin  [EMAIL PROTECTED] wrote:
 
 
 Search for r'^something' can never be better/faster than match for
 r'something', and with a dopey implementation of search [which Python's
 re is NOT] it could be much worse. So please don't tell newbies to
 search for r'^something'.
 
 
 You're somehow getting mixed up in thinking that ^ is some kind of
 not operator -- it's the start of line anchor in this context.
 
 I can't imagine where you got that idea from.
 
 If I change [which Python's re is NOT] to [Python's re's search() is
 not dopey], does that help you?
 
 The point was made in a context where the OP appeared to be reading a
 line at a time and parsing it, and re.compile(r'something').match()
 would do the job; re.compile(r'^something').search() will do the job too
 -- BECAUSE ^ means start of line anchor -- but somewhat redundantly, and
 very inefficiently in the failing case with dopey implementations of
 search() (which apply match() at offsets 0, 1, 2, .).
 
 
  I don't see much difference.

 and I didn't expect that you would -- like I wrote above: Python's re's
 search() is not dopey.

Your wording makes it hard to distinguish what exactly is dopey.

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


Re: NEWB: General purpose list iteration?

2005-08-11 Thread Devan L
def descend(iterable):
if hasattr(iterable, '__iter__'):
for element in iterable:
descend(element)
else:
do_something(iterable)

This will just do_something(object) to anything that is not an
iterable. Only use it if all of your nested structures are of the same
depth.

If you used it on the following list of lists

[[something],[[something_else],[other_thing]]]

it would modify something, something_else, and other_thing.

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


Re: Some newbie cgi form questions...

2005-08-07 Thread Devan L

googleboy wrote:
 for key in form.keys():Yes,  I know which fields are hidden because I
 made them that way,  but  I am trying to figure out a way I can iterate
 over the fields and do one thing with hidden fields (assign them to
 variables that tell the form how to process) and a different thing with
 non hidden fields (present them back to the browser with after
 emailling them on to the form owner).  I want this script to work fro
 several forms on the same webserver, some of which will have several
 different and individual names.  I am also hopeful that I may be able
 to extrapolate from that ways to differentiate other things from my
 form input things, but we'll see how it goes.

 My html code looks like this:

   h1Send Feedback/h1

   form action=cgi/formparse.py method=get
 input name=subject type=text size=32 maxlength=196Subjectbr
 input name=name type=text maxlength=64Your namebr
 input name=email1 type=text size=24 maxlength=128Your email
 address. br
 brbrPlease let me know your thoughts below.brtextarea
 name=thoughts cols=64 rows=12/textareabr
 input  type=submit value=Submit
 input type=hidden name=recipient value=[EMAIL PROTECTED]
 input type=hidden name=email value=[EMAIL PROTECTED]
 input type=hidden name=mailserver value=localhost
 input name=Clear type=button value=Clear/form


 In other news, I have tried suggested syntaxes aboce (thanks guys!) but
 I get strange errors.  If the following:

 for key in form.keys():
 if not form.keys():
 print h1 ERROR! h1BR
 print Please go back and fill in all fields.  All fields are
 required.br

 I get a traceback in teh error_log file that looks like this:

 Traceback (most recent call last):
   File cgi/form.py, line 34, in ?
 for key, value in form.items():
   File /usr/local/lib/python2.3/cgi.py, line 533, in __getattr__
 raise AttributeError, name
 AttributeError: items

 if this:

 for key in form.keys():
 if not form.keys()[key]:
 print h1 ERROR! h1BR
 print Please go back and fill in all fields.  All fields are
 required.br
 print '/body/html'

 the I get this traceback:

 Traceback (most recent call last):
   File /var/www/users/senta/html/gobooks/cgi/form.py, line 35, in ?
 if not form.keys()[key]:
 TypeError: list indices must be integers

 As you can see,  I am using python 2.3 (my web service provider is
 responsible for this - I'd use 2.4.1 if I could)

 TIA

 googleboy

 import cgi
 form = cgi.FieldStorage()
 dir(form)
['FieldStorageClass', '_FieldStorage__write', '__contains__',
'__doc__', '__getattr__', '__getitem__', '__init__', '__iter__',
'__len__', '__module__', '__repr__', 'bufsize', 'disposition',
'disposition_options', 'done', 'file', 'filename', 'fp', 'getfirst',
'getlist', 'getvalue', 'has_key', 'headers', 'innerboundary',
'keep_blank_values', 'keys', 'length', 'list', 'make_file', 'name',
'outerboundary', 'read_binary', 'read_lines', 'read_lines_to_eof',
'read_lines_to_outerboundary', 'read_multi', 'read_single',
'read_urlencoded', 'skip_lines', 'strict_parsing', 'type',
'type_options']

The form is not a dictionary.

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


Re: Fat and happy Pythonistas (was Re: Replacement for keyword 'global' good idea? ...)

2005-08-06 Thread Devan L
 advisedly)
 against the map, filter and reduce operators?
 It seems to be completely irrational from my
 viewpoint. I've seen the arguements, and they
 make no sense.

Map and filter are not special. Why have two ways to do the same thing?
Reduce is a special case, but the arguments against map and filter are
quite sensible.

  BTW, I think large and stable would have been less offensive than fat
  and happy, but perhaps you meant to imply we're both lazy and complacent,
  rather than just satisfied with something that works and not inclined to
  shoot for moving targets every working day.  If so, I'm not sure why you'd
  say that, since the evidence doesn't support it.

 I'm not suggesting shooting at a moving target. I'm suggesting
 getting the head out of the sand, looking at trends, and figuring
 out the _large_ steps to take next, not the nickle and dime fixups
 that are the only things I see in PEP 3000. (Not to say that some
 of them aren't going to be a lot of work. Some of them are.)

 I came back from Agile2005 last week. The open space session
 on Ruby was maybe 5 times as large as the one on Python. And
 both of them were kind of an afterthought. Look at the OSCon
 blogs. There were a number of hardcore Python people there.
 They couldn't get into Ruby sessions because they were jammed.
 Standing room only.

More people does not mean better.

 There's a report out that the scripting languages in general are
 losing mindshare big time in Europe, Africa and the east in general.
 In fact, everywhere except in North America. I'm generally somewhat
 skeptical of these reports unless I can see the  methodology, but it's
 definitely a data point.

 Another thing that stands out: the explicit versus dynamic typing debate
 has moved on from program correctness (which is a wash) to
 other areas that explicit (or derived) type information can be used
 for. I see this in PyFit: the languages where explicit type information
 is available by reflection have cleaner implementations. The languages
 with dynamic typing all have to deal with the fact that they can't get
 type information by reflection, and it's a diverse mess.

 The world is moving on, in ways that I think you're not seeing.
 And Python is standing still in many of those same ways.

Take another look then.

-Devan L

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


Re: Newbie Program

2005-08-05 Thread Devan L
Eric wrote:
 I am reading a book on Python and ran across and exercise that I just
 can't seem to figure out.  Its pretty simple, but I just can't get
 past a certain point.

 The task is to create a program that flips a coin 100 times and keeps
 track of the total of heads and tails which is printed to the screen.

 My plan was to use import random with a range of 2 and use a
 conditional statement to count to 100. The problem I am encountering
 is how do I keep track of the totals for heads and tails?

 Any suggestions would be appreciated. Keep in mind I am very new to
 Python so there may be a more sophisticated way, but I am just trying
 to use what the book has taught so far.

 Thanks,
 Eric

Someone else read the same book, I suppose.

http://groups-beta.google.com/group/comp.lang.python/browse_thread/thread/87fcaf8ffbbd69e7/c7ff2d2b5b0d776d?q=coin+fliprnum=3#c7ff2d2b5b0d776d

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


Re: Wheel-reinvention with Python

2005-08-01 Thread Devan L
Ed Leafe wrote:
 On Sunday 31 July 2005 22:39, Paul Rubin wrote:

   import dabo
   app = dabo.dApp()
   dApp.start()
  
Sorry, I couldn't do it in 5.  ;-) Oh, and that includes a full menu,
   too.
 
  I get an ImportError exception when I try that.  Any suggestions?  Note
  that I don't get that exception from Tkinter.
  bash-3.00$ python
  Python 2.3.4 (#1, Oct 26 2004, 16:42:40)
  [GCC 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)] on linux2
  Type help, copyright, credits or license for more information.
   import dabo
  Traceback (most recent call last):
File stdin, line 1, in ?
  ImportError: No module named dabo
   import Tkinter
  

  Oh, c'mon now Paul, now you're trolling. You know exactly what the problem
 is, and try to make it look like a bug.

  Fine: you don't want to use anything that doesn't come standard with Python.
 You've made your point. We get it. There is no need to repeat yourself
 constantly.

  The only point of my post was that for those without your aversion to
 installing useful tools, Dabo provides a ton of functionality. Also, as my
 partner Paul McNett pointed out, I could have done it in *two* lines:

 import dabo
 dabo.dApp().start()

 ;-)

 --

 -- Ed Leafe
 -- http://leafe.com
 -- http://dabodev.com
If you're creating a new instance of your dApp(I assume its a class)
with no arguments, then effectively your just creating a default
program which is already defined in the dabo module. If you could write
it in a few, short lines of code by defining a new class, then you
might have something there.

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


Re: [pygame]how to copy a surface to an other surface with alpha value?

2005-07-27 Thread Devan L


flyaflya wrote:
 I want to join some surfaces to a new big surface with alpha cannel, I want
 the new surface has  same pixels(inclue r,g,b and alpha value) as the pixels
 on the source surfaces.
 my code as follow:

 surf = pygame.Surface((200,200))
 surf.blit(surf1, (0,0))
 surf.blit(surf2, (0,100))
 .

 but these codes can't copy alpha value to the new surface.how can I deal
 with it?

Try the pygame mailing list or irc channel.
http://www.pygame.org/info.shtml

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


Re: searching string url

2005-07-27 Thread Devan L
Sounds somewhat like homework. So I won't just give you a code
solution. Use the regular expression(re) module to match the urls.

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


Re: A Module on Time Date

2005-07-26 Thread Devan L


Robert Maas, see http://tinyurl.com/uh3t wrote:
  From: Robert Kern [EMAIL PROTECTED]
  As you can see in the datetime documentation, the module was introduced
  in Python 2.3. I recommend updating your Python installation.

 What do you mean your?? I don't have any Python installation of my
 own. All I have is what this small local ISP provides on its Unix shell
 machine which I share with hundreds of other users. There's no way I
 can install a new version of anything on the system account.
 Your recommendation will be disregarded as total crap. plonk

You should contact your isp and ask them to upgrade, then.

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


Re: Problem loading a file of words

2005-07-24 Thread Devan L
teoryn wrote:
 I've been spending today learning python and as an exercise I've ported
 a program I wrote in java that unscrambles a word. Before describing
 the problem, here's the code:

 *--beginning of file--*
 #!/usr/bin/python
 # Filename: unscram.py

 def sort_string(word):
 '''Returns word in lowercase sorted alphabetically'''
 word = str.lower(word)
 word_list = []
 for char in word:
 word_list.append(char)
 word_list.sort()
 sorted_word = ''
 for char in word_list:
 sorted_word += char
 return sorted_word

 print 'Building dictionary...',

 dictionary = { }

 # Notice that you need to have a file named 'dictionary.txt'
 # in the same directory as this file. The format is to have
 # one word per line, such as the following (of course without
 # the # marks):

 #test
 #hello
 #quit
 #night
 #pear
 #pare

 f = file('dictionary.txt')

 # This loop builds the dictionary, where the key is
 # the string after calling sort_string(), and the value
 # is the list of all 'regular' words (from the dictionary,
 # not sorted) that passing to sort_string() returns the key

 while True:
 line = f.readline()
 if len(line) == 0:
 break
 line = str.lower(line[:-1]) # convert to lowercase just in case
 and
 # remove the return at the end of
 the line
 sline = sort_string(line)
 if sline in dictionary: # this key already exist, add to
 existing list
 dictionary[sline].append(line)
 print 'Added %s to key %s' % (line,sline) #for testing
 else:   # create new key and list
 dictionary[sline] = [line]
 print 'Created key %s for %s' % (sline,line) #for
 testing
 f.close()

 print 'Ready!'

 # This loop lets the user input a scrambled word, look for it in
 # dictionary, and print all matching unscrambled words.
 # If the user types 'quit' then the program ends.
 while True:
 lookup = raw_input('Enter a scrambled word : ')

 results = dictionary[sort_string(lookup)]

 for x in results:
 print x,

 print

 if lookup == 'quit':
 break
 *--end of file--*


 If you create dictionary.txt as suggested in the comments, it should
 work fine (assumeing you pass a word that creates a valid key, I'll
 have to add exceptions later). The problem is when using a large
 dictionary.txt file (2.9 MB is the size of the dictionary I tested) it
 always gives an error, specifically:
 (Note: ccehimnostyz is for zymotechnics, which is in the large
 dictionary)


 *--beginning of example--*
 Enter a scrambled word : ccehimnostyz
 Traceback (most recent call last):
   File unscram.py, line 62, in ?
 results = dictionary[sort_string(lookup)]
 KeyError: 'ccehimnostyz'
 *--end of example--*


 If you'd like a copy of the dictionary I'm using email me at teoryn at
 gmail dot com or leave your email here and I'll send it to you (It's
 702.2 KB compressed)

 Thanks,
 Kevin

Heh, it reminds me of the code I used to write.

def sort_string(word):
return ''.join(sorted(list(word.lower(
f = open('dictionary.txt','r')
lines = [line.rstrip('\n') for line in f.readlines()]
f.close()
dictionary = dict((sort_string(line),line) for line in lines)
lookup = ''
while lookup != 'quit':
lookup = raw_input('Enter a scrambled word:')
if dictionary.has_key(lookup):
word = dictionary[lookup]
else:
word = 'Not found.'
print word

You need python 2.4 to use this example.

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


Re: Problem loading a file of words

2005-07-24 Thread Devan L


Robert Kern wrote:
 That's definitely not the kind of dictionary that he wants.

 --
 Robert Kern
 [EMAIL PROTECTED]

 In the fields of hell where the grass grows high
   Are the graves of dreams allowed to die.
-- Richard Harter

Oh, I missed the part where he put values in a list.

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


Re: Odd behaviour of regexp module

2005-07-13 Thread Devan L
Why doesn't group have an argument? group() or group(0) returns what
the pattern matched, not what it returns.

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


Re: question on input

2005-07-12 Thread Devan L
Use raw_input instead. It returns a string of whatever was typed. Input
expects a valid python expression.

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


Re: Help with inverted dictionary

2005-07-12 Thread Devan L
import re
name = Robert
f = file('phonebook.txt','r')
lines = [line.rstrip(\n) for line in f.readlines()]
pat = re.compile(name, re.I)
related_lines = [line for line in lines if pat.search(line)]

And then you write the lines in related_lines to a file. I don't really
write text to files much so, um, yeah.

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


Re: Creating anonymous functions using eval

2005-07-12 Thread Devan L
How is this different from a nested function?

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


Re: Help with inverted dictionary

2005-07-12 Thread Devan L
I think you need to get a database. Anyways, if anything, it should
create no more than 5,000 files, since 5,000 facts shouldn't talk about
30,000 animals. There have been a few discussions about looking at
files in directories though, if you want to look at those.

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


Re: Help with inverted dictionary

2005-07-12 Thread Devan L
Oh, I seem to have missed the part saying 'or other word'. Are you
doing this for every single word in the file?

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


Re: Creating anonymous functions using eval

2005-07-12 Thread Devan L
Well, the string that gets passed is more or less a function
definition, which is then called with exec. I don't see why you'd need
to write a string out with the function definition and then call it.
You could just write the function.

As for the nested functions, I had been presuming that it was intended
to use as a better function than lambda within a function. Sorry for
the confusion.

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


Re: automatically assigning names to indexes

2005-07-12 Thread Devan L
import math
class Vector:
def __init__(self, coordinates):
self.coordinates = coordinates
self.magnitude = sum([c**2 for c in coordinates])**0.5
self.direction = getangle(Vector([1]+[0 for i in
range(len(coordinates)-1)]))
def dotproduct(self, vector):
sum([a*b for a,b in zip(self.coordinates,vector.coordinates)])
def crossproduct(self, vector, pvector):
return
pvector*self.magnitude*vector.magnitude*math.sin(self.getangle(vector))
def getangle(self, vector):
return
math.acos(self.dotproduct(vector)/(self.magnitude*vector.magnitude))
def __mul__(self, scalar):
return Vector([c*scalar for c in self.coordinates])
def __add__(self, vector):
return Vector([c+d for c,d in
zip(self.coordinates,vector.coordinates)])
def __sub__(self, vector):
return Vector([c-d for c,d in
zip(self.coordinates,vector.coordinates)])

What about this?

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


Re: Creating anonymous functions using eval

2005-07-12 Thread Devan L
 You missed Steven's point which is to quote the message to which you are
 replying. Not everyone is reading this list in a conveniently threaded
 form, so you need to provide some context for them to be able to follow
 along.

Ah, sorry, I didn't quite get what he was referring to.

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


Re: Yet Another Python Web Programming Question

2005-07-09 Thread Devan L
Take some time to learn one of the web frameworks. If your host doesn't
already have it, ask your host if they would consider adding it.

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


Re: Python Forum

2005-07-09 Thread Devan L
I see a total of 12 posts and 8 users.

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


Re: removing list comprehensions in Python 3.0

2005-07-08 Thread Devan L
List comprehensions are faster than generator comprehensions for
iterating over smaller sequences.

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


Re: removing list comprehensions in Python 3.0

2005-07-08 Thread Devan L
 import timeit
 t1 = timeit.Timer('list(i for i in xrange(10))')
 t1.timeit()
27.267753024476576
 t2 = timeit.Timer('[i for i in xrange(10)]')
 t2.timeit()
15.050426800054197
 t3 = timeit.Timer('list(i for i in xrange(100))')
 t3.timeit()
117.61078097914682
 t4 = timeit.Timer('[i for i in xrange(100)]')
 t4.timeit()
83.502424470149151

Hrm, okay, so generators are generally faster for iteration, but not
for making lists(for small sequences), so list comprehensions stay.

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


Re: map/filter/reduce/lambda opinions and background unscientific mini-survey

2005-07-06 Thread Devan L
 Here's a couple of examples from my own code:

 # from a Banzhaf Power Index calculator
 # adds things that aren't numbers
 return reduce(operator.add,
 (VoteDistributionTable({0: 1, v: 1}) for v in electoral_votes))

return sum([VoteDistributionTable({0:1, v:1} for v in
electoral_votes],VoteDistributionTable({}))
Any time you use operator.add, you can probably use
sum(sequence,initialvalue)

 # from a custom numeric class
 # converts a tuple of digits into a number
 mantissa = sign * reduce(lambda a, b: 10 * a + b, mantissa)

I'll admit I can't figure out a way to replace reduce without writing
some ugly code here, but I doubt these sorts of things appear often.

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


Re: map/filter/reduce/lambda opinions and background unscientific mini-survey

2005-07-05 Thread Devan L
def flatten(iterable):
if not hasattr(iterable, '__iter__'):
return [iterable]
return sum([flatten(element) for element in iterable],[])
Recursion makes things so much shorter.

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


Re: Favorite non-python language trick?

2005-07-03 Thread Devan L
Okay, maybe that was too restrictive, reduce can *usually* be replaced
with sum. Sorry about that.

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


Re: map/filter/reduce/lambda opinions and background unscientific mini-survey

2005-07-02 Thread Devan L
 Claiming that sum etc. do the same job is the whimper of
someone who doesn't want to openly disagree with Guido.

Could you give an example where sum cannot do the job(besides the
previously mentioned product situation?

Also, map is easily replaced.
map(f1, sequence) == [f1(element) for element in sequence]

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


Re: Favorite non-python language trick?

2005-07-02 Thread Devan L
sum(sequence[0] + [1/element for element in sequence[1:]])

I think that should work.

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


Re: Regular Expression for pattern substitution

2005-07-01 Thread Devan L
re.replace.

I don't think there's any way to avoid it. Except maybe having an alias
email address or a fake one.

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


Re: map/filter/reduce/lambda opinions and background unscientific mini-survey

2005-07-01 Thread Devan L
None of them are really indispensible. Map and filter cab be replaced
with list comprehensions. reduce is redundant except when multiplying a
series; there's a sum function for a reason. Lambda looks cleaner in
some cases, but you don't gain any functionality.

What really struck me, though, is the last line of the abstract:

I expect tons of disagreement in the feedback, all from
ex-Lisp-or-Scheme
folks. :-)

Guido wrote somewhere that the original map, filter, and reduce came
from a lisp hacker who missed them.

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


Re: Regular Expression for pattern substitution

2005-07-01 Thread Devan L
Hrm, thought it had one. Guess it would help if I actually used regular
expression for replacement.

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


Re: class attribute to instance attribute

2005-07-01 Thread Devan L
Well, I've never heard of a method like that for assigning variables.
I'd rather put it in the __init__ method.

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


Re: Favorite non-python language trick?

2005-07-01 Thread Devan L
With the exception of reduce(lambda x,y:x*y, sequence), reduce can be
replaced with sum, and Guido wants to add a product function.

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


Re: Python for everything?

2005-06-30 Thread Devan L
Python for everything except things that need to be ridiculously
optimized for speed. Thats what C embedded in Python and Psyco enhanced
Python code is for.

Oh wait, thats still all Python...

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


Re: class attribute to instance attribute

2005-06-30 Thread Devan L
Why make it an instance attribute? Couldn't you just look at the class
attribute? If its something that depends on each instance's value
assigned to the attribute, why not make it an instance attribute to
start with?

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


Re: How to run commands in command line from a script

2005-06-30 Thread Devan L
The code module, perhaps?
http://www.python.org/doc/2.4.1/lib/module-code.html

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


Re: Escaping commas within parens in CSV parsing?

2005-06-30 Thread Devan L
Try this.
re.findall(r'(.+? \(.+?\))(?:,|$)',yourtexthere)

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


Re: Splitting string into dictionary

2005-06-30 Thread Devan L
One line solution.
dict(re.findall(r'(.+?)' \| '(.+?)'(?:\s\||$),yourtexthere))

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


Re: regulars expressions ?

2005-06-28 Thread Devan L
re.findall(r'?(.+?)?(?:,|$)', yourtexthere)

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


Re: regulars expressions ?

2005-06-28 Thread Devan L
Oh, oops, sorry, that code doesn't respect the quotes.
Use this code:
re.findall(r'(.+?|\S+)(?:,|$)', yourtexthere)

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


Re: Newbie: Help Figger Out My Problem

2005-06-28 Thread Devan L
import random
flips = 100
results = [random.randint(0,1) for i in range(flips)]
heads = results.count(0)
tails = results.count(1)
print Heads:%s % heads
print Tails:%s % tails
I think this is more compact.

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


Re: When someone from Britain speaks, Americans hear a British accent...

2005-06-28 Thread Devan L
Thats like posting about Google here because the newsgroup is hosted on
Google.

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


Re: noob question

2005-06-26 Thread Devan L
To recognize variables that  you have assigned, just look for
assignment. If your code is readible, and you know it well, you
shouldn't need the $ sign in front of everything.

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


Re: Favorite non-python language trick?

2005-06-26 Thread Devan L
 return a if a.value == true
 database.query(q) unless database.connect == error
(etc)

if a.value == True:
return a

if not database.connect == error:
database.query(q)

Trading two words for one word doesn't necessarily make the code
better.

 unless false then print 1 # this prints 1 forever
while not False:
print 1

unless seems to become while not, as opposed to if not. Should be
more consistent.

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


Re: Favorite non-python language trick?

2005-06-25 Thread Devan L
Why overload when you can use class methods?

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


Re: what list comprehension can't

2005-06-24 Thread Devan L
I wasn't aware that python supported if then else.

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