running windows 'start' cmd using spawnl

2006-08-23 Thread Tor Erik
Hi,

I need to start a program in a new cmd-window. To do this I need to 
execute: start [command]
With os.system this is straight-forward.
But I need to do it with spawnl and P_NOWAIT. I.e, asynchronously.
The problem is that I need to know the path where start resides,
which I'm unable to find.

Does anyone know where this command is located, or an alternative way of 
doing what I want?

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


Client-side TCP socket receiving "Address already in use" upon connect

2006-09-02 Thread Tor Erik
Hi,

The reason is that my application does about 16 connects and data 
transfers per second, to the same 16 remote hosts. After approx 200 secs 
there are 4000 sockets waiting to be garbage collected by the OS. At 
this point is seems that connect loops and starts using the same local 
addresses it used 4000 connections ago, resulting in an "Address already 
in use" exception.

A possible solution to this would be to keep the connection to each 
remote host open, but that would require parsing of the received data 
into the data items (which are files by the way) sent by the application.

My question is if there are any other methods of solving this? Maybe a 
socket option of some sort...

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


Re: Client-side TCP socket receiving "Address already in use" upon connect

2006-09-03 Thread Tor Erik
Sybren Stuvel wrote:
> Tor Erik enlightened us with:
>> The reason is that my application does about 16 connects and data
>> transfers per second, to the same 16 remote hosts. After approx 200
>> secs there are 4000 sockets waiting to be garbage collected by the
>> OS.
> 
> Which OS are we talking about?

Windows XP

> 
>> At this point is seems that connect loops and starts using the same
>> local addresses it used 4000 connections ago, resulting in an
>> "Address already in use" exception.
> 
> After how many seconds does this happen?

200 seconds approx

> 
>> My question is if there are any other methods of solving this? Maybe
>> a socket option of some sort...
> 
> If I'm correct (please correct me if I'm not), on Linux you can use
> 'sysctl -w net.ipv4.tcp_fin_timeout=X' to set the time between closing
> the socket and releasing it to be reused. You can also check the
> SO_REUSEADDR argument to the setsockopt function. Read 'man 7 socket'
> for more info.

I've read about SO_REUSEADDR. As far as I understand, this is what 
SO_REUSEADDR is for:

1. Allow a listening socket to bind itself to its well-known port even 
if previously established connections use it as their local port. 
Setting this option should be done between calls to socket and bind, and 
hence is only usable for listening sockets, not client sockets like mine.

2. Allow multiple servers on the same host with different ip-adresses to 
listen to the same port.

I've tried setting this option, but could not see any notable changes...

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


Re: Client-side TCP socket receiving "Address already in use" upon connect

2006-09-03 Thread Tor Erik
Fredrik Lundh wrote:
> Tor Erik wrote:
> 
>> The reason is that my application does about 16 connects and data 
>> transfers per second, to the same 16 remote hosts. After approx 200 
>> secs there are 4000 sockets waiting to be garbage collected by the OS.
> 
> what does "netstat" say about these sockets ?
> 
> 
> 

They are in the TIME_WAIT state... The msn library has an article on how 
to solve this:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/BTS06CoreDocs/html/6987640c-1d80-4fbf-b43a-021fc8ba06a4.asp

Summing up one could either:

1. Increase the upper range of ephemeral ports that are dynamically 
allocated to client TCP/IP socket connections:

set registry key: 
KEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\MaxUserPort
to a new DWORD value... (5000 - 65534)
The default in XP is 3976 -> http://support.microsoft.com/kb/Q149532

or

2. Reduce the client TCP/IP socket connection timeout value from the 
default value of 240 seconds

set registry key:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\TcpTimedWaitDelay
to a new DWORD value (30 - 300)

The TCP RFC (RFC 793) recommends a value of 2*msl(Maximum Segment 
Lifetime). The general consensus about the value of msn seems to be 1-2 
minutes, depending on the underlying network... (2*2 min = 2*120 sec = 
240 sec)


