Proxy authentication in common libraries

2008-10-26 Thread Sahasranaman
Hi

I am behind a proxy server that needs proxy authentication. There are
a lot of libraries that  come without proxy support. The function
below, which is part of the python-twitter library does HTTP
Authentication, and I can't figure out how to do this with a
ProxyBasicAuthHandler object. I'm pasting the function here. Can
someone tell me how this code can be rewritten to do proxy
authentication as well as http authentication?

  def _GetOpener(self, url, username=None, password=None):
if username and password:
  self._AddAuthorizationHeader(username, password)
  handler = self._urllib.HTTPBasicAuthHandler()
  (scheme, netloc, path, params, query, fragment) =
urlparse.urlparse(url)
  handler.add_password(Api._API_REALM, netloc, username, password)
  opener = self._urllib.build_opener(handler)
else:
  opener = self._urllib.build_opener()
opener.addheaders = self._request_headers.items()
return opener
--
http://mail.python.org/mailman/listinfo/python-list


Re: Odd math related issue.

2008-07-21 Thread sahasranaman
On Jul 21, 5:30 pm, Fredrik Lundh <[EMAIL PROTECTED]> wrote:
> Alexandru Palade wrote:
> > However, you should be carefully because using an %i modifier for a
> > what-should-be a float value truncates the value in a way you may not
> > expect.
>
> > What I mean is that if you have sent 2 out of 3 bytes, the math will be
> > 200/3 which with the %i modifier will print 66, rather than 66.6 (or at
> > least 67 which is closer - have a look at the round() function).
>
> My suggested workaround doesn't use floats.  As for rounding, that's
> more of a usability issue -- seeing the download process getting stuck
> at 100% can be rather frustrating for the poor user.  Better truncate
> towards zero.
>
> > Another thing, you could have just added a dot after the constant in
> > order to promote the expression to be evaluated as float. As in
> >    percentage = bytes_transferred / /self/.__sessions[path].total_bytes
> > * 100.
> > (notice the last dot)
>
> Did you try that?
>
>  >>> 2 / 3 * 100.
> 0.0
>
> You can fix this with parentheses, but I usually recommend an explicit
> "cast" instead, to make it obvious what you're doing:
>
>      result = float(a) / b
>
> 

Use 2.0 / 3 * 100 to solve this. Why make things look bigger?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Odd math related issue.

2008-07-21 Thread sahasranaman
On Jul 21, 5:30 pm, Fredrik Lundh <[EMAIL PROTECTED]> wrote:
> Alexandru Palade wrote:
> > However, you should be carefully because using an %i modifier for a
> > what-should-be a float value truncates the value in a way you may not
> > expect.
>
> > What I mean is that if you have sent 2 out of 3 bytes, the math will be
> > 200/3 which with the %i modifier will print 66, rather than 66.6 (or at
> > least 67 which is closer - have a look at the round() function).
>
> My suggested workaround doesn't use floats.  As for rounding, that's
> more of a usability issue -- seeing the download process getting stuck
> at 100% can be rather frustrating for the poor user.  Better truncate
> towards zero.
>
> > Another thing, you could have just added a dot after the constant in
> > order to promote the expression to be evaluated as float. As in
> >    percentage = bytes_transferred / /self/.__sessions[path].total_bytes
> > * 100.
> > (notice the last dot)
>
> Did you try that?
>
>  >>> 2 / 3 * 100.
> 0.0
>
> You can fix this with parentheses, but I usually recommend an explicit
> "cast" instead, to make it obvious what you're doing:
>
>      result = float(a) / b
>
> 

try 2.0 / 3 * 100
that would solve all trouble
--
http://mail.python.org/mailman/listinfo/python-list