Re: Non-blocking connect

2008-05-02 Thread mp
Thanks Roy. I was just trying to understand someone else's code, but
in the end it turns out that this was just a bug.

What weirded me out was how injecting a print statement preventing the
error from occurring, but now I get it. Without blocking, the
connection handshake occurs in parallel after the connect_exc method
is called. In my example, my processor reaches the send call before
the connection manages to complete in the background. However, if you
stick in a print statement after the connect_exc call and before the
send call, it delays processing just long enough for the connection to
complete, thus no exception is thrown by the send call.
--
http://mail.python.org/mailman/listinfo/python-list


Non-blocking connect

2008-05-02 Thread mp
Code is at bottom. Basically, if I turn off socket blocking prior to
connecting, I get a "Socket is not connected" error when I try to send
data. However, if I do not turn off blocking, OR if I place a print
statement anywhere before the send call, it works! WTF?

I'd like to understand what's going on in the background here, if you
know don't skimp on the details.

Thanks

---
import
socket
sock = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
sock.setblocking(0)
sock.connect_ex(('localhost',
9000))
sock.setblocking(1)
sock.send('foo')
sock.close()
--
http://mail.python.org/mailman/listinfo/python-list


Re: Can you determine the sign of the polar form of a complex number?

2007-10-17 Thread schaefer . mp
Just to clarify what I'm after:

If you plot (-3)^n where n is a set of negative real numbers between 0
and -20 for example, then you get a discontinuos line due to the
problem mentioned above with fractional exponents. However, you can
compute what the correct absolute value of the the missing points
should be (see z2 above for an example), but I would like to know how
to determine what the correct sign of z2 should be so that it fits the
graph.



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


Can you determine the sign of the polar form of a complex number?

2007-10-17 Thread schaefer . mp
To compute the absolute value of a negative base raised to a
fractional exponent such as:

z = (-3)^4.5

you can compute the real and imaginary parts and then convert to the
polar form to get the correct value:

real_part = ( 3^-4.5 ) * cos( -4.5 * pi )
imag_part = ( 3^-4.5 ) * sin( -4.5 * pi )

|z| = sqrt( real_part^2 + imag_part^2 )

Is there any way to determine the correct sign of z, or perform this
calculation in another way that allows you to get the correct value of
z expressed without imaginary parts?

For example, I can compute:

z1 = (-3)^-4 = 0,012345679
and
z3 = (-3)^-5 = -0,004115226

and I can get what the correct absolute value of z2 should be by
computing the real and imaginary parts:

|z2| = (-3)^-4.5 = sqrt( 3,92967E-18^2 + -0,007127781^2 ) =
0,007127781

but I need to know the sign.

Any help is appreciated.



but I can know the correct sign for this value.

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


Re: negative base raised to fractional exponent

2007-10-17 Thread schaefer . mp
On Oct 17, 4:05 am, Ken Schutte <[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] wrote:
> > Does anyone know of an approximation to raising a negative base to a
> > fractional exponent? For example, (-3)^-4.1 since this cannot be
> > computed without using imaginary numbers. Any help is appreciated.
>
> As others have said, you can use Python's complex numbers (just write -3
> as -3+0j).  If for some reason you don't want to, you can do it all with
> reals using Euler's formula,
>
> (-3)^-4.1  =  (-1)^-4.1  *  3^-4.1
> =
> e^(j*pi*-4.1)  *  3^-4.1
> =
> (cos(pi*-4.1) + j*sin(pi*-4.1)) * 3^-4.1
>
> in Python:
>
>  >>> import math
>  >>> real_part = (3**-4.1) * math.cos(-4.1 * math.pi)
>  >>> imaj_part = (3**-4.1) * math.sin(-4.1 * math.pi)
>  >>> (real_part,imaj_part)
> (0.01026806021211755, -0.0037372276904401318)
>
> Ken

Thank you for this. Now I need to somehow express this as a real
number. For example, I can transform the real and imaginary parts into
a polar coordinate giving me the value I want:

z = sqrt( real_part**2 + imaj_part**2 )