I do not want to alter my registry, so I'm currently testing an idea 
where I let the client connect and send its content, appended with my 
own "magic" EOF byte-sequence. When the server receives this EOF, it 
takes care to close the connection. This should eliminate the problem as 
it is the peer closing the connection that enters the TIME_WAIT state...

I will report my experiences...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Client-side TCP socket receiving "Address already in use" upon connect

2006-09-03 Thread Tor Erik
Tor Erik wrote:
> Fredrik Lundh wrote:
>> Tor Erik wrote:
>>
>>> The reason is that my application does about 16 connects and data 
>>> transfers per second, to the same 16 remote hosts. After approx 200 
>>> secs there are 4000 sockets waiting to be garbage collected by the OS.
>>
>> what does "netstat" say about these sockets ?
>>
>> 
>>
> 
> They are in the TIME_WAIT state... The msn library has an article on how 
> to solve this:
> 
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/BTS06CoreDocs/html/6987640c-1d80-4fbf-b43a-021fc8ba06a4.asp
>  
> 
> 
> Summing up one could either:
> 
> 1. Increase the upper range of ephemeral ports that are dynamically 
> allocated to client TCP/IP socket connections:
> 
> set registry key: 
> KEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\MaxUserPort
>  
> 
> to a new DWORD value... (5000 - 65534)
> The default in XP is 3976 -> http://support.microsoft.com/kb/Q149532
> 
> or
> 
> 2. Reduce the client TCP/IP socket connection timeout value from the 
> default value of 240 seconds
> 
> set registry key:
> HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\TcpTimedWaitDelay
>  
> 
> to a new DWORD value (30 - 300)
> 
> The TCP RFC (RFC 793) recommends a value of 2*msl(Maximum Segment 
> Lifetime). The general consensus about the value of msn seems to be 1-2 
> minutes, depending on the underlying network... (2*2 min = 2*120 sec = 
> 240 sec)
> 
> 
> I do not want to alter my registry, so I'm currently testing an idea 
> where I let the client connect and send its content, appended with my 
> own "magic" EOF byte-sequence. When the server receives this EOF, it 
> takes care to close the connection. This should eliminate the problem as 
> it is the peer closing the connection that enters the TIME_WAIT state...
> 
> I will report my experiences...

Well...  my idea does not work as expected. Even though the server 
(remote host) calls socket.close(), it is the client that executes 
TIME_WAIT. My guess is that the subtrates below socket closes the 
connection at the peer calling connect regardless of where socket.close 
is called.

Thoughts anyone?
-- 
http://mail.python.org/mailman/listinfo/python-list


Get CPU usage of a single process in Windows

2006-09-08 Thread Tor Erik
Hi,

This should be possible as Taskmanager tracks CPU usage for every 
process... Anyone know how this can be done?

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


What algorithm does Python use to evaluate: if substring in string

2006-09-09 Thread Tor Erik
I would be surprised if it is the naive:

m = 0
s1 = "me"
s2 = "locate me"
s1len = len(s1)
s2len = len(s2)
found = False

while m + s1len <= s2len:
if s1 == s2len[m:m+s1len]:
found = True
break
m += 1
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What algorithm does Python use to evaluate: if substring in string

2006-09-09 Thread Tor Erik
Alex Martelli wrote:
> Tor Erik <[EMAIL PROTECTED]> wrote:
> 
>> I would be surprised if it is the naive:
> 
> Yep -- it's "a mix between Boyer-Moore and Horspool with a few more
> bells and whistles on the top", as documented and implemented in
> Objects/stringlib/fastsearch.h in the Python sources and well discussed
> and explained at http://effbot.org/zone/stringlib.htm .
> 
> 
> Alex
>  

Ok. Two questions:

1. Is "a in b" simply an alias for "b.find(a)"?

2. Is this algorithm exclusive to Python 2.5, or is it contained in 2.4 
aswell?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Windows bandwidth monitor

2006-09-13 Thread Tor Erik
RunLevelZero wrote:
> Hey guys, I've done some searching but can't seem to find anything that
> helps me out. I want to write a simple bandwidth monitor that will sit
> in the taskbar of windows and tell me my upload and download speeds. I
> intend to use wxpython for the little taskbar icon. If anyone can give
> me a helping hand I would be more than appreciative.
> 
> Thanks
> 

For the monitoring part, google "win32pdh"
-- 
http://mail.python.org/mailman/listinfo/python-list


Why not event-driven packages in other than the main thread?

2006-09-14 Thread Tor Erik
Hi,

I've developed an application were I've used Tkinter for the GUI.
When I ran the GUI in another thread than the main, it kept locking
up.
I experienced similar problems with Twisted.

Both of these tools are event-based, so I guess that is the root of the 
problem...

But could anyone tell me why running these in a thread other than the 
main one doesn't work?

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


Re: Why not event-driven packages in other than the main thread?

2006-09-14 Thread Tor Erik
Bjoern Schliessmann wrote:
> Tor Erik wrote:
> 
>> But could anyone tell me why running these in a thread other than
>> the main one doesn't work?
> 
> Just for personal interest: Why would you want to run the GUI in
> another thread? It's common to leave the GUI in the main thread and
> let worker threads handle heavy time-consuming stuff.
> 
> Regards,
> 
> 
> Björn
> 

If you have two event-based frameworks, both needing to run in the main 
thread, such as Tkinter and Twisted, you have a problem
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why not event-driven packages in other than the main thread?

2006-09-14 Thread Tor Erik
Jean-Paul Calderone wrote:
> On Thu, 14 Sep 2006 11:13:59 +0200, Tor Erik <[EMAIL PROTECTED]> wrote:
>> Hi,
>>
>> I've developed an application were I've used Tkinter for the GUI.
>> When I ran the GUI in another thread than the main, it kept locking
>> up.
>> I experienced similar problems with Twisted.
>>
>> Both of these tools are event-based, so I guess that is the root of the
>> problem...
>>
>> But could anyone tell me why running these in a thread other than the
>> main one doesn't work?
> 
> I don't know about Tkinter, but Twisted can be run in a thread other than
> the main thread.  Several projects make use of this feature extensively, so
> while I wouldn't recommend it, it should certainly work.
> 
> Perhaps you did something wrong in setting it up, or perhaps you have found
> a bug.  Can you post a minimal example to reproduce this to Twisted's bug
> tracker?
> 
> Jean-Paul

Oh... I've re-coded that part of my app. now, so it doesn't contain any 
references to Twisted anymore. However, I was aiming to build a scalable 
HTTP server, and tried using Twisted for the task. Whenever I started 
the thing from the main thread (as a separate thread), nothing happened. 
That is, I received no exceptions, but the HTTP server was not 
running... When executing the HTTP server as a separate app., things 
worked perfectly.
-- 
http://mail.python.org/mailman/listinfo/python-list


How can I set the size of a window with tkinter?

2005-09-27 Thread Tor Erik Sønvisen
Hi

I create a canvas that is to big for the default window-size, so it gets cut 
to fit...
How can I increase the window-size to make sure the canvas fits?

regards tores 


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


Socket options

2005-09-12 Thread Tor Erik Sønvisen
Hi

For an online game I'm developing I need some advice concerning tcp-sockets, 
and especially which socket options to set and not.
What I want is a connection where nothing is buffered (but are sent 
immediatly), and I also want to keep the connections persistent until 
explicitly closed.
The server-side socket will receive multiple incomming requests from 
clients...

regards tores 


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


using variable-value

2005-09-21 Thread Tor Erik Sønvisen
Hi

In php I can assign a value to a variable and use this varaible to access a 
property in some object:

$var = 'property';
$object->{$var}

This will transelate to $object->property...
Is this possible in Python?

# Prints help on methods in Canvas-instance
for method in dir(self.canvas):
print method
print help(self.canvas.method)

gives me " AttributeError: Canvas instance has no attribute 'method' "...

regards tores 


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


kill a process in XP

2005-02-23 Thread Tor Erik Sønvisen
Hi.

>From my Python-program I spawn a new process. When using P_NOWAIT spawnl 
returns the pid but in windows it returns a process handle.
Later I want to kill this process. How can I do this when I only have the 
process handle?