but this is an absolute terms. How does one determine the correct sign
for this value?


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


negative base raised to fractional exponent

2007-10-16 Thread schaefer . mp
Does anyone know of an approximation to raising a negative base to a
fractional exponent? For example, (-3)^-4.1 since this cannot be
computed without using imaginary numbers. Any help is appreciated.

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


Non-Blocking IO

2007-08-31 Thread mp
I'm trying to use popen2 to call a program and then write and read
data from the program using a Python script. Unfortunately, my calls
to read block (I need non-blocking IO), and all the workarounds I've
seen online don't work. Here is my most promising solution and how it
breaks:

Source of solution: 
http://mail.python.org/pipermail/python-dev/2005-March/052263.html


def
setblocking(fd,flag):
" set/clear blocking
mode"
# get the file's current flag
settings
fl = fcntl.fcntl(fd,
fcntl.F_GETFL)
if
flag:
# clear non-blocking mode from
flags
fl = fl &
~os.O_NONBLOCK
 
else:
# set non-blocking mode from
flags
fl = fl |
os.O_NONBLOCK
# update the file's
flags
fcntl.fcntl(fd, fcntl.F_SETFL,
fl)


def
try3():
fin,fout=
os.popen2("echo.py")
 
setblocking(fout.fileno(),False)
 
os.write(fin.fileno(),'blah')
 
fin.flush()
print os.read(fout.fileno(),256)

Calling try3() yields the error:
  File "./test.py", line 54, in try3
print os.read(fout.fileno(),256)
OSError: [Errno 35] Resource temporarily unavailable



If anyone could help me accomplish this I'd be extremely grateful.
Thanks!
MP

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


Re: Character encoding

2006-11-07 Thread mp
I'd prefer a more generalized solution which takes care of all possible
ampersand characters. I assume that there is code already written which
does this.

Thanks

i80and wrote:
> I would suggest using string.replace.  Simply replace ' ' with ' '
> for each time it occurs.  It doesn't take too much code.
>
> On Nov 7, 1:34 pm, "mp" <[EMAIL PROTECTED]> wrote:
> > I have html document titles with characters like >,  , and
> > ‡. How do I decode a string with these values in Python?
> > 
> > Thanks

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


Character encoding

2006-11-07 Thread mp
I have html document titles with characters like >,  , and
‡. How do I decode a string with these values in Python?

Thanks

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


mx.DateTime to datetime.datetime

2006-07-28 Thread mp
Is there a constructor for an mx.DateTime object which takes a
datetime.datetime object? It seems like a pretty common thing to do but
I didn't see such a constructor in the mx.DateTime docs.

Thanks

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


Python framework questions

2006-07-05 Thread mp
Hello, I have a couple general questions.

First, how do most web frameworks serve html? I'm coding in python and
I want to keep all my html seperate from my python stuff. I can serve
these html files from a mysql database or just from the file system, do
people use both these options? Are there other options?

Second, is a cgi-bin directory really necessary? Are there security
issues with configuring Apache to allow cgi-bin execution in other
directories?

Thanks
MP

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


Modify one character in a string

2006-05-24 Thread mp
X-No-Archive
How do I go about modifying one character in a string elegantly?
In other words, I want a function that will change '' to 'aaza',
given the index 2 of the character in the string.

Also, how do I do this when dealing with a file ; which file mode
should I use and what function should I use to modify a single
character once in that file mode?

Thanks
MP

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


symbolic links, aliases, cls clear

2006-03-29 Thread mp
i have a python program which attempts to call 'cls' but fails:

sh: line 1: cls: command not found

i tried creating an alias from cls to clear in .profile, .cshrc, and
/etc/profile, but none of these options seem to work.

my conclusion is that a python program that is executing does not use
the shell (because it does not recognize shell aliases). is this
correct?

should i use a symbolic link? if so, where should i place it?

what is the difference between aliases and symbolic links?

if i execute a command like 'clear' to clear the screen, where does the
shell look to find the command 'clear'?

i'm using os x.

thanks
mp

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