-tores- 


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


Delete first line from file

2005-03-01 Thread Tor Erik Sønvisen
Hi

How can I read the first line of a file and then delete this line, so that 
line 2 is line 1 on next read?

regards 


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


select random entry from dictionary

2005-03-07 Thread Tor Erik Sønvisen
Hi

How can I select a random entry from a dictionary, regardless of its 
key-values?

regards tores 


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


EOF-file missing

2005-04-14 Thread Tor Erik Sønvisen
Hi

>From a client I read a file into a string using read().
On the server-side (it's a HTTPServer) i access the same string through the 
input stream rfile.
However all useful read-methods (readlines, readline, read) expect an EOF 
before terminating.
And for some reason the stream doesn't have the seek method either, so it's 
impossible to set the file-pointer to the end of the stream.
how can I access the whole string then?
Is it possible to add an EOF to the string before sending it from the 
client? Or maybe there are other solutions?

regards 


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


logging to two files

2005-04-20 Thread Tor Erik Sønvisen
Hi

Have the following code:
import logging

logging.basicConfig(level = logging.DEBUG,
format = '[%(levelname)-8s %(asctime)s] 
%(message)s',
filename = 'rfs.log',
filemode = 'w')

When using logging.(debug, info etc) stuff is logged to rfs.log.
How may I specify another log with different charateristics, such as a 
different file

regards 


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


Oracle database export

2006-10-05 Thread Tor Erik Soenvisen
Hi,

I need to export an Oracle database to a DDL-formated file. On the Web, I 
found a Python script that did exactly this for a MS Access database, but
not one for Oracle databases.

Does anyone know of such a tool or Python script.

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


Assigning different Exception message

2006-10-12 Thread Tor Erik Soenvisen
try:
self.cursor.execute(sql)
except AttributeError, e:
if e.message == "oracleDB instance has no attribute 'cursor'":
e.message = 'oracleDB.open() must be called before' + \
' oracleDB.query()'
raise AttributeError, e

This code does not re-assign e's message when the conditional is satisfied. 
Why not?

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


FTP over SSL

2006-10-20 Thread Tor Erik Soenvisen
Hi,

Anyone know about existing code supporting FTP over SSL?
I guess pycurl http://pycurl.sourceforge.net/ could be used, but that
requires knowledge of libcurl, which I haven't.

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


Protecting against SQL injection

2006-10-24 Thread Tor Erik Soenvisen
Hi,

How safe is the following code against SQL injection:

# Get user privilege
digest = sha.new(pw).hexdigest()
# Protect against SQL injection by escaping quotes
uname = uname.replace("'", "''")
sql = 'SELECT privilege FROM staff WHERE ' + \
  'username=\'%s\' AND password=\'%s\'' % (uname, digest)
res = self.oraDB.query(sql)

pw is the supplied password abd uname is the supplied password.

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


Code feedback

2006-11-17 Thread Tor Erik Soenvisen
Hi, all I would like some feedback on a multithreaded HTTP server I've 
written. The server serves python-scripts by dynamically loading scripts
in the same directory as itself. When a request is made to one of these
scripts the script is executed and its output is returned to the 
requester.

Here is the server code, HTTPServer.py:

# Basic, threaded HTTP server, serving requests via python scripts
# Author: Tor Erik Soenvisen

# Std lib imports
import BaseHTTPServer, os, threading, Queue

class HTTPServer(BaseHTTPServer.HTTPServer):
""" A threaded HTTP server """

port = 80
# Maximum requests on hold
request_queue_size = 250
# Maximum requests serviced concurrently
workers = 20

def __init__(self, port=None, reqSize=None, workers=None):

if port is not None: self.port = port
if reqSize is not None: self.request_queue_size = reqSize
if workers is not None: self.workers = workers

self.sharedPath = os.path.dirname(os.path.abspath(__file__))
# self.sharedPath must be set before calling self.getHandlers
self.handler = self.getHandlers()
self.jobQueue = Queue.Queue(self.request_queue_size)
addr = ('', self.port)
BaseHTTPServer.HTTPServer.__init__(self, addr, 
HTTPRequestHandler)

# Exit threads when application exits
self.daemon_threads = True
 
self.createThreadPool()
self.serve_forever()

def getHandlers(self):
""" Imports all python scripts in the current directory except 
this one.
These scripts are the response generators corresponding to 
all valid
path requests. The idea is to comprise something similar to 
a
lightweight CGI, where each script generate HTTP responses
to HTTP requests
"""
import inspect
# 1. List all files in current directory
# 2. Skip files not having a .py extension, in addition to this 
file
# 3. Slice of .py extension
handler = dict.fromkeys([x[:-3] for x in os.listdir
(self.sharedPath) \
 if x.endswith('.py') and \
 x != os.path.basename(__file__)])
for name in handler:
handler[name] = __import__(name)
# Make sure the handler contains a run function accepting at 
least
# one parameter
if not hasattr(handler[name], 'run') or \
   len(inspect.getargspec(handler[name].run)[0]) != 2:
print 'Handler %s.py dropped because it lacks ' % name + 
\
  'a function named run accepting two arguments'
del handler[name]

return handler

def createThreadPool(self):
""" Creates pool of worker threads, servicing requests """
self.tpool = []
for i in range(self.workers):
self.tpool.append(threading.Thread
(target=self.process_request_thread,
   args=()))
if self.daemon_threads:
self.tpool[-1].setDaemon(1)
self.tpool[-1].start()

def serve_forever(self):
""" Handle requests "forever" """
while 1:
self.handle_request()

def process_request(self, request, client_address):
""" Task source: dispath requests to job queue """
self.jobQueue.put((request, client_address))

def process_request_thread(self):
""" Task sink: process requests from job queue """
while 1:
request, client_address = self.jobQueue.get()
try:
self.finish_request(request, client_address)
self.close_request(request)
except:
self.handle_error(request, client_address)
self.close_request(request)

class HTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
""" Handles HTTP requests. GET is the only method currently 
supported """

# Set HTTP protocol version 1.1
protocol_version = 'HTTP/1.1'
# Set default response headers
responseHeaders = {'Content-Type': 'text/html',
   'Content-Length': '',
   'Connection': 'close'}

def log_message(self, format, *args):
""" Log function: see BaseHTTPServer.py for info on arguments 
"""
pass

def do_GET(self):
""" Handles HTTP GET requests """

name = self.path.lstrip('/')
try:
handler = self.server.handler[name].ru

len(var) is [CONSTANT] equal to len(var) == [CONSTANT]?

2006-11-23 Thread Tor Erik Soenvisen
Hi,


(len(['']) is 1) == (len(['']) == 1) => True

Is this the case for all numbers? I've tried running the following:

for i in range(1):
for j in range(1):
if i != j:
assert id(i) != id(j), 'i=%d, j=%d, id=%d' % (i, j, id
(i))

which executes fine. Hence, 0- is okey... But this is a relatively 
small range, and sooner or later you probably get two numbers with the same 
id... Thoughts anyone?

Regards Tor Erik

PS: For those of you who don't know: keyword is compares object identities
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: len(var) is [CONSTANT] equal to len(var) == [CONSTANT]?

2006-11-23 Thread Tor Erik Soenvisen
Steven D'Aprano <[EMAIL PROTECTED]> wrote in
news:[EMAIL PROTECTED]: 

> On Thu, 23 Nov 2006 10:48:32 +, Tor Erik Soenvisen wrote:
> 
>> Hi,
>> 
>> 
>> (len(['']) is 1) == (len(['']) == 1) => True
> 
> You shouldn't rely on this behaviour:
> 
>>>> x = 10
>>>> len('a' * x) == x
> True
>>>> len('a' * x) is x
> False
> 
> (Your results may vary -- this depends on the implementation.)
> 
> 
> 
>> Is this the case for all numbers? I've tried running the following:
>> 
>> for i in range(1):
>>  for j in range(1):
>>   if i != j:
>>assert id(i) != id(j), 'i=%d, j=%d, id=%d' % (i, j, id
>> (i))
>> 
>> which executes fine. Hence, 0- is okey... 
> 
> This doesn't necessarily hold for all integers -- again, it depends on
> the implementation, the precise version of Python, and other factors.
> Don't rely on "is" giving the same results as "==".
> 
>>>> (1+2+3+4+5)**7 == 15**7
> True
>>>> (1+2+3+4+5)**7 is 15**7
> False
> 
>> But this is a relatively 
>> small range, and sooner or later you probably get two numbers with
>> the same id... Thoughts anyone?
> 
> No, you will never get two objects existing at the same time with the
> same id. You will get two objects that exist at different times with
> the same id, since ids may be reused when the object is deleted.
> 
>> PS: For those of you who don't know: keyword is compares object
>> identities 
> 
> Exactly. There is no guarantee that any specific integer object "1"
> must be the same object as another integer object "1". It may be, but
> it isn't guaranteed.
> 
> I think the only object that is guaranteed to hold for is None. None
> is a singleton, so there is only ever one instance. Hence, you should
> test for None with "obj is None" rather than ==, because some custom
> classes may do silly things with __eq__:
> 
> class Blank(object):
> """Compares equal to anything false, including None."""
> def __eq__(self, other):
> return not other
> 
> 
I've seen code like this:

if type([]) is list:
print 'Is list'

which seem to work. And also I've seen "var is None", as you mention.
-- 
http://mail.python.org/mailman/listinfo/python-list


The reverse of encode('...', 'backslashreplace')

2007-09-03 Thread Tor Erik Sønvisen
Hi,

How can I transform b so that the assertion holds? I.e., how can I
reverse the backslash-replaced encoding, while retaining the str-type?

>>> a = u'æ'
>>> b = a.encode('ascii', 'backslashreplace')
>>> b
'\\xe6'
>>> assert isinstance(b, str) and b == 'æ'

Traceback (most recent call last):
  File "", line 1, in 
assert isinstance(b, str) and b == 'æ'
AssertionError

Regards,
tores
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Just bought Python in a Nutshell

2007-09-15 Thread Tor Erik Sønvisen
Python in a nutshell also comes in a second edition:
http://www.oreilly.com/catalog/pythonian2/index.html. Here, many of
the new features in Python 2.5 are included. I haven't read through
the first the edition, but I can honestly say that reading through the
second edition has made me a better programmer, not just a better
Python programmer. I only wish I'd read through it earlier, which
would have saved me a lot of agony:)

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


Tapping into the access of an int instance

2007-09-20 Thread Tor Erik Sønvisen
Hi,

Does anyone know how to interrupt the lookup of an integer value? I
know I need to subclass int, since builtin types can't be altered
directly...

Below is how far I've come... What I want is to tap into the access of
instance i's value 1...

>>> class Int(int):
def __init__(self, *a, **k):
super(Int, self).__init__(self, *a, **k)


>>> i = Int(1)
>>> i
1

Regards,
Tor Erik
-- 
http://mail.python.org/mailman/listinfo/python-list


Convert obejct string repr to actual object

2007-10-08 Thread Tor Erik Sønvisen
Hi,

I've tried locating some code that can recreate an object from it's
string representation...
The object in question is really a dictionary containing other
dictionaries, lists, unicode strings, floats, ints, None, and
booleans.

I don't want to use eval, since I can't trust the source sending the
object I'm sure someone must have had the same need and created
code for it... Maybe Pypy has what I need??? Haven't looked though...

Regards,
Tor Erik

PS: The string repr is created by a server outside of my control...
-- 
http://mail.python.org/mailman/listinfo/python-list


str and __setitem__

2007-01-25 Thread Tor Erik Soenvisen
Hi,

What do I need to do to make the code below work as expected:

class str2(str):

def __setitem__(self, i, y):
assert type(y) is str
assert type(i) is int
assert i < len(self)

self = self[:i] + y + self[1+i:]


a = str2('123')
a[1] = '1'
print a
123


The print statement should return 113

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


python shelve on win vs unix

2007-02-09 Thread Tor Erik Soenvisen
Hi,

Have a problem using shelve on windows and unix. While my windows box 
supports dbhash, my unix box supports gdbm, and neither supports what the 
other does. The problem arises when I try to use the shelve generated in 
unix on my windows box.

Hoepfully there are alternatives to installing the berkeley db (needed by 
dbhash) on unix, which I'm looking at now. I'm no unix guru and would 
probably use "forever" to get it up and running.

Any suggestions for a quick and dirty solution (such as alternatives to 
shelve for persistent storage) to this problem would be appreciated.

regards, Tor Erik
-- 
http://mail.python.org/mailman/listinfo/python-list


Simple eval

2007-11-18 Thread Tor Erik Sønvisen
Hi,

A while ago I asked a question on the list about a simple eval
function, capable of eval'ing simple python constructs (tuples, dicts,
lists, strings, numbers etc) in a secure manner:
http://groups.google.com/group/comp.lang.python/browse_thread/thread/58a01273441d445f/
>From the answers I got I chose to use simplejson... However, I was
also pointed to a simple eval function by Fredrik Lundh:
http://effbot.org/zone/simple-iterator-parser.htm. His solution, using
module tokenize, was short and elegant. So I used his code as a
starting point for simple evaluation of dicts, tuples, lists, strings,
unicode strings, integers, floats, None, True, and False. I've
included the code below, together with some basic tests, and
profiling... On my computer (winXP, python 2.5), simple eval is about
5 times slower than builtin eval...

Comments, speedups, improvements in general, etc are appreciated. As
this is a contribution to the community I suggest that any
improvements are posted in this thread...

-Tor Erik

Code (tested on 2.5, but should work for versions >= 2.3):

'''
Recursive evaluation of:
tuples, lists, dicts, strings, unicode strings, ints, floats,
True, False, and None
'''

import cStringIO, tokenize, itertools

KEYWORDS = {'None': None, 'False': False, 'True': True}

def atom(next, token):
if token[1] == '(':
out = []
token = next()
while token[1] != ')':
out.append(atom(next, token))
token = next()
if token[1] == ',':
token = next()
return tuple(out)
elif token[1] == '[':
out = []
token = next()
while token[1] != ']':
out.append(atom(next, token))
token = next()
if token[1] == ',':
token = next()
return out
elif token[1] == '{':
out = {}
token = next()
while token[1] != '}':
key = atom(next, token)
next() # Skip key-value delimiter
token = next()
out[key] = atom(next, token)
token = next()
if token[1] == ',':
token = next()
return out
elif token[1].startswith('u'):
return token[1][2:-1].decode('unicode-escape')
elif token[0] is tokenize.STRING:
return token[1][1:-1].decode('string-escape')
elif token[0] is tokenize.NUMBER:
try:
return int(token[1], 0)
except ValueError:
return float(token[1])
elif token[1] in KEYWORDS:
return KEYWORDS[token[1]]
raise SyntaxError('malformed expression (%r)¨' % token[1])

def simple_eval(source):
src = cStringIO.StringIO(source).readline
src = tokenize.generate_tokens(src)
src = itertools.ifilter(lambda x: x[0] is not tokenize.NL, src)
res = atom(src.next, src.next())
if src.next()[0] is not tokenize.ENDMARKER:
raise SyntaxError("bogus data after expression")
return res


if __name__ == '__main__':
expr = (1, 2.3, u'h\xf8h\n', 'h\xc3\xa6', ['a', 1],
{'list': [], 'tuple': (), 'dict': {}}, False, True, None)
rexpr = repr(expr)

a = simple_eval(rexpr)
b = eval(rexpr)
assert a == b

import timeit
print timeit.Timer('eval(rexpr)', 'from __main__ import
rexpr').repeat(number=1000)
print timeit.Timer('simple_eval(rexpr)', 'from __main__ import
rexpr, simple_eval').repeat(number=1000)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: "do" as a keyword

2007-12-11 Thread Tor Erik Sønvisen
> I also started to ponder about the "'do
> something' if 'true' else 'do this'", and pondered if perhaps
> this statement could do with the including of the keyword do.

Python has support for this in versions >= 2.5:

>>> a = range(0, 5)
>>> b = range(5, 8)
>>> min(a) if sum(a) < sum(b) else min(b)
0

In prior versions, you can use the and/or trick:

>>> (sum(a) < sum(b) and [min(a)] or [min(b)])[0]
0

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


Bluetooth

2005-09-23 Thread Tor Erik S�nvisen
Hi

I'm making a server-side solution in Python and need to be able to 
communicate through bluetooth. Is there any bluetooth-packages out there for 
python?

regards tores


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


Re: Bluetooth

2005-09-23 Thread Tor Erik S�nvisen
Okei

I've been doing some research since my first post and now I'm really 
confused. I'm programming in xp, and found some c++ code out of this world 
that supposidly should list all available services to a given bluetooth 
device. This code however where only working with certain bluetooth-devices 
supported by the Microsoft Bluetooth Stack. If someone out there has any 
advice/ experience on bluetooth-programming on laptops (preferably in 
windows) I would appreciate it. Maybe there is some easy-to-grasp tutorials 
out there using the windows API for bluetooth-programing???

regards tores

"Paul Boddie" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
Tor Erik Sønvisen wrote:
> I'm making a server-side solution in Python and need to be able to
> communicate through bluetooth. Is there any bluetooth-packages out there 
> for
> python?

At the lowest level, you should be able to create sockets for Bluetooth
communications (see the socket module's documentation and the source
code in Modules/socketmodule.c); for example:

from socket import *
s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM)

On Linux, upon binding a device file to the Bluetooth device, you can
use things like pySerial to manage the communications; this works with
software like t616hack:

http://www.nelson.monkey.org/~nelson/weblog/tech/phone/
http://pyserial.sourceforge.net/

Various other tools exist which understand Bluetooth communications,
notably the OpenOBEX tools:

http://triq.net/obex/

I prefer to work with tools at the higher levels (obexftp to access
files, pySerial/t616hack to access messages), but provided your Python
distribution is set up correctly, you should be able to work at the
lowest levels too.

Paul


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

Determine type of a socket

2005-09-26 Thread Tor Erik S�nvisen
Hi

How can I determine the type of a socket (TCP or UDP) object?

regards tores 


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


Where to find python c-sources

2005-09-29 Thread Tor Erik S�nvisen
Hi

I need to browse the socket-module source-code. I believe it's contained in 
the file socketmodule.c, but I can't locate this file... Where should I 
look?

regards tores 


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


Re: Where to find python c-sources

2005-09-30 Thread Tor Erik S�nvisen

"Erik Max Francis" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Tor Erik Sønvisen wrote:
>
>> I need to browse the socket-module source-code. I believe it's contained 
>> in the file socketmodule.c, but I can't locate this file... Where should 
>> I look?
>
> The source tarball, available on python.org.  Are people really too lazy 
> to do elementary research on Google?
>
> -- 
> Erik Max Francis && [EMAIL PROTECTED] && http://www.alcyone.com/max/
> San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
>   The people are to be taken in very small doses.
>   -- Ralph Waldo Emerson

Thanks for the answers... And yes, I have searched google! 


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

output events on select

2005-09-30 Thread Tor Erik S�nvisen
Hi

When using select, what exactly will trigger an output-event?
I have a socket registered in the output-list of the select but an 
output-event is never generated for that socket. I know the socket is ready 
to send data, so how can I make select return with an output-event?

regards tores



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


Most efficient way of storing 1024*1024 bits

2005-11-02 Thread Tor Erik S�nvisen
Hi

I need a time and space efficient way of storing up to 6 million bits. Time 
efficency is more important then space efficency as I'm going to do searches 
through the bit-set.

regards tores 


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


Convert from unicode to int

2005-09-22 Thread Tor Erik S�nvisen
Hi

Is there any simpler way to convert a unicode numeric to an int than:

int(u'1024'.encode('ascii'))

??

regards tores 


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