[issue23041] csv needs more quoting rules

2022-02-23 Thread Samwyse

Samwyse  added the comment:

I just signed the contributor agreement. (Thought I had done that last year but 
I don’t see any emails. Is there any place to check?)

I agree that round-tripping should Bebe possible for any value of quoting.

Hopefully this will finally get done before its eighth birthday.

--

___
Python tracker 
<https://bugs.python.org/issue23041>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45656] argparse bug: prefix_chars argument to add_argument_group doesn't really work

2021-10-28 Thread Samwyse


New submission from Samwyse :

Using the prefix_chars argument to parser.add_argument_group causes usage 
information to print correctly, but the resulting parser doesn't recognize the 
options.  The included program reproduces the issue; it produces the following 
output on my system.

--- python version
3.6.8 (default, Sep 26 2019, 11:57:09)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
--- test using prefix_chars='-/' with ArgumentParser
--- show help info
usage: test-1 [-h] [-foo FOO] [/bar BAR]

optional arguments:
  -h, --help  show this help message and exit
  -foo FOO
  /bar BAR

--- try parsing something
Namespace(bar='b', foo='f')
--- test using prefix_chars='-/' with an argument group
--- show help info
usage: test-2 [-h] [-foo FOO] [/bar BAR]

optional arguments:
  -h, --help  show this help message and exit

other arguments:
  -foo FOO
  /bar BAR

--- try parsing something
usage: test-2 [-h] [-foo FOO] [/bar BAR]
test-2: error: unrecognized arguments: /bar b

--
components: Library (Lib)
files: argparser-bug.py
messages: 405217
nosy: samwyse
priority: normal
severity: normal
status: open
title: argparse bug: prefix_chars argument to add_argument_group doesn't really 
work
type: behavior
versions: Python 3.6
Added file: https://bugs.python.org/file50410/argparser-bug.py

___
Python tracker 
<https://bugs.python.org/issue45656>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44882] add FileInput.rollback() for in-place filters

2021-08-10 Thread Samwyse


Change by Samwyse :


--
title: add .rollback() for in-place filters -> add FileInput.rollback() for 
in-place filters

___
Python tracker 
<https://bugs.python.org/issue44882>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44882] add .rollback() for in-place filters

2021-08-10 Thread Samwyse


New submission from Samwyse :

Sometimes bad things happen when processing an in-place filter, leaving an 
empty or incomplete input file and a backup file that needs to recovered. The 
FileInput class has all the information needed to do this, but it is in private 
instance variables.  A .rollback() method could close the current file and 
rename the backup file to its original name.  For example:

  for line in fileinput.input(inplace=True):
try:
  ...
except SomeError:
  fileinput.rollback(close=False)  # continue with next file

A simplistic implementation could be:

  def rollback(self, close=True):
if self._backupfilename:
  os.rename(self._backupfilename, self.filename)
  self._backupfilename = None
if close:
  self.close()
else:
  self.nextfile()

--
components: Library (Lib)
messages: 399361
nosy: samwyse
priority: normal
severity: normal
status: open
title: add .rollback() for in-place filters
type: enhancement
versions: Python 3.10, Python 3.11, Python 3.9

___
Python tracker 
<https://bugs.python.org/issue44882>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43556] fix attr names for ast.expr and ast.stmt

2021-03-19 Thread Samwyse


New submission from Samwyse :

In Doc/library/ast.rst, the lineno and end_col attributes are repeated; the 
second set should have 'end_' prefixed to them.  Also, there's a minor 
indentation error in the RST file.

# diff ast.rst ast.rst~ 
78c78
<   col_offset
---
> col_offset
83c83
<   :attr:`lineno`, :attr:`col_offset`, :attr:`end_lineno`, and 
:attr:`end_col_offset`
---
>   :attr:`lineno`, :attr:`col_offset`, :attr:`lineno`, and 
> :attr:`col_offset`

--
assignee: docs@python
components: Documentation
messages: 389077
nosy: docs@python, samwyse
priority: normal
severity: normal
status: open
title: fix attr names for ast.expr and ast.stmt
type: enhancement
versions: Python 3.9

___
Python tracker 
<https://bugs.python.org/issue43556>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue33158] Add fileobj property to csv reader and writer objects

2018-03-27 Thread Samwyse

New submission from Samwyse <samw...@gmail.com>:

Many objects have properties that allow access to the arguments used to create 
them.  In particular, file objects have a name property that returns the name 
used when opening a file.  A fileobj property would be convenient, as you 
otherwise, for example, need to pass an extra argument to routines that need 
both the csv object and the underlying file object.  Adopting this enhancement 
would also provide consistency with the dialect constructer argument, which is 
available as an object property.

Changing the fileobj while the csv object is in use would open a can of worms, 
so this should be a read-only property.

Optionally, the fileobj property could be reflected in the DictReader and 
DictWriter classes, but the value would be accessible via the .reader and 
.writer properties of those classes.

--
components: Library (Lib)
messages: 314538
nosy: samwyse
priority: normal
severity: normal
status: open
title: Add fileobj property to csv reader and writer objects
type: enhancement
versions: Python 2.7, Python 3.8

___
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue33158>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue29940] Add follow_wrapped=True option to help()

2017-03-29 Thread Samwyse

New submission from Samwyse:

The help(obj) function uses the type of obj to create its result.  This is less 
than helpful when requesting help on a wrapped object.  Since 3.5, 
inspect.signature() and inspect.from_callable() have a follow_wrapped option to 
get around similar issues.  Adding the option to help() would prevent 
surprising behavior while still allowing current behavior to be used when 
needed.  See http://stackoverflow.com/a/17705456/603136 for more.

--
components: Library (Lib)
messages: 290782
nosy: samwyse
priority: normal
severity: normal
status: open
title: Add follow_wrapped=True option to help()
type: enhancement
versions: Python 2.7, Python 3.3, Python 3.4, Python 3.5, Python 3.6, Python 3.7

___
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue29940>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue26299] wsgiref.util FileWrapper raises ValueError: I/O operation on closed file.

2016-02-05 Thread Samwyse

New submission from Samwyse:

While developing, I am using wsgiref.simple_server.  Using to serve static 
content isn't working.  The attached program demonstrates the issue.  Run it 
and connect to http://127.0.0.1:8000/.  You will see three buttons.  Clicking 
on 'wsgi.filewrapper' causes the FileWrapper class found in wsgiref.util to be 
used, while clicking on 'PEP 0333' uses code suggested in PEP 0333 
(https://www.python.org/dev/peps/pep-0333/#id36).  Both of these fail.  
Clicking on 'slurp' causes the entire file to loaded and returned in a list.  
This works.

When an application returns an instance of environ['wsgi.file_wrapper'], or 
creates it's own iterator, an error is raised during the initial read of the 
file.  Reading the entire file and returning a single-element list (the 'slurp' 
technique) works, but is impractical for larger files.

I've tested this with both Python 2.7.9 and 3.4.3 under Windows 7 (my laptop) 
and 3.4.3 under Alpine Linux (a Docker instance).

--
components: Library (Lib)
files: wsgitest.py
messages: 259685
nosy: samwyse
priority: normal
severity: normal
status: open
title: wsgiref.util FileWrapper raises ValueError: I/O operation on closed file.
versions: Python 2.7, Python 3.4
Added file: http://bugs.python.org/file41828/wsgitest.py

___
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue26299>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue25760] TextWrapper fails to split 'two-and-a-half-hour' correctly

2015-11-28 Thread Samwyse

New submission from Samwyse:

Single character words in a hyphenated phrase are not split correctly.  The 
root issue it the wordsep_re class variable.  To reproduce, run the following:

>>> import textwrap
>>> textwrap.TextWrapper.wordsep_re.split('two-and-a-half-hour')
['', 'two-', 'and-a', '-half-', 'hour']

It works if 'a' is replaces with two or more alphabetic characters.

>>> textwrap.TextWrapper.wordsep_re.split('two-and-aa-half-hour')
['', 'two-', '', 'and-', '', 'aa-', '', 'half-', 'hour']

The problem is in this part of the pattern:  (?=\w+[^0-9\W])

I confess that I don't understand the situation that would require that 
complicated of a pattern.  Why wouldn't (?=\w) would work?

--
components: Library (Lib)
messages: 28
nosy: samwyse
priority: normal
severity: normal
status: open
title: TextWrapper fails to split 'two-and-a-half-hour' correctly
type: behavior
versions: Python 2.7, Python 3.2, Python 3.3, Python 3.4

___
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue25760>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue23041] csv needs more quoting rules

2014-12-15 Thread Samwyse

Samwyse added the comment:

Yes, it's based on a real-world need.  I work for a Fortune 500 company and we 
have an internal tool that exports CSV files using what I've described as the 
QUOTE_NOTNULL rules.  I need to create similar files for re-importation.  Right 
now, I have to post-process the output of my Python program to get it right.  I 
added in the QUOTE_STRINGS rule for completeness.  I think these two new rules 
would be useful for anyone wanting to create sparse CSV files.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue23041
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue23059] sort misc help topics in cmd

2014-12-15 Thread Samwyse

New submission from Samwyse:

I've discovered that do_help method of cmd.Cmd prints the documented and 
undocumented commands in sorted order, but does not sort the miscellaneous 
topics.  This would be an easy fix, just change 
self.print_topics(self.misc_header, help.keys(),15,80) to use 
sorted(help.keys()).

--
components: Library (Lib)
messages: 232680
nosy: samwyse
priority: normal
severity: normal
status: open
title: sort misc help topics in cmd
versions: Python 3.5

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue23059
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue23041] csv needs more quoting rules

2014-12-15 Thread Samwyse

Samwyse added the comment:

Skip, I don't have any visibility into how the Java program I'm feeding data 
into works, I'm just trying to replicate the csv files that it exports as 
accurately as possible.  It has several other quirks, but I can replicate all 
of them using Dialects; this is the only feature I can't.  The files I'm 
looking at have quoted strings and numbers, but there aren't any quoted empty 
strings.  I'm using a DictWriter to create similar csv files, where missing 
keys are treated as values of None, so I'd like those printed without quotes.  
If we also want to print empty strings without quotes, that wouldn't impact me 
at all.

Besides my selfish needs, this could be useful to anyone wanting to reduce the 
save of csv files that have lots of empty fields, but wants to quote all 
non-empty values.  This may be an empty set, I don't know.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue23041
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue23059] cmd module should sort misc help topics

2014-12-15 Thread Samwyse

Changes by Samwyse samw...@gmail.com:


--
title: sort misc help topics in cmd - cmd module should sort misc help topics

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue23059
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue23041] csv needs more quoting rules

2014-12-12 Thread Samwyse

New submission from Samwyse:

The csv module currently implements four quoting rules for dialects: 
QUOTE_MINIMAL, QUOTE_ALL, QUOTE_NONNUMERIC and QUOTE_NONE.  These rules treat 
values of None the same as an empty string, i.e. by outputting two consecutive 
quotes.  I propose the addition of two new rules, QUOTE_NOTNULL and 
QUOTE_STRINGS.  The former behaves like QUOTE_ALL while the later behaves like 
QUOTE_NONNUMERIC, except that in both cases values of None are output as an 
empty field.  Examples follow.


Current behavior (which will remain unchanged)

 csv.register_dialect('quote_all', quoting=csv.QUOTE_ALL)
 csv.writer(sys.stdout, dialect='quote_all').writerow(['foo', None, 42])
foo,,42

 csv.register_dialect('quote_nonnumeric', quoting=csv.QUOTE_NONNUMERIC)
 csv.writer(sys.stdout, dialect='quote_nonnumeric').writerow(['foo', None, 
 42])
foo,,42


Proposed behavior

 csv.register_dialect('quote_notnull', quoting=csv.QUOTE_NOTNULL)
 csv.writer(sys.stdout, dialect='quote_notnull').writerow(['foo', None, 42])
foo,,42

 csv.register_dialect('quote_strings', quoting=csv.QUOTE_STRINGS)
 csv.writer(sys.stdout, dialect='quote_strings').writerow(['foo', None, 42])
foo,,42

--
components: Library (Lib)
messages: 232560
nosy: samwyse
priority: normal
severity: normal
status: open
title: csv needs more quoting rules
type: enhancement
versions: Python 2.7, Python 3.2, Python 3.3, Python 3.4, Python 3.5, Python 3.6

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue23041
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue23041] csv needs more quoting rules

2014-12-12 Thread Samwyse

Samwyse added the comment:

David:  That's not a problem for me.

Sorry I can't provide real patches, but I'm not in a position to compile (much 
less test) the C implementation of _csv.  I've looked at the code online and 
below are the changes that I think need to be made.  My use cases don't require 
special handing when reading empty fields, so the only changes I've made are to 
the code for writers.  I did verify that the reader code mostly only checks for 
QUOTE_NOTNULL when parsing.  This means that completely empty fields will 
continue to load as zero-length strings, not None.  I won't stand in the way of 
anyone wanting to fix that for these new rules.



typedef enum {
QUOTE_MINIMAL, QUOTE_ALL, QUOTE_NONNUMERIC, QUOTE_NONE,
QUOTE_STRINGS, QUOTE_NOTNULL
} QuoteStyle;



static StyleDesc quote_styles[] = {
{ QUOTE_MINIMAL,QUOTE_MINIMAL },
{ QUOTE_ALL,QUOTE_ALL },
{ QUOTE_NONNUMERIC, QUOTE_NONNUMERIC },
{ QUOTE_NONE,   QUOTE_NONE },
{ QUOTE_STRINGS,QUOTE_STRINGS },
{ QUOTE_NOTNULL,QUOTE_NOTNULL },
{ 0 }
};



switch (dialect-quoting) {
case QUOTE_NONNUMERIC:
quoted = !PyNumber_Check(field);
break;
case QUOTE_ALL:
quoted = 1;
break;
case QUOTE_STRINGS:
quoted = PyString_Check(field);
break;
case QUOTE_NOTNULL:
quoted = field != Py_None;
break;
default:
quoted = 0;
break;
}



csv.QUOTE_MINIMAL means only when required, for example, when a\n
field contains either the quotechar or the delimiter\n
csv.QUOTE_ALL means that quotes are always placed around fields.\n
csv.QUOTE_NONNUMERIC means that quotes are always placed around\n
fields which do not parse as integers or floating point\n
numbers.\n
csv.QUOTE_STRINGS means that quotes are always placed around\n
fields which are strings.  Note that the Python value None\n
is not a string.\n
csv.QUOTE_NOTNULL means that quotes are only placed around fields\n
that are not the Python value None.\n

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue23041
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue21413] urllib.request.urlopen dies on non-basic/digest auth schemes

2014-05-01 Thread Samwyse

New submission from Samwyse:

In Python 2.x, this opens an NTLM protected URL:
opener = urllib2.build_opener(proxy_handler, auth_NTLM, auth_digest, 
auth_basic)
urllib2.install_opener(opener)
response = urllib2.urlopen(url)

In Python 3.x, this raises an error:
opener = urllib.request.build_opener(proxy_handler, auth_NTLM, auth_digest, 
auth_basic)
urllib.request.install_opener(opener)
response = urllib.request.urlopen(url)

The error is:
ValueError: AbstractDigestAuthHandler does not support the following scheme: 
'NTLM'

Removing auth_digest from the list of handlers allows the code to work.

--
components: Library (Lib)
messages: 217734
nosy: samwyse
priority: normal
severity: normal
status: open
title: urllib.request.urlopen dies on non-basic/digest auth schemes
versions: Python 3.1, Python 3.2, Python 3.3, Python 3.4, Python 3.5

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue21413
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18191] urllib2/urllib.parse.splitport does not handle IPv6 correctly

2013-07-22 Thread Samwyse

Samwyse added the comment:

Fixes Serhiy Storchaka's complaints, removes duplicate test.

--
nosy: +samwyse
Added file: http://bugs.python.org/file31013/test_urlparse.diff

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18191
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18191] urllib2/urllib.parse.splitport does not handle IPv6 correctly

2013-07-22 Thread Samwyse

Samwyse added the comment:

Handles raw IPv6 URLs

--
Added file: http://bugs.python.org/file31015/urllib.diff

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18191
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue17229] unable to discover preferred HTTPConnection class

2013-02-18 Thread Samwyse

New submission from Samwyse:

When a URL is opened, the opener-director is responsible for locating the 
proper handler for the specified protocol. Frequently, an existing protocol 
handler will be subclassed and then added to the collection maintained by the 
director. When urlopen is called, the specified request is immediately handed 
off to the director's open method which finds the correct handler and invokes 
the protocol-specific XXX_open method. At least in the case of the HTTP 
protocols, if an error occurs then the director is called again to find and 
invoke a handler for the error; these handlers generally open a new connection 
after adding headers to avoid the error going forward. Finally, it is important 
to note that at the present time, the HTTP handlers in urllib2 are built using 
a class (infourl) that isn't prepared to deal with a persistent connection, so 
they always add a Connection: close header to the request.

Unfortunately, NTLM only certifies the current connection, meaning that a 
Connection: keep-alive header must be used to keep it open throughout the 
authentication process. Furthermore, because the opener director only provides 
a do_open method, there is no way to discover the type of connection without 
also opening it. This means that the HTTPNtlmAuthHandler cannot use the normal 
HTTPHandler and must therefore must hardcode the HTTPConnection class. If a 
custom class is required for whatever reason, the only way to cause it to be 
used is to monkey-patch the code. For an example, see 
http://code.google.com/p/python-ntlm/source/browse/trunk/python26/ntlm_examples/test_ntlmauth.py

This can be avoided if, instead of putting the instantiation of the desired 
HTTP connection class inline, the HTTPHandler classes used a class instance 
variable. Something like the following should work without breaking any 
existing code:

class HTTPHandler(AbstractHTTPHandler):

_connection = httplib.HTTPConnection

@property
def connection(self):
Returns the class of connection being handled.
return self._connection

def http_open(self, req):
return self.do_open(_connection, req)

http_request = AbstractHTTPHandler.do_request_

--
components: Library (Lib)
messages: 182343
nosy: samwyse
priority: normal
severity: normal
status: open
title: unable to discover preferred HTTPConnection class
versions: 3rd party, Python 2.6, Python 2.7, Python 3.1, Python 3.2, Python 
3.3, Python 3.4, Python 3.5

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue17229
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue15350] {urllib,urllib.parse}.urlencode.__doc__ is unclear

2012-10-12 Thread samwyse

samwyse added the comment:

Look good.  I'd fix the last line, however:  sent the quote_plus -
sent to the quote_plus function, maybe.

On Fri, Sep 28, 2012 at 6:18 AM, Brian Brazil rep...@bugs.python.org wrote:

 Brian Brazil added the comment:

 How does the attached patch look?

 I also reworded the first line to be a bit clearer, and be under 80 chars.

 --
 keywords: +patch
 nosy: +bbrazil
 Added file: http://bugs.python.org/file27329/issue15350.patch

 ___
 Python tracker rep...@bugs.python.org
 http://bugs.python.org/issue15350
 ___

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue15350
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue16122] Allow *open* to accept file-like objects

2012-10-03 Thread samwyse

New submission from samwyse:

I'm once again frustrated by a third-party module that only accepts filenames, 
not already-opened file-like objects.  This prevents me from passing in 
StringIO objects or any of the standard file streams.  Currently, *open()* 
function accepts strings or (in Python 3.X) integers.  I propose that *open()* 
accept file-like objects, either returning them unchanged, or by returning a 
wrapper object.  While there are many different types of file-like objects, 
they all have one characteristic in common: the presence of a .close() method.

A non-wrapped version of open() could be as simple as this:

  try:
file = original_open(name, mode, buffering)
  except TypeError:
if hasattr(name, 'close'):
return name
raise

Returning a wrapper object would be slightly more complicated, but would allow 
the wrapped object to remain open even after the wrapper is closed.

--
components: IO
messages: 171908
nosy: samwyse
priority: normal
severity: normal
status: open
title: Allow *open* to accept file-like objects
type: enhancement
versions: Python 2.7, Python 3.5

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue16122
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue13866] {urllib, urllib.parse}.urlencode should not use quote_plus

2012-07-14 Thread samwyse

samwyse samw...@gmail.com added the comment:

Since no one else seems willing to do it, here's a patch that adds a 
'quote_via' keyword parameter to the urlencode function.

 import urllib.parse
 query={foo: + }
 urllib.parse.urlencode(query)
'foo=%2B+'
 urllib.parse.urlencode(query, quote_via=urllib.parse.quote)
'foo=%2B%20'

--
keywords: +patch
Added file: http://bugs.python.org/file26378/urllib_parse.diff

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue13866
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue15350] {urllib,urllib.parse}.urlencode.__doc__ is unclear

2012-07-14 Thread samwyse

New submission from samwyse samw...@gmail.com:

The doc string for url encode states:

The query arg may be either a string or a bytes type. When query arg is a
string, the safe, encoding and error parameters are sent to the quote_via
function for encoding

IMHO, this implies that the argument can be a string.  Note that the preceding 
paragraphs starts out with If the query arg is a sequence of two-element 
tuples. I think that it should read:

The components of the query arg may be either a string or a bytes
type. When query arg is a string, the safe, encoding and error
parameters are sent to the quote_via function for encoding.

--
components: Library (Lib)
messages: 165440
nosy: samwyse
priority: normal
severity: normal
status: open
title: {urllib,urllib.parse}.urlencode.__doc__ is unclear
type: behavior
versions: Python 2.6, Python 2.7, Python 3.1, Python 3.2, Python 3.3, Python 3.4

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue15350
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue15327] Argparse: main arguments and subparser arguments indistinguishable

2012-07-14 Thread samwyse

samwyse samw...@gmail.com added the comment:

The Namespace object contains a combined list of all of the arguments, from 
both the main parser and the subparser.  I don't see any way to resolve 
identically name augments without breaking a lot of code that relies on the 
current behavior.  I would instead propose that anyone needing to distinguish 
between identically named args in nested parsers use distinct names as the 
primary name of the argument, and the non-distictive name as an alias.  For 
example:
parser.add_argument('--verbose(main)', '--verbose', ...)

Running the attached file produces the following output.

# using non-distinctive args
['--verbose', 'command'] = Namespace(command='command', verbose=True)
['command', '--verbose'] = Namespace(command='command', verbose=True)
['--verbose', 'command', '--verbose'] = Namespace(command='command', 
verbose=True)

# using distinctive args
['--verbose', 'command'] = Namespace(command='command', verbose(main)=True, 
verbose(subcommand)=False)
['command', '--verbose'] = Namespace(command='command', verbose(main)=False, 
verbose(subcommand)=True)
['--verbose', 'command', '--verbose'] = Namespace(command='command', 
verbose(main)=True, verbose(subcommand)=True)

--
nosy: +samwyse
Added file: http://bugs.python.org/file26379/argparsetest.py

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue15327
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue15327] Argparse: main arguments and subparser arguments indistinguishable

2012-07-14 Thread samwyse

samwyse samw...@gmail.com added the comment:

One change and one minor problem with my suggestion.

First, you don't need the second alias, since it's a prefix of the argument 
name.  Also, HelpFormatter._format_actions_usage ends with a bit of code 
labeled clean up separators for mutually exclusive groups; it removes matched 
parentheses from the argument names.  :(  I'd recommend that you instead use 
one of the following (all of which I've tested):

parser.add_argument('--verbose[main]', ...)
parser.add_argument('--verbose{main}', ...)
parser.add_argument('--verbosemain', ...)

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue15327
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue13866] {urllib, urllib.parse}.urlencode should not use quote_plus

2012-07-13 Thread samwyse

Changes by samwyse samw...@gmail.com:


--
nosy: +samwyse

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue13866
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue15289] Adding __getitem__ as a class method doesn't work as expected

2012-07-10 Thread samwyse

samwyse samw...@gmail.com added the comment:

Thanks, Eric.  Based on what you said, I was able to get the desired behavior 
by creating a metaclass.

--
Added file: http://bugs.python.org/file26343/Issue15289.py

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue15289
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue15289] Adding __getitem__ as a class method doesn't work as expected

2012-07-07 Thread samwyse

New submission from samwyse samw...@gmail.com:

I'm using a class as a decorator, and saving information in a class variable.  
I wanted to access the information via a __getitem__ class method, but using 
conventional syntax doesn't work on a class.  The following exception is thrown 
when the attached script is run.

Traceback (most recent call last):
  File /Users/sam/Documents/forth.py, line 34, in module
print Alias[f']
TypeError: 'type' object has no attribute '__getitem__'

--
components: None
files: 20120607.py
messages: 164926
nosy: samwyse
priority: normal
severity: normal
status: open
title: Adding __getitem__ as a class method doesn't work as expected
type: behavior
versions: Python 2.7
Added file: http://bugs.python.org/file26309/20120607.py

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue15289
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue6321] Reload Python modules when running programs

2011-12-20 Thread samwyse

samwyse samw...@gmail.com added the comment:

[issue5847] fixed in 2.7/3.1

--
resolution:  - out of date
status: open - closed

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue6321
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



2.X functools.update_wrapper dislikes missing function attributes

2011-04-26 Thread samwyse
I noticed a behavior in Jython 2.5.2 that's arguably an implementation
bug, but I'm wondering if it's something to be fixed for all versions
of Python.  I was wanting to decorate a Java instance method, and
discovered that it didn't have a __module__ attribute.  This caused
the following message:

  File C:\jython2.5.2\Lib\functools.py, line 33, in update_wrapper
setattr(wrapper, attr, getattr(wrapped, attr))
AttributeError: 'instancemethod' object has no attribute '__module__'

The relevant code is:
for attr in assigned:
setattr(wrapper, attr, getattr(wrapped, attr))
for attr in updated:
getattr(wrapper, attr).update(getattr(wrapped, attr, {}))

Note that attributes to be updated get a default value.  I'm proposing
that attributes to be assigned do the same, most likely an empty
string.  A non-string value (such as None) could break anything
expecting a string value, so it seems like a bad idea.  Python 3.2
catches AttributeError and passes.  I don't like this solution.  While
it prevents any attributes from being added to the wrapper, the
wrapper likely has its own values (at least for the default
attributes) and using those values could cause confusion.

Any opinions?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: 2.X functools.update_wrapper dislikes missing function attributes

2011-04-26 Thread samwyse
I just noticed an old issue that relate to this: 
http://bugs.python.org/issue3445

This dates back to 2008 and is marked as fixed, but my copies of
Python 2.5.4 and 2.7.1 don't seem to implement it.  I'll try to dig
further.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Proposed changes to logging defaults

2010-12-14 Thread samwyse
On Dec 9, 6:12 pm, Vinay Sajip vinay_sa...@yahoo.co.uk wrote:
 Some changes are being proposed to how logging works in default
 configurations.

 Briefly - when a logging event occurs which needs to be output to some
 log, the behaviour of the logging package when no explicit logging
 configuration is provided will change, most likely to log those events
 to sys.stderr with a default format.

I'm in favor of this change.  I've long wished that I could just add
lots of warning/error/info logging to a script and have it just work
without having to spend time configuring the logging system.
-- 
http://mail.python.org/mailman/listinfo/python-list


optparse/argparse for cgi/wsgi?

2010-12-10 Thread samwyse
Has anyone ever built some sort of optparse/argparse module for cgi/
wsgi programs?  I can see why a straight port wouldn't work, but a
module that can organize parameter handling for web pages seems like a
good idea, especially if it provided a standard collection of both
client- and server-side validation processes, easy
internationalization, and a way to create customizable help pages.
-- 
http://mail.python.org/mailman/listinfo/python-list


[issue10178] PEP 378 uses replace where translate may work better

2010-10-23 Thread samwyse

New submission from samwyse samw...@gmail.com:

PEP 378 states;

  format(n, 6,f).replace(,, X).replace(., ,).replace(X, .)

This is complex and relatively slow.  A better technique, which IMHO the 
proposal should high-lighted, would be:

  swap_commas_and_periods = bytes.maketrans(b',.', b'.,')
  format(n, 6,f).translate(swap_commas_and_periods)

While performing the maketrans each time a string is formatted is slower than 
the triple replace, calling it once and caching the result is faster.  I have 
tested with with the 3.1 interpreter; example timings follow.

 Timer(
  '1,234,567.89'.replace(',', 'X').replace('.', ',').replace('X', '.')
).timeit()
3.0645400462908015

 Timer(
  '1,234,567.89'.translate(swap_commas_and_periods)
, 
  swap_commas_and_periods = bytes.maketrans(b',.', b'.,')
).timeit()
2.276630409730846


 Timer(
  '1,234,567.89'.translate(bytes.maketrans(b',.', b'.,'))
).timeit()
3.760715677551161

--
assignee: d...@python
components: Documentation
messages: 119427
nosy: d...@python, samwyse
priority: normal
severity: normal
status: open
title: PEP 378 uses replace where translate may work better
type: behavior
versions: Python 2.7, Python 3.1

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue10178
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue10178] PEP 378 uses replace where translate may work better

2010-10-23 Thread samwyse

samwyse samw...@gmail.com added the comment:

The text in question is also talking about the problems with using 'replace' to 
swap pairs of characters, so a better, alternate, process would be valuable, 
especially for anyone unaware of the translate method.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue10178
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue6321] Reload Python modules when running programs

2010-08-15 Thread samwyse

samwyse samw...@gmail.com added the comment:

As it happens, I do use Windows and almost exclusively start IDLE via 
right-clicks on .py files. I've never seen the behavior you describe documented 
anywhere.

On Aug 15, 2010, at 1:37 PM, Cherniavsky Beni rep...@bugs.python.org wrote:

 
 Cherniavsky Beni c...@users.sf.net added the comment:
 
 When you run a program using F5 in IDLE,
 it completely restarts the underlying interpreter!
 If you meant a different way of running, please elaborate.
 
 (Exception: it uses the same interpreter if you're running idle -n; this 
 commonly happens on Windows if you rightclick-Edit with IDLE... a .py file - 
 just don't use that.)
 
 --
 nosy: +cben
 
 ___
 Python tracker rep...@bugs.python.org
 http://bugs.python.org/issue6321
 ___

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue6321
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



Re: simple (I hope!) problem

2010-08-05 Thread samwyse
On Aug 5, 4:32 am, Jean-Michel Pichavant jeanmic...@sequans.com
wrote:
 samwyse wrote:
  On Aug 3, 1:20 am, Steven D'Aprano steve-REMOVE-
  t...@cybersource.com.au wrote:

  On Mon, 02 Aug 2010 17:19:46 -0700, samwyse wrote:

  Fortunately, I don't need the functionality of the object, I just want
  something that won't generate an error when I use it.  So, what is the
  quickest way to to create such an object (replacing the 'pass' in my
  first snippet).  My solution is this:

      class C:
          def filter(self, *args, **kwds):
              pass
      register = C()

  but it seems like I should be able to do something better, as measured
  by lines of code, faking more than just a 'filter' method, or both.  Any
  ideas?  Thanks!

  You want a variation on the Null Object design pattern.

  class NullWithMethods(object):
      def __getattr__(self, name):
          return self
      def __call__(self, *args, **kwargs):
          pass

  And in action:

  c = NullWithMethods()
  c.spam(hello, world)
  c.something_completely_unlikely.spam.ham(hello, world, foo=42)

  --
  Steven

  JM emailed me a good solution, but yours is great! Thanks!

 The version I gave you overrides __getattribute__. To be honest,
 overriding __getattr__ is a better solution.Just in case you don't know
 the difference, __getattr__ is called only if the attribute is not found
 while __getattribute__ is actually called to find the attribute.

 JM

I have to apologize for not replying as soon as I got your email.  It
did everything I needed, so I implemented it in my code and went to
town.  Then, when I did finally return to the c.l.py, there was an
solution that exceeded my needs by letting me chain together arbitrary
lists of attributes.  Now that I've slept on it, I've come up with a
solution that I like even more:

 class Placeholder(object):
def __getattr__(self, name):
return self
def __getitem__(self, index):
return self
def __call__(self, *args, **kwargs):
return self

 x = Placeholder()
 x('hello, world').y[42].z
__main__.Placeholder object at 0x01E46490

Yes, running it from the prompt looks ugly, but within a program the
return value is silently discarded.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: simple (I hope!) problem

2010-08-04 Thread samwyse
On Aug 3, 1:20 am, Steven D'Aprano steve-REMOVE-
t...@cybersource.com.au wrote:
 On Mon, 02 Aug 2010 17:19:46 -0700, samwyse wrote:
  Fortunately, I don't need the functionality of the object, I just want
  something that won't generate an error when I use it.  So, what is the
  quickest way to to create such an object (replacing the 'pass' in my
  first snippet).  My solution is this:

      class C:
          def filter(self, *args, **kwds):
              pass
      register = C()

  but it seems like I should be able to do something better, as measured
  by lines of code, faking more than just a 'filter' method, or both.  Any
  ideas?  Thanks!

 You want a variation on the Null Object design pattern.

 class NullWithMethods(object):
     def __getattr__(self, name):
         return self
     def __call__(self, *args, **kwargs):
         pass

 And in action:

  c = NullWithMethods()
  c.spam(hello, world)
  c.something_completely_unlikely.spam.ham(hello, world, foo=42)

 --
 Steven

JM emailed me a good solution, but yours is great! Thanks!
-- 
http://mail.python.org/mailman/listinfo/python-list


simple (I hope!) problem

2010-08-02 Thread samwyse
I'm writing for the Google app engine and have stubbed my toe yet
again on a simple obstacle.  Non-trivial app engines programs require
the import of several modules that aren't normally in my PYTHONPATH.
I'd like to be able to test my code outside of the app engine
framework.  I've tried several solutions in the past that worked but
weren't particularly elegant or portable.  Now I've had a new idea.
Here's my latest attempt:

import os, re
if __name__ == '__main__':
pass
else
from google.appengine.ext import webapp
register = webapp.template.create_template_register()

This works great, except my code makes use of the resister object in
several places, like this:

register.filter(emptylines)

Fortunately, I don't need the functionality of the object, I just want
something that won't generate an error when I use it.  So, what is the
quickest way to to create such an object (replacing the 'pass' in my
first snippet).  My solution is this:

class C:
def filter(self, *args, **kwds):
pass
register = C()

but it seems like I should be able to do something better, as
measured by lines of code, faking more than just a 'filter' method, or
both.  Any ideas?  Thanks!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: simple integer subclass

2010-08-02 Thread samwyse
On Aug 2, 6:52 pm, Andreas Pfrengle a.pfren...@gmail.com wrote:
 I'm trying to define a subclass of int called int1. An int1-object
 shall behave exactly like an int-object, with the only difference that
 the displayed value shall be value + 1 (it will be used to display
 array indices starting at 1 instead of 0). Right now I have:

 class int1(int):
     def __str__(self):
         return int.__str__(self + 1)

 However, if I calculate with int1 and int- (or other number) objects,
 the result is always coerced to an int (or other number object), e.g:
 a = int1(5)
 b = 5
 print a      # 6
 print a+b  #10

 How can I tell int1 to be the default integer object? Do I need to
 overload *every* mathematical operation method of int, or is there an
 easier way?

I had a similar problem a few years ago, and couldn't find a solution
then.  The thread from back then may shed some light on your problem.
http://groups.google.com/group/comp.lang.python/browse_thread/thread/10cfe2affc265ac/2ad03b121c1c6489
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Behavior of re.split on empty strings is unexpected

2010-08-02 Thread samwyse
On Aug 2, 12:34 pm, John Nagle na...@animats.com wrote:
 The regular expression split behaves slightly differently than string
 split:

I'm going to argue that it's the string split that's behaving oddly.
To see why, let's first look at some simple CSV values:
cat,dog
,missing,,values,

How many fields are on each line and what are they?  Here's what
re.split(',') says:

 re.split(',', 'cat,dog')
['cat', 'dog']
 re.split(',', ',missing,,values,')
['', 'missing', '', 'values', '']

Note that the presence of missing values is clearly flagged via the
presence of empty strings in the results.  Now let's look at string
split:

 'cat,dog'.split(',')
['cat', 'dog']
 ',missing,,values,'.split(',')
['', 'missing', '', 'values', '']

It's the same results.  Let's try it again, but replacing the commas
with spaces.

 re.split(' ', 'cat dog')
['cat', 'dog']
 re.split(' ', ' missing  values ')
['', 'missing', '', 'values', '']
 'cat dog'.split(' ')
['cat', 'dog']
 ' missing  values '.split(' ')
['', 'missing', '', 'values', '']

It's the same results; however many people don't like these results
because they feel that whitespace occupies a privileged role.  People
generally agree that a string of consecutive commas means missing
values, but a string of consecutive spaces just means someone held the
space-bar down too long.  To accommodate this viewpoint, the string
split is special-cased to behave differently when None is passed as a
separator.  First, it splits on any number of whitespace characters,
like this:

 re.split('\s+', ' missing  values ')
['', 'missing', 'values', '']
 re.split('\s+', 'cat dog')
['cat', 'dog']

But it also eliminates any empty strings from the head and tail of the
list, because that's what people generally expect when splitting on
whitespace:

 'cat dog'.split(None)
['cat', 'dog']
 ' missing  values '.split(None)
['missing', 'values']
-- 
http://mail.python.org/mailman/listinfo/python-list


[issue3058] Let SimpleXMLRPCServer pass client_address to called functions.

2010-07-11 Thread samwyse

samwyse samw...@gmail.com added the comment:

More importantly, the dispatch method is now part of
the SimpleXMLRPCDispatcher, which (as a mix-in class) has no direct access
to the RequestHandler instance that comprises the request.  This breaks
Victor's and my idea, unless one is willing to subclass
SimpleXMLRPCRequestHandler to provide a_dispatch method.  The ability to do
this, however, is only provided for backward compatibility and I doubt it
could be part of a general solution.

On Thu, Jul 8, 2010 at 11:33 PM, Terry J. Reedy rep...@bugs.python.orgwrote:


 Terry J. Reedy tjre...@udel.edu added the comment:

 In 3.x, class  SimpleXMLRPCServer lives in module xmlrpc.server

 --
 nosy: +tjreedy
 versions: +Python 3.2 -Python 2.5

 ___
 Python tracker rep...@bugs.python.org
 http://bugs.python.org/issue3058
 ___


--
Added file: http://bugs.python.org/file17956/unnamed

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue3058
___More importantly, the dispatch method is now part of 
the SimpleXMLRPCDispatcher, which (as a mix-in class) has no direct access to 
the span class=Apple-style-span style=font-family: arial, sans-serif; 
font-size: 13px; border-collapse: collapse; RequestHandler instance that 
comprises the request.  This breaks Victor#39;s and my idea, unless one is 
willing to subclass SimpleXMLRPCRequestHandler to provide a_dispatch method. 
 The ability to do this, however, is only provided for backward compatibility 
and I doubt it could be part of a general solution./spanbr

brdiv class=gmail_quoteOn Thu, Jul 8, 2010 at 11:33 PM, Terry J. Reedy 
span dir=ltrlt;a 
href=mailto:rep...@bugs.python.org;rep...@bugs.python.org/agt;/span 
wrote:brblockquote class=gmail_quote style=margin:0 0 0 
.8ex;border-left:1px #ccc solid;padding-left:1ex;

br
Terry J. Reedy lt;a href=mailto:tjre...@udel.edu;tjre...@udel.edu/agt; 
added the comment:br
br
In 3.x, class  SimpleXMLRPCServer lives in module xmlrpc.serverbr
br
--br
nosy: +tjreedybr
versions: +Python 3.2 -Python 2.5br
divdiv/divdiv class=h5br
___br
Python tracker lt;a 
href=mailto:rep...@bugs.python.org;rep...@bugs.python.org/agt;br
lt;a href=http://bugs.python.org/issue3058; 
target=_blankhttp://bugs.python.org/issue3058/agt;br
___br
/div/div/blockquote/divbr
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



Re: enhancing 'list'

2010-01-18 Thread samwyse
On Jan 18, 1:56 am, Terry Reedy tjre...@udel.edu wrote:
 On 1/17/2010 5:37 PM, samwyse wrote:





  Consider this a wish list.  I know I'm unlikely to get any of these in
  time for for my birthday, but still I felt the need to toss it out and
  see what happens.

  Lately, I've slinging around a lot of lists, and there are some simple
  things I'd like to do that just aren't there.

  s.count(x[, cmp[, key]])
  - return number of i‘s for which s[i] == x.  'cmp' specifies a custom
  comparison function of two arguments, as in '.sort'.  'key' specifies
  a custom key extraction function of one argument.
  s.index(x[, i[, j[, cmp[, key)
  - return smallest k such that s[k] == x and i= k  j.  'cmp' and
  'key' are as above.
  s.rindex(x[, i[, j[, cmp[, key)
  - return largest k such that s[k] == x and i= k  j.  'cmp' and
  'key' are as above.

  There are two overlapping proposals here.  One is to add the .rindex
  method, which strings already have.  The other is to extend the
  optional arguments of .sort to all other methods that test for item
  equality.

  One last thing, the Python 2.6.2 spec says .count and .index only
  apply to mutable sequence types.  I see no reason why they
  (and .rindex) couldn't also apply to immutable sequences (tuples, in
  particular).

 In 3.x, tuple does have those methods, even though the doc is not clear
 (unless fixed by now).

That's good to hear.  Perhaps I should have tried them directyly, but
my 3.1 docs still echo the 2.x docs, which only show them for
immutable sequences.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: enhancing 'list'

2010-01-18 Thread samwyse
On Jan 18, 3:06 am, Peter Otten __pete...@web.de wrote:
 samwyse wrote:
  Lately, I've slinging around a lot of lists, and there are some simple
  things I'd like to do that just aren't there.

  s.count(x[, cmp[, key]])
  - return number of i‘s for which s[i] == x.  'cmp' specifies a custom
  comparison function of two arguments, as in '.sort'.  'key' specifies
  a custom key extraction function of one argument.

 What's your use case exactly? If I were to enhance count/index/rindex I
 would go for the simpler

  missing = object()                                                  
  class List(list):                                                

 ...     def count(self, value=missing, predicate=missing):            
 ...             if value is missing:
 ...                     if predicate is missing:
 ...                             raise TypeError
 ...                     return sum(1 for item in self if predicate(item))
 ...             else:
 ...                     if predicate is not missing:
 ...                             raise TypeError
 ...                     return list.count(self, value)
 ... items = List(range(10))
  items.count(7)
 1
  items.count(predicate=lambda item: item%3)

 6

 which nicely covers all applications I can imagine.

 Peter

That is a good idea.  However, I was looking more at the simplicity of
building of ideas that are already present in .sort.  And this
implementation is pretty simple as well.

 class List(list):
import __builtin__
def count(self, value, cmp=__builtin__.cmp):
return sum(1 for item in self if not cmp(item, value))


 items = List(range(10))
 items.count(7)
1
 items.count(3, lambda a, b: not a%b)  # My way
6
 items.count(Ellipsis, lambda a, b: not a%3)  # Your way
6

As a side note, wouldn't it be nice if '...' could be used in more
places than just slices?  IMHO, a useful idiom would be to use it to
signify irrelevant or don't care, as opposed to 'None' which (in
my mind, at least) signifies missing or unknown.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: enhancing 'list'

2010-01-18 Thread samwyse
On Jan 17, 11:30 pm, Asun Friere afri...@yahoo.co.uk wrote:
 On Jan 18, 9:37 am, samwyse samw...@gmail.com wrote:

  Consider this a wish list.  I know I'm unlikely to get any of these in
  time for for my birthday, but still I felt the need to toss it out and
  see what happens.

  Lately, I've slinging around a lot of lists, and there are some simple
  things I'd like to do that just aren't there.

 If memory serves me correctly, it has been possible to subclass 'built-
 in' types since Py2.2 or thereabouts.

True, but I've had bad experiences doing that.  See, for example,
http://groups.google.com/group/comp.lang.python/browse_thread/thread/10cfe2affc265ac
where I tried to subclass 'int'.  More importantly, subclassing means
that people have to keep re-inventing the same methods.  Having a
single implementation would save time, not to mention the speed
advantages of implementing them in the hosting language (C,
Java, .NET, etc).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The answer

2010-01-18 Thread samwyse
On Jan 17, 8:30 pm, Jive Dadson notonthe...@noisp.com wrote:
 Okay, with your help I've figured it out.  Instructions are below, but
 read the caveat by Ben Fenny in this thread.  All this stuff is good for
 one default version of Python only.  The PYTHONPATH described below, for
 example, cannot specify a version number.  Yes, that's a pain in the
 butt, but there's no way around it.  If you switch versions, you may
 have to delete all the .pyc files that will show up in the module
 folders.  Python ought to check them to see if they are valid, but I do
 not know if it does so.

 These instructions are for MS Windows.

 1) Create your modules folder. Let's say it's named Modules.  The
 documentation calls it a package.

 2) In an explorer window or on the desktop, right click on My Computer,
 and select Properties.

 3) Select the Advanced tab, and click on Environment Variables near the
 bottom.

 4) Look for an environment variable named PYTHONPATH.

     a) If you do not find one, create one using the New button(s). I
 don't know if it has to be in User Variables or System Variables.  To
 save time experimenting, I just put one in both. For the value, put the
 full path of the folder Modules.

     b) If there's already a PYTHONPATH,  Edit it, adding a semi-colon
 and the full path of folder Module to the end.

 5) Put your module folders into the folder Module.

 6) (Here's a really arcane bit.) Into each module folder, put a file
 named __init__.py.  It will be executed when you load the module.  It
 can be empty, but it has to be there or else the module folder will be
 ignored.

In your original thread, you never quite said why you can't use site-
packages and .pth files.  Are you not allowed to modify your local
installation?  If you are writing something for distribution to
others, then site-packages and .pth files are the best way to go,
since they don't assume any particular operating system.  If you can't
(or won't) use them, then just create Module as a sub-directory of
wherever your program lives, since that directory is always prepended
to PYTHONPATH.  If you need to use the same module from multiple
directories, most modern operating systems support symbolic links; if
you're using Windows, well, here's a nickel kid, get yourself a better
computer (http://farm1.static.flickr.com/
89/240711122_f9888e5a3b_o.jpg).

I don't think that __init__.py is very arcane, since it is described
in detail in the documentation.  It's also a great place to use the
standard site.addsitedir() function, which is another platform
independent way to manipulate Python's search path.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: enhancing 'list'

2010-01-18 Thread samwyse
On Jan 18, 6:20 am, Peter Otten __pete...@web.de wrote:
 Note that the cmp() builtin and the cmp parameter for list.sort() are gone
 in Python 3.

I've got Python 3 installed, and am using it for most new
development.  In this case case, however, I'm writing for the Google
App Engine, which is stuck at 2.5.  :(  (Curiously, no matter how I
order my PATH, the wrong version seems to appear first more than half
the time!  I'm seriously considering renaming all my Python 3 code to
use a .py3 file extension.)

 samwyse wrote:
  As a side note, wouldn't it be nice if '...' could be used in more
  places than just slices?  IMHO, a useful idiom would be to use it to
  signify irrelevant or don't care, as opposed to 'None' which (in
  my mind, at least) signifies missing or unknown.

 That is a pretty subtle distinction...

Hey, I'm a pretty subtle person...

 I prefer keyword arguments, but in Python 3 you can use the ellipsis literal
 freely:

  ... == ...
 True
  [..., 42, ...].count(...)

 2

 Peter

I must have either missed or forgotten about that.  Thanks!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Parse a log file

2010-01-18 Thread samwyse
On Jan 18, 6:52 am, kak...@gmail.com kak...@gmail.com wrote:
 Hello to all!
 I want to parse a log file with the following format for
 example:
               TIMESTAMPE            Operation     FileName
 Bytes
 12/Jan/2010:16:04:59 +0200   EXISTS       sample3.3gp   37151
 12/Jan/2010:16:04:59 +0200  EXISTS        sample3.3gp   37151
 12/Jan/2010:16:04:59 +0200  EXISTS        sample3.3gp   37151
 12/Jan/2010:16:04:59 +0200  EXISTS        sample3.3gp   37151
 12/Jan/2010:16:04:59 +0200  EXISTS        sample3.3gp   37151
 12/Jan/2010:16:05:05 +0200  DELETE      sample3.3gp   37151

 How can i count the operations for a month(e.g total of 40 Operations,
 30 exists, 10 delete?)
 Any tips?

 Thanks in advance
 Antonis

time.strptime(string[, format])
Parse a string representing a time according to a format. The return
value is a struct_time as returned by gmtime() or localtime().

The format parameter uses the same directives as those used by strftime
(); it defaults to %a %b %d %H:%M:%S %Y which matches the formatting
returned by ctime(). If string cannot be parsed according to format,
or if it has excess data after parsing, ValueError is raised. The
default values used to fill in any missing data when more accurate
values cannot be inferred are (1900, 1, 1, 0, 0, 0, 0, 1, -1).

 import time
 ts='12/Jan/2010:16:04:59 +0200'
 time.strptime(ts[:-6], '%d/%b/%Y:%H:%M:%S')
time.struct_time(tm_year=2010, tm_mon=1, tm_mday=12, tm_hour=16,
tm_min=4, tm_sec=59, tm_wday=1, tm_yday=12, tm_isdst=-1)

I leave the conversion of the last six characters (the time zone
offset) as an exercise for the student.  :)
-- 
http://mail.python.org/mailman/listinfo/python-list


enhancing 'list'

2010-01-17 Thread samwyse
Consider this a wish list.  I know I'm unlikely to get any of these in
time for for my birthday, but still I felt the need to toss it out and
see what happens.

Lately, I've slinging around a lot of lists, and there are some simple
things I'd like to do that just aren't there.

s.count(x[, cmp[, key]])
- return number of i‘s for which s[i] == x.  'cmp' specifies a custom
comparison function of two arguments, as in '.sort'.  'key' specifies
a custom key extraction function of one argument.
s.index(x[, i[, j[, cmp[, key)
- return smallest k such that s[k] == x and i = k  j.  'cmp' and
'key' are as above.
s.rindex(x[, i[, j[, cmp[, key)
- return largest k such that s[k] == x and i = k  j.  'cmp' and
'key' are as above.

There are two overlapping proposals here.  One is to add the .rindex
method, which strings already have.  The other is to extend the
optional arguments of .sort to all other methods that test for item
equality.

One last thing, the Python 2.6.2 spec says .count and .index only
apply to mutable sequence types.  I see no reason why they
(and .rindex) couldn't also apply to immutable sequences (tuples, in
particular).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Bare Excepts

2009-12-30 Thread samwyse
On Dec 30, 7:23 am, Jean-Michel Pichavant jeanmic...@sequans.com
wrote:

 Rule N°2:
 dont use BARE EXCEPT, or you'll piss off MRAB for good :o). Beside from
 kidding, don't use bare except.

I inherited some code that used bare excepts *everywhere*.  There were
about 4K lines of code, IIRC, and I think that they were more except
clauses than elses.  Eventually, I used sed to add a print_exc() after
each one, just so I could figure out what the expected exceptions
were.  It was so bad that I seriously considered writing a program
just to parse all the tracebacks from my instrumented version and then
revise the source code for me, but I didn't want to accidentally miss
any real errors.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to test a URL request in a while True loop

2009-12-30 Thread samwyse
On Dec 30, 10:00 am, Brian D brianden...@gmail.com wrote:

 What I don't understand is how to test for a valid URL request, and
 then jump out of the while True loop to proceed to another line of
 code below the loop. There's probably faulty logic in this approach. I
 imagine I should wrap the URL request in a function, and perhaps store
 the response as a global variable.

 This is really more of a basic Python logic question than it is a
 urllib2 question.

There, I've condensed your question to what you really meant to say.
You have several approaches.  First, let's define some useful objects:
 max_attempts = 5
 def do_something(i):
assert 2  i  5

Getting back to original question, if you want to limit the number of
attempts, don't use a while, use this:

 for count in xrange(max_attempts):
print 'attempt', count+1
do_something(count+1)

attempt 1
Traceback (most recent call last):
  File pyshell#55, line 3, in module
do_something(count+1)
  File pyshell#47, line 2, in do_something
assert 2  i  5
AssertionError

If you want to keep exceptions from ending the loop prematurely, you
add this:

 for count in xrange(max_attempts):
print 'attempt', count+1
try:
do_something(count+1)
except StandardError:
pass

Note that bare except clauses are *evil* and should be avoided.  Most
exceptions derive from StandardError, so trap that if you want to
catch errors.  Finally, to stop iterating when the errors cease, do
this:

 try:
for count in xrange(max_attempts):
print 'attempt', count+1
try:
do_something(count+1)
raise StopIteration
except StandardError:
pass
except StopIteration:
pass

attempt 1
attempt 2
attempt 3


Note that StopIteration doesn't derive from StandardError, because
it's not an error, it's a notification.  So, throw it if and when you
want to stop iterating.

BTW, note that you don't have to wrap your code in a function.
do_something could be replaced with it's body and everything would
still work.
-- 
http://mail.python.org/mailman/listinfo/python-list


class version of func_globals?

2009-12-29 Thread samwyse
Is there any way to get the global namespace of the module in which a
class was defined?  Answers for both Python 2.x and 3.x will be
cheerfully accepted.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: class version of func_globals?

2009-12-29 Thread samwyse
On Dec 29, 5:18 am, Dave Angel da...@ieee.org wrote:
 samwyse wrote:
  Is there any way to get the global namespace of the module in which a
  class was defined?  Answers for both Python 2.x and 3.x will be
  cheerfully accepted.

 I don't know if it's the same in general, but consider the following
 sequence in 2.6:

 import sys

 class MyClass(object):
     pass

 print class--, dir(MyClass)
 print module--, dir(MyClass.__module__)
 mod = sys.modules[MyClass.__module__]
 print mod
 print globals--, dir(mod)

 DaveA

Excellent!  Exactly what I wanted, but wasn't clever enough to figure
out for myself.  Thank you very much.
-- 
http://mail.python.org/mailman/listinfo/python-list


creating ZIP files on the cheap

2009-12-23 Thread samwyse
I've got an app that's creating Open Office docs; if you don't know,
these are actually ZIP files with a different extension.  In my case,
like many other people, I generating from boilerplate, so only one
component (content.xml) of my ZIP file will ever change.  Instead of
creating the entire ZIP file each time, what is the cheapest way to
accomplish my goal?  I'd kind-of like to just write the first part of
the file as a binary blob, then write my bit, then write most of the
table of contents as another blob, and finally write a TOC entry for
my bit.  Has anyone ever done anything like this?  Thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: UnicodeDecodeError? Argh! Nothing works! I'm tired and hurting and...

2009-11-24 Thread samwyse
On Nov 24, 4:43 pm, Steven D'Aprano st...@remove-this-
cybersource.com.au wrote:

 Oh yes, and people using Windows can't use maildir because (1) it doesn't
 allow colons in names, and (2) it doesn't have atomic renames. Neither of
 these are insurmountable problems: an implementation could substitute
 another character for the colon, and while that would be a technical
 violation of the standard, it would still work. And the lack of atomic
 renames would simply mean that implementations have to be more careful
 about not having two threads writing to the one mailbox at the same time.

A common work around for the former is to URL encode the names, which
let's you stick all sorts of odd characters.

I'm afraid I can't help with the latter, though.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python simply not scaleable enough for google?

2009-11-12 Thread samwyse
On Nov 11, 3:57 am, Robert P. J. Day rpj...@crashcourse.ca wrote:
 http://groups.google.com/group/unladen-swallow/browse_thread/thread/4...

   thoughts?

Google's already given us its thoughts:
http://developers.slashdot.org/story/09/11/11/0210212/Go-Googles-New-Open-Source-Programming-Language
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: New syntax for blocks

2009-11-11 Thread samwyse
On Nov 10, 1:23 pm, r rt8...@gmail.com wrote:
 Forgive me if i don't properly explain the problem but i think the
 following syntax would be quite beneficial to replace some redundant
 if's in python code.

 if something_that_returns_value() as value:
     #do something with value

 # Which can replace the following syntactical construct...

 value = something_that_returns_value()
 if value:
     #do something with value

I don't like the proposed syntax.  I know, it's copied from the with
statement, but it makes the assignment look like an afterthought.
Plus, it's not good English when read aloud.

If you want something useful, wait two years for the moratorium to
expire and then figure out how to augment the for statement with an
if clause, as is currently done in comprehensions.  In other words,
instead of this:
  for target_list in expression_list : suite
let's have this:
  for target_list in expression_list [ if expression_nocond ]
: suite

You don't save much typing, but you really do save one indention
level.  OTOH, I can see the use of an else-clause following the 'for'
as being even more confusing to anyone new to the language.

And there's also the fact that an expression_list consists of
conditional_expressions, which potentially use if.  You could
probably get the parser to figure it out based on the presence of the
else clause, but it wouldn't be easy.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python C api: create a new object class

2009-11-11 Thread samwyse
On Nov 10, 1:09 pm, lallous lall...@lgwm.org wrote:
 Hello

 I have 3 questions, hope someone can help:

 1)
 How can I create an instance class in Python, currently I do:

 class empty:
   pass

 Then anytime I want that class (which I treat like a dictionary):

 o = empty()
 o.myattr = 1
 etc

 Is there is a one line syntax to instantiate an instance?

I think that you want this:

class c(object):
def __init__(self, **kwds):
self.__dict__ = kwds

x = c(a=1, b=2)
print x.a, x.b
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python along or bash combined with python (for manipulating files)

2009-10-13 Thread samwyse
On Oct 13, 9:13 pm, Peng Yu pengyu...@gmail.com wrote:
 Bash is easy to use on manipulating files and directories (like change
 name or create links, etc) and on calling external programs. For
 simple functions, bash along is enough. However, bash does not support
 the complex functions. Python has a richer library that could provide
 support for complex functions (such compute the relative path between
 two paths).

 I'm wondering for a task that can not be done with bash along whether
 it would be better to do in pure python or with a mix of both python
 and bash. What I care is mostly coding speed and a little bit
 maintainability (but not much). Can somebody provide some experience
 on when to combine python and bash and when to use pure python?

Scripting languages try to optimize gluing disparate programs together
to accomplish a task; bash excels at this.  Programing languages try
to optimize finding the solution to a problem; Python excels at this.

Generally, I try to stick to one language per problem, be it bash, C+
+, Java, Perl or Python.  Bash scripts translate easily into the
others, so you don't lose much time if you decide you started with the
wrong language.

Countering that, I also maintain a toolbox of programs that I can
call upon when needed.  In those cases, I don't hesitate to call a
program that I've written in any language from a bash script.

BTW, I actually prefer ksh to bash, but YMMV.
-- 
http://mail.python.org/mailman/listinfo/python-list


Looking for a buffered/windowed iterator

2009-10-12 Thread samwyse
I have Python program that lets me interact with a bunch of files.
Unfortunately, the program assumes that the bunch is fairly small, and
I have thousands of files on relatively slow storage.  Just creating a
list of the file names takes several minutes, so I'm planning to
replace the list with an iterator in another thread.  However, each
file requires several seconds to load before I can work with it.  The
current code alleviates this via a thread that loads the next file
while I'm examining the current one.  I'd like my new iterator to
provide a fixed window around the current item.  I've looked at the
pairwise recipe, but it doesn't allow me to peek at items within the
window (to generate thumbnails, etc), and I've looked at arrayterator,
but it divides the stream into small contiguous blocks where crossing
a block boundary is relatively expensive.

Previous discussions in c.l.py (primarily those that propose new
functions to be added to itertools) claim that people do this all the
time, but seem woefully short of actual examples.  Before I possibly
re-invent the wheel(*), could someone point me to some actual code
that approximates what I want to do?  Thanks.

(*) Re-inventing a wheel is generally pretty simple.  But then you
discover that you also need to invert axle grease, a leaf spring
suspension, etc.
-- 
http://mail.python.org/mailman/listinfo/python-list


myparentclass.__subclasses__() not working for me

2009-09-14 Thread samwyse
### I've tried this under both Python 2.5.1 and 3.1.1, and it isn't
working with either one.  Here is my program:

class Plugin(object):
This is the base object for a plug-in.
pass

def load_plugins(plugin_subdir='plugins'):
import sys, pkgutil, imp, os.path

try:
# Use this path if we're running as a module.
homedir = __path__[0]
except NameError:
# Use this path if we're running stand-alone.
homedir = sys.path[0]
plugin_path = [ os.path.join(homedir, plugin_subdir) ]

modules = {}
for loader, name, is_pkg in pkgutil.iter_modules(plugin_path):
file, pathname, desc = imp.find_module(name, plugin_path)
modules[name] = imp.load_module(name, file, pathname, desc)
for pair in modules.items():
print('name = %r\nmodule = %r\n' % pair)

if __name__ == '__main__':
print('subclasses = %r\n' %(Plugin.__subclasses__()))
load_plugins()
print('subclasses = %r\n' %(Plugin.__subclasses__()))

### And here is my plugin, in plugins/myplugin.py:

from plugin import Plugin
class MyPlugin(Plugin):
pass

### When I run the main program, I get this:

subclasses = []

name = 'myplugin'
module = module 'myplugin' from 'C:\Documents and Settings\sam_denton
\Desktop\scripting\plugins\myplugin.py'

subclasses = []

###  Obviously, myplugin is being found found and loaded, but my base
class doesn't know about it.  Any ideas?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: best way to display photos

2009-08-25 Thread samwyse
On Aug 25, 1:40 am, Thomas Guettler h...@tbz-pariv.de wrote:
 Some years ago I had the same problem.

 I wrote a simple app with pygtk. You get get it from here:
    http://guettli.sourceforge.net/gthumpy/src/README.html
 The next pictures get loaded in background. Switching from
 one image to the next is faster then in some other apps. But
 somehow it is not ready for the public. I guess I am the only
 one who uses it.

 Flags are symlinks in the home directory .local/share/app/gthumpy. This
 means you should not move your images, otherwise the flags get lost.

 I don't think it will work on ms-windows, but it could be ported.

 Or you use mirage:http://mirageiv.berlios.de/index.html

 It is a pygtk image viewer. You can define shortcuts that execute user defined
 commands.

   Thomas

 samwyse schrieb:

  I have several thousand photographs that I need to quickly classify,
  all by myself.  After extensive searches, I have been unable to find
  anything to my liking, so desire to write something myself.  I'm
  thinking about displaying a photo and waiting for keystrokes to tag
  it; 'i' for interior, 'e' for exterior, etc., while hitting space or
  enter will advance to the next photo.  My big question is, what's the
  best way to display the photos.  I've used PIL in the past, but IIRC
  it uses an external program.  Pygame is the next obvious choice, but
  like PIL it requires an add-in.  That leaves Tkinter.  Has anyone used
  it to display .JPG files, perhaps with rescaling to fit my screen?
  How is its performance?  Is there any other possibilities that I've
  missed?  Thanks.

 --
 Thomas Guettler,http://www.thomas-guettler.de/
 E-Mail: guettli (*) thomas-guettler + de

Thanks!  Both of these look very similar to what I need.  I will
investigate further.
-- 
http://mail.python.org/mailman/listinfo/python-list


best way to display photos

2009-08-24 Thread samwyse
I have several thousand photographs that I need to quickly classify,
all by myself.  After extensive searches, I have been unable to find
anything to my liking, so desire to write something myself.  I'm
thinking about displaying a photo and waiting for keystrokes to tag
it; 'i' for interior, 'e' for exterior, etc., while hitting space or
enter will advance to the next photo.  My big question is, what's the
best way to display the photos.  I've used PIL in the past, but IIRC
it uses an external program.  Pygame is the next obvious choice, but
like PIL it requires an add-in.  That leaves Tkinter.  Has anyone used
it to display .JPG files, perhaps with rescaling to fit my screen?
How is its performance?  Is there any other possibilities that I've
missed?  Thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: setting Referer for urllib.urlretrieve

2009-08-10 Thread samwyse
On Aug 9, 9:41 am, Steven D'Aprano st...@remove-this-
cybersource.com.au wrote:
 On Sun, 09 Aug 2009 06:13:38 -0700,samwysewrote:
  Here's what I have so far:

  import urllib

  class AppURLopener(urllib.FancyURLopener):
      version = App/1.7
      referrer = None
      def __init__(self, *args):
          urllib.FancyURLopener.__init__(self, *args)
          if self.referrer:
              addheader('Referer', self.referrer)

  urllib._urlopener = AppURLopener()

  Unfortunately, the 'Referer' header potentially varies for each url that
  I retrieve, and the way the module is written, I can't change the calls
  to __init__ or open. The best idea I've had is to assign a new value to
  my class variable just before calling urllib.urlretrieve(), but that
  just seems ugly.  Any ideas?  Thanks.

 [Aside: an int variable is an int. A str variable is a str. A list
 variable is a list. A class variable is a class. You probably mean a
 class attribute, not a variable. If other languages want to call it a
 variable, or a sausage, that's their problem.]

 If you're prepared for a bit of extra work, you could take over all the
 URL handling instead of relying on automatic openers. This will give you
 much finer control, but it will also require more effort on your part.
 The basic idea is, instead of installing openers, and then ask the urllib
 module to handle the connection, you handle the connection yourself:

 make a Request object using urllib2.Request
 make an Opener object using urllib2.build_opener
 call opener.open(request) to connect to the server
 deal with the connection (retry, fail or read)

 Essentially, you use the Request object instead of a URL, and you would
 add the appropriate referer header to the Request object.

 Another approach, perhaps a more minimal change than the above, would be
 something like this:

 # untested
 class AppURLopener(urllib.FancyURLopener):
     version = App/1.7
     def __init__(self, *args):
         urllib.FancyURLopener.__init__(self, *args)
     def add_referrer(self, url=None):
         if url:
             addheader('Referer', url)

 urllib._urlopener = AppURLopener()
 urllib._urlopener.add_referrer(http://example.com/;)

Thanks for the ideas.  I'd briefly considered something similar to
your first idea, implementing my own version of urlretrieve to accept
a Request object, but it does seem like a good bit of work.  Maybe
over Labor Day.  :)

The second idea is pretty much what I'm going to go with for now.  The
program that I'm writing is almost a clone of wget, but it fixes some
personal dislikes with the way recursive retrievals are done.  (Or
maybe I just don't understand wget's full array of options well
enough.)  This means that my referrer changes as I bounce up and down
the hierarchy, which makes this less convenient.  Still, it does seem
more convenient that re-writing the module from scratch.
-- 
http://mail.python.org/mailman/listinfo/python-list


setting Referer for urllib.urlretrieve

2009-08-09 Thread samwyse
Here's what I have so far:

import urllib

class AppURLopener(urllib.FancyURLopener):
version = App/1.7
referrer = None
def __init__(self, *args):
urllib.FancyURLopener.__init__(self, *args)
if self.referrer:
addheader('Referer', self.referrer)

urllib._urlopener = AppURLopener()

Unfortunately, the 'Referer' header potentially varies for each url
that I retrieve, and the way the module is written, I can't change the
calls to __init__ or open. The best idea I've had is to assign a new
value to my class variable just before calling urllib.urlretrieve(),
but that just seems ugly.  Any ideas?  Thanks.

PS for anyone not familiar with the RFCs:  Yes, I'm spelling
referrer correctly everywhere in my code.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Recipes for trace statements inside python programs?

2009-06-25 Thread samwyse
I use an @trace decorator.  This (http://wordaligned.org/articles/
echo) will get you started but there are lots of others available.  My
personal preference is a decorator that catches, displays and re-
raises exceptions as well as displaying both calling parameters and
returned values.

btw, here's a cool Python3K (or 2.6 if you turn on print functions)
trick that's sort-of on topic since it relates to log files and such:
import functools, sys
warn = functools.partial(print, file=sys.stderr)
logfile = open(...)
log = functools.partial(print, file=logfile)
# etc.

On Jun 25, 3:33 am, Francesco Bochicchio bieff...@gmail.com wrote:
 Hi all,

 as many - I think - python programmers, I find muself debugging my
 scripts by placing print statements in strategic places rather than
 using the python debugger, and commenting/uncommenting them according
 to myy deugging needs.  After a time, these prints staements start to
 evolving in some ad-hoc half-baked framework ... so I wonder if there
 is somewhere there is a full-baked trace statement support framework
 which I can use. I'm aware of the logging module, but for me it its
 more geared toward  application logging rather than toward trace for
 debugging purpose.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python simple web development

2009-06-25 Thread samwyse
I just started with web2py (http://www.web2py.com/) for an internal-
use-only app that doesn't need to be very pretty.  Been using it for
about a week and after re-watching the tutorial, I've decided that I'm
making things way too complicated.  So today I'm going to replace a
lot of my code with some built-ins.

On Jun 25, 2:29 am, Private Private mail...@gmail.com wrote:
 Hi,

 I am looking for a python library which will allow me to do a simple
 web development. I need to use
 some forms (but nice looking :-) ), creating images based on input
 from those forms, etc. I have read a bit about Django and TurboGears
 but I am afraid that this is too big for my requirements (am I
 wrong ?).

 Can you suggest anything ?

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


I need a dict that inherits its mappings

2009-06-25 Thread samwyse
I need a dict-like object that, if it doesn't contain a key, will
return the value from a parent object.  Is there an easy way to do
this so I don't have to define __getitem__ and __contains__ and others
that I haven't even thought of yet?  Here's a use case, if you're
confused:

en_GB=mydict()
en_US=mydict(en_GB)

en_GB['bonnet']='part of your car'
print en_US['bonnet']  # prints 'part of your car'

en_US['bonnet']='a type of hat'
print en_US['bonnet']  # prints 'a type of hat'
print en_GB['bonnet']  # prints 'part of your car'
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to convert he boolean values into integers

2009-06-25 Thread samwyse
 int('1010100110', 2)
678

On Jun 25, 7:06 am, krishna gnperu...@gmail.com wrote:
 Hi Guys,

 I need to convert 1010100110 boolean value to some think like 2345, if
 its possible then post me your comment on this

 Advanced thanks for all

 Narayana perumal.G

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


[issue6321] Reload Python modules when running programs

2009-06-21 Thread samwyse

New submission from samwyse samw...@gmail.com:

Every time IDLE is asked to run a program, it doesn't ensure that the 
modules referenced by the program are completely loaded.  This can cause 
problems if one of those modules is also being edited, because once it 
is loaded, any subsequent changes are not compiled and re-loaded.  
PyUnit faced a similar problem and solved it with a custom importer 
(http://pyunit.sourceforge.net/notes/reloading.html).  Ideally, the 
custom importer would be used in two places:  The obvious choice is when 
a program is run, unloading when it returns.  The less obvious is when 
the Python Shell window is opened, since import statements can be run 
from there as well.  Closing that window should cause all such imports 
to be unloaded.  Of course, care must be taken to insure that all run 
commands are properly nested within the lifetime of a shell window.

--
components: IDLE
messages: 89593
nosy: samwyse
severity: normal
status: open
title: Reload Python modules when running programs
type: feature request
versions: Python 3.0

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue6321
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



Re: preferring [] or () in list of error codes?

2009-06-09 Thread samwyse
On Jun 8, 10:06 pm, Chris Rebert c...@rebertia.com wrote:
 On Mon, Jun 8, 2009 at 6:57 PM, samwysesamw...@gmail.com wrote:
  On Jun 8, 7:37 pm, Carl Banks pavlovevide...@gmail.com wrote:
  On Jun 8, 4:43 pm, Ben Finney ben+pyt...@benfinney.id.au wrote:
   m...@pixar.com writes:
Is there any reason to prefer one or the other of these statements?

        if e.message.code in [25401,25402,25408]:
        if e.message.code in (25401,25402,25408):

  If you want to go strictly by the book, I would say he ought to be
  using a set since his collection of numbers has no meaningful order
  nor does it make sense to list any item twice.

  As the length of the list increases, the increased speeds of looking
  something up makes using a set makes more sense.  But what's the best
  way to express this?  Here are a few more comparisons (using Python
  3.0)...

  S=lambda x:x in set((25401,25402,25408))
  dis(S)
   1           0 LOAD_FAST                0 (x)
               3 LOAD_GLOBAL              0 (set)
               6 LOAD_CONST               3 ((25401, 25402, 25408))
               9 CALL_FUNCTION            1
              12 COMPARE_OP               6 (in)
              15 RETURN_VALUE
  S=lambda x:x in{25401,25402,25408}
  dis(S)
   1           0 LOAD_FAST                0 (x)
               3 LOAD_CONST               0 (25401)
               6 LOAD_CONST               1 (25402)
               9 LOAD_CONST               2 (25408)
              12 BUILD_SET                3
              15 COMPARE_OP               6 (in)
              18 RETURN_VALUE
  S=lambda x:x in{(25401,25402,25408)}
  dis(S)
   1           0 LOAD_FAST                0 (x)
               3 LOAD_CONST               3 ((25401, 25402, 25408))
               6 BUILD_SET                1
               9 COMPARE_OP               6 (in)
              12 RETURN_VALUE

  I conclude that using constructors is generally a bad idea, since the
  compiler doesn't know if you're calling the builtin or something with
  an overloaded name.  I presume that the compiler will eventually
  optimize the second example to match the last, but both of them use
  the BUILD_SET opcode.  I expect that this can be expensive for long

 Erm, unless I misunderstand you somehow, the second example will and
 should *never* match the last.
 The set {25401,25402,25408}, containing 3 integer elements, is quite
 distinct from the set {(25401,25402,25408)}, containing one element
 and that element is a tuple.
 set(X) != {X}; set([X]) = {X}

D'oh!  I was thinking about how you can initialize a set from an
iterator and for some reason thought that you could do the same with a
set constant.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: preferring [] or () in list of error codes?

2009-06-09 Thread samwyse
On Jun 9, 12:30 am, Emile van Sebille em...@fenx.com wrote:
 On 6/8/2009 8:43 PM Ben Finney said...

  The fact that literal set syntax is a relative newcomer is the primary
  reason for that, I'd wager.

 Well, no.  It really is more, that's odd... why use set?

Until I ran some timing tests this morning, I'd have said that sets
could determine membership faster than a list, but that's apparently
not true, assuming that the list has less than 8K members.  Above 16K
members, sets are much faster than lists.  I'm not sure where the
break is, or even why there's a break.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: preferring [] or () in list of error codes?

2009-06-08 Thread samwyse
On Jun 8, 7:37 pm, Carl Banks pavlovevide...@gmail.com wrote:
 On Jun 8, 4:43 pm, Ben Finney ben+pyt...@benfinney.id.au wrote:
  m...@pixar.com writes:
   Is there any reason to prefer one or the other of these statements?

           if e.message.code in [25401,25402,25408]:
           if e.message.code in (25401,25402,25408):

 If you want to go strictly by the book, I would say he ought to be
 using a set since his collection of numbers has no meaningful order
 nor does it make sense to list any item twice.

As the length of the list increases, the increased speeds of looking
something up makes using a set makes more sense.  But what's the best
way to express this?  Here are a few more comparisons (using Python
3.0)...

 S=lambda x:x in set((25401,25402,25408))
 dis(S)
  1   0 LOAD_FAST0 (x)
  3 LOAD_GLOBAL  0 (set)
  6 LOAD_CONST   3 ((25401, 25402, 25408))
  9 CALL_FUNCTION1
 12 COMPARE_OP   6 (in)
 15 RETURN_VALUE
 S=lambda x:x in{25401,25402,25408}
 dis(S)
  1   0 LOAD_FAST0 (x)
  3 LOAD_CONST   0 (25401)
  6 LOAD_CONST   1 (25402)
  9 LOAD_CONST   2 (25408)
 12 BUILD_SET3
 15 COMPARE_OP   6 (in)
 18 RETURN_VALUE
 S=lambda x:x in{(25401,25402,25408)}
 dis(S)
  1   0 LOAD_FAST0 (x)
  3 LOAD_CONST   3 ((25401, 25402, 25408))
  6 BUILD_SET1
  9 COMPARE_OP   6 (in)
 12 RETURN_VALUE

I conclude that using constructors is generally a bad idea, since the
compiler doesn't know if you're calling the builtin or something with
an overloaded name.  I presume that the compiler will eventually
optimize the second example to match the last, but both of them use
the BUILD_SET opcode.  I expect that this can be expensive for long
lists, so I don't think that it's a good idea to use set constants
inside loops.  Instead it should be assigned to a global or class
variable.
-- 
http://mail.python.org/mailman/listinfo/python-list


py3k printing generators -- not!

2009-06-06 Thread samwyse
The one thing that's killing me in Python 3000 is that every time I
try to print something, it seems like I get generator object
genexpr at 0x01BAF508.  Googling only found one reference, a
posting elsewhere by one Carl Johnson (aka carlj7,
http://www.artima.com/forums/flat.jsp?forum=106thread=211200#275387),
which apparently was never answered.  Is anyone else finding this
bothersome, or is it entirely due to my FP background?

Always saying print(','.join(x)) gets tiresome in a hurry.  I've
thought about defining my own function prnt that wraps print and
fixes generators, but that requires me to get their type, which
despite the claims of help(type(x for x in range(0))) cannot be
found in builtins.  How are other solving this?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: py3k printing generators -- not!

2009-06-06 Thread samwyse
On Jun 6, 7:58 am, Carl Banks pavlovevide...@gmail.com wrote:
 On Jun 6, 5:28 am, samwyse samw...@gmail.com wrote:

  Always saying print(','.join(x)) gets tiresome in a hurry.  

 What about print(list(x))

Yeah, I like that.  Or, to save some typing:
  prnt = lambda x: print(list(x))

 Interestingly, the fact that it wasn't in builtins didn't stop you
 from being able to pass the type to the help() function.  I wonder if
 you can use the same trick to obtain the type for use in your prnt()
 function.

Of course, but it would mean evaluating an expression every time I
defined my function.  That just seems unhygenic

 (Failing that, you could use from types import GeneratorType.)

Ah!  I should've thought of that.

 In my experience, if you want a custom print function that prints
 things the way you want, you have to write it yourself.  People's
 expectations are too different.

Well, that's why I rferenced the other article,
http://www.artima.com/forums/flat.jsp?forum=106thread=211200#275387),
to show that other people have the same expectations.  (In the
article, Carl Johnson correctly points out that the real problem is
that str() responds to generators differently than do the other
collection types.)
-- 
http://mail.python.org/mailman/listinfo/python-list


Missing codecs in Python 3.0

2009-06-02 Thread samwyse
I have a Python 2.6 program (a code generator, actually) that tries
several methods of compressing a string and chooses the most compact.
It then writes out something like this:
  { encoding='bz2_codec', data = '...'}

I'm having two problems converting this to Py3.  First is the absence
of the bz2_codec, among others.  It was very convenient for my program
to delay selection of the decoding method until run-time and then have
an easy way to load the appropriate code.  Is this gone forever from
the standard libraries?

Second, I would write my data out using the 'string_escape' codec.
It, too, has been removed; there's a 'unicode_escape' codec which is
similar, but I would rather use a 'byte_escape' codec to produce
literals of the form b'asdf'.  Unfortunately, there isn't one that I
can find.  I could use the repr function, but that seems less
efficient.  Does anyone have any ideas?  Thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I change the behavior of the 'python-docs' action in IDLE?

2009-05-31 Thread samwyse
On Apr 16, 2:02 pm, samwyse samw...@gmail.com wrote:
 In the Windows version of Python 2.5, pressing F1 brought up the
 python.chm file.  I've just installed 2.6, and the same action 
 openshttp://www.python.org/doc/current/.  I'd prefer the former behavior.
 I know how to change the key bindings in config-keys.def, but I think
 I want to change the action, not the key binding.  Thanks.

I just installed Python 3.0.1 via the Windows 32-bit installer.
Opening Python Docs takes me to http://docs.python.org/dev/3.0/,
which doesn't exist.  Renaming python301.chm to python30.chm or
python300.chm doesn't seem to help find that file.  HELP!
-- 
http://mail.python.org/mailman/listinfo/python-list


Turning HTMLParser into an iterator

2009-05-31 Thread samwyse
I'm processing some potentially large datasets stored as HTML.  I've
subclassed HTMLParser so that handle_endtag() accumulates data into a
list, which I can then fetch when everything's done.  I'd prefer,
however, to have handle_endtag() somehow yield values while the input
data is still streaming in.  I'm sure someone's done something like
this before, but I can't figure it out.  Can anyone help?  Thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to find the last decorator of a chain

2009-05-31 Thread samwyse
On May 30, 6:16 pm, Gabriel gabr...@opensuse.org wrote:

 I have something like this:

 @render(format=a)
 @render(format=b)
 @
 def view(format, data):
   return data

 In my understanding this equivalent to:

 render('a',
  render('b',
   view(***)))

Not quite.  'render' is a function of one argument that returns a
decorator. So, the equivalent is more like this:
  view = render('a')(render('b')(view))
or more simply:
  fb = render('b')
  view = fb(view)
  fa = render('a')
  view = fa(view)

 Is there any way to know, in this case, that 'a' is the 'default' format?

Will this do?  (In case the formatting gets messed up, I've also
posted the code to http://python.pastebin.com/f7f229d9d)

##from functools import wraps

def render(c):
def decorator(f):
##@wraps(f)
def wrapper(*args, **kwds):
if getattr(wrapper, 'outermost', False):
print('outer wrapper', c)
else:
print('inner wrapper', c)
return f(*args, **kwds)
return wrapper
return decorator

def mark_as_top(f):
print('marking', f)
f.outermost = True
##@wraps(f)
return f

@mark_as_top
@render('a')
@render('b')
@render('c')
@render('d')
def f():
pass

f()

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


[issue3058] Let SimpleXMLRPCServer pass client_address to called functions.

2009-05-16 Thread samwyse

samwyse samw...@gmail.com added the comment:

A more general solution would be to pass the RequestHandler instance as
a parameter to the dispatch function.  This would allow the function to
pick out more than just the client address.  To avoid breaking
pre-existing code, this should be made optional, perhaps by adding a
keyword to the register_function method.  Something like this:

  def __init__(...):
self._include_request = set()
  def register_function(..., include_request=False):
self._include_request.add(method)

Later, the dispatch function would be invoked like this:
  kwds = {}
  if method in self._include_request:
kwds.update(request=self)
  [...]
  return self.instance._dispatch(method, params, **kwds)
  [...]
  return func(client_address, *params, **kwds)

--
nosy: +samwyse

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue3058
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



Re: creating classes with mix-ins

2009-05-12 Thread samwyse
On May 11, 9:01 pm, Carl Banks pavlovevide...@gmail.com wrote:
 On May 11, 11:16 am, samwyse samw...@gmail.com wrote:

  Should I use a class decorator, or a metaclass?

 Here's the thing: unless you have advance knowledge of the methods
 defined by self.blog, you can't get the attr_list at class definition
 time, which means neither the metaclass nor the decorator would be a
 good approach.  If that's the case, you should define newPost,
 editPost, and whatever other methods of self.blog as ordinary
 attributes of self, within the init function.  boilerplate would be
 the same except you would pass self to it and allow template to use it
 from its nested scope (it would no longer be an instance method since
 it's an ordinary attribute).

 If you do know what the methods of self.blog will be, then that's
 where you get attr_list from.  So, for instance, if blogHandler always
 returns an object of type Blog, then you could inspect Blog's type
 dict to see what methods are defined in it; in fact you probably want
 to check the whole MRO for Blog, like this (untested):

 attr_list = []
 for cls in Blog.__mro__:
     for value in cls.__dict__:
         if is_wrapped_method(value):
             attr_list.append(value)

 A metaclass is probably overkill to assign the wrapped blog methods.
 I probably wouldn't even bother with the decorator, and just write the
 loop after the class definition.  Then you can use MetaBlog directly
 for klass.

 class MetaBlog(object):
     ...

 for what in attr_list:
     setattr(MetaBlog, what, boilerplate(what))

 If it were the kind of thing I found myself doing often I'd refactor
 into a decorator.

Unfortunately, 'boilerplate()' uses the handlers that I provide when
MetaBlog is instantiated.  I tried the following, but it didn't work
(for reasons that were obvious in retrospect).

def instantiate_template(m_name, instance):
isAuthorised = instance.security.isAuthorised
method_to_wrap = getattr(instance.blog, m_name)
def template(self, which, username, password, *args):
if not isAuthorised(username, password, which, m_name):
raise Exception('Unauthorised access')
return method_to_wrap(which, *args)
template.__name__ = method_to_wrap.__name__
template.__doc__ = method_to_wrap.__doc__
return template

class MetaWeblog(object):
def __init__(self,
 securityHandler=SimpleSecurityHandler,
 blogHandler=SimpleBlogHandler):
self.security = securityHandler()
self.blog = blogHandler()
# from http://www.xmlrpc.com/metaWeblogApi
m_prefix = 'metaWeblog.'
m_list = ('newPost', 'editPost', 'getPost', 'newMediaObject',
  'getCategories', 'getRecentPosts', )

# Here's where things fell apart
for m_name in m_list:
dotted_name = m_prefix + m_name
method = instantiate_template(m_name, self)
setattr(self, dotted_name, method)

So, I replaced that last for-loop with this:

# Since we're about to monkey-patch the class, we should
# make certain that all instances use the same handlers.
handlers = (self.security, self.blog)
try:
assert getattr(self.__class__, '_handlers') == handlers
except AttributeError:
for m_name in m_list:
dotted_name = m_prefix + m_name
method = instantiate_template(m_name, self)
setattr(self.__class__, dotted_name, method)
setattr(self.__class__, '_handlers', handlers)

This is good enough for now, since I can't conceive of a reason why
MetaBlog would be instantiated more than once.  If it were, on the
other hand, it would probably be because you wanted to use different
handlers.  In that case, I think I'd want to use a class factory,
something like this:
server.register_instance(
MetaWeblogFactory(securityHandler, blogHandler)()
)

Anyway, thanks for getting me over a conceptual hump.
-- 
http://mail.python.org/mailman/listinfo/python-list


creating classes with mix-ins

2009-05-11 Thread samwyse
I'm writing a class that derives it's functionality from mix-ins.
Here's the code:

def boilerplate(what):   # This used to be a decorator, but all of
the
##what = f.__name__  # function bodies turned out to be
'pass'.
'Validate the user, then call the appropriate plug-in.'
def template(self, which, username, password, *args):
if not self.security.isAuthorised(username, password,
which, what):
raise Exception('Unauthorised access')
return getattr(self.blog, what)(which, *args)
template.__name__ = what
template.__doc__ = getattr(self.blog, what).__doc__
return template

class MetaWeblog(object):
def __init__(self,
 securityHandler=SimpleSecurityHandler,
 blogHandler=SimpleBlogHandler):
self.security = securityHandler()
self.blog = blogHandler()
newPost = boilerplate('newPost')
editPost = boilerplate('editPost')
getPost = boilerplate('getPost')
# etc, etc, etc

I'd like to replace the method definitions with a loop:
for what in attr_list:
setattr(klass, what, boilerplate(what))

That begs the question of where I define 'klass' and 'attr_list'.
Should I use a class decorator, or a metaclass?  In favor of
decorators is that I can see how to do it; in favor of a metaclass is
that I get to learn how to use them.  ;-)  What are the other pros and
cons for each choice?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: creating classes with mix-ins

2009-05-11 Thread samwyse
On May 11, 1:16 pm, samwyse samw...@gmail.com wrote:
 I'm writing a class that derives it's functionality from mix-ins.

While waiting, I gave a try at using class decorators.  Here's what I
came up with:

def add_methods(*m_list, **kwds):
def wrapper(klass):
for m_name in m_list:
def template(self, which, username, password, *args):
if not self.security.isAuthorised(username, password,
which, m_name):
raise Exception('Unauthorised access')
return getattr(self.blog, m_name)(which, *args)
dotted_name = kwds.get('prefix', '') + m_name
template.__name__ = dotted_name
template.__doc__ = dotted_name
setattr(klass, dotted_name, template)
return klass
return wrapper

@add_methods('newPost', 'editPost', 'getPost', 'newMediaObject',
 'getCategories', 'getRecentPosts',
 prefix='metaWeblog.')
class MetaWeblog(object):
def __init__(self,
 securityHandler=SimpleSecurityHandler,
 blogHandler=SimpleBlogHandler):
self.security = securityHandler()
self.blog = blogHandler()

if __name__ == '__main__':
server = SimpleXMLRPCServer((localhost, 8080))
server.register_instance(MetaWeblog())
server.register_introspection_functions()
server.serve_forever()

The problem that I'm having is that when I call newPost,
SimpleBlogHandler.getRecentPosts gets invoked.  Apparently add_methods
isn't generating unique templates.  I think I need to move 'template'
into another function, but I'm starting to wonder if metaclasses might
work better.  Any ideas?
-- 
http://mail.python.org/mailman/listinfo/python-list


How do I change the behavior of the 'python-docs' action in IDLE?

2009-04-16 Thread samwyse
In the Windows version of Python 2.5, pressing F1 brought up the
python.chm file.  I've just installed 2.6, and the same action opens
http://www.python.org/doc/current/.  I'd prefer the former behavior.
I know how to change the key bindings in config-keys.def, but I think
I want to change the action, not the key binding.  Thanks.
--
http://mail.python.org/mailman/listinfo/python-list


Re: sharing/swapping items between lists

2009-04-15 Thread samwyse
On Apr 14, 7:01 pm, Aaron Brady castiro...@gmail.com wrote:
 Here is an idea.  Create a list of all possible pairs, using
 itertools.combinations.  You'll notice everyone gets equal play time
 and equal time against each other on a pair-by-pair basis.  Then, call
 random.shuffle until one player isn't playing on two courts in one
 day.

 It has the disadvantage that you might end up with player A playing
 lots early on and rarely at the end, and B rarely early on and lots at
 the end.  Perhaps you could generate a few to several correct
 solutions, then choose the most evenly distributed.  Does this make
 sense?

Here's my idea:  generate all possible pairs:
 import itertools
 players = [chr(c) for c in xrange(ord('a'),ord('z')+1)]
 all_pairs = list(itertools.combinations(players,2))

partition the list:
 def choose_nonoverlapping(pairs):
chosen, leftover, used = list(), list(), list()
for p in pairs:
a, b = p
if a in used or b in used:
leftover.append(p)
else:
chosen.append(p)
used.append(a)
used.append(b)
return chosen, leftover

 court_count = 10
 week_count = 10
 pairs = all_pairs
 for week in xrange(week_count):
print 'week', week+1
this_week, pairs = choose_nonoverlapping(pairs)
print ', '.join(map(lambda t: ' vs '.join(t), this_week
[:court_count]))
pairs[0:0] = this_week[court_count:]


week 1
a vs b, c vs d, e vs f, g vs h, i vs j, k vs l, m vs n, o vs p, q vs
r, s vs t
week 2
u vs v, w vs x, y vs z, a vs c, b vs d, e vs g, f vs h, i vs k, j vs
l, m vs o
week 3
n vs p, q vs s, r vs t, a vs d, b vs c, e vs h, f vs g, i vs l, j vs
k, m vs u
week 4
o vs v, w vs y, x vs z, a vs e, b vs f, c vs g, d vs h, i vs m, j vs
n, k vs p
week 5
l vs q, r vs s, t vs u, a vs f, b vs e, c vs h, d vs g, i vs n, j vs
m, k vs o
week 6
p vs v, w vs z, x vs y, a vs g, b vs h, c vs e, d vs f, i vs o, j vs
q, k vs m
week 7
l vs n, r vs u, a vs h, b vs g, c vs f, d vs e, i vs p, j vs o, k vs
q, m vs s
week 8
t vs v, a vs i, b vs j, c vs k, d vs l, e vs m, f vs n, g vs o, h vs
p, q vs u
week 9
r vs w, s vs x, a vs j, b vs i, c vs l, d vs k, e vs n, f vs m, g vs
p, h vs o
week 10
q vs t, u vs y, v vs z, a vs k, b vs l, c vs i, d vs j, e vs o, f vs
p, g vs m
--
http://mail.python.org/mailman/listinfo/python-list


Re: sharing/swapping items between lists

2009-04-15 Thread samwyse
On Apr 15, 8:13 am, Aaron Brady castiro...@gmail.com wrote:
 On Apr 15, 6:57 am, samwyse samw...@gmail.com wrote:

  Here's my idea:  generate all possible pairs:

   import itertools
   players = [chr(c) for c in xrange(ord('a'),ord('z')+1)]
   all_pairs = list(itertools.combinations(players,2))

  partition the list:
   def choose_nonoverlapping(pairs):
  chosen, leftover, used = list(), list(), list()
  for p in pairs:
  a, b = p
  if a in used or b in used:
  leftover.append(p)
  else:
  chosen.append(p)
  used.append(a)
  used.append(b)
  return chosen, leftover

   court_count = 10
   week_count = 10
   pairs = all_pairs
   for week in xrange(week_count):
  print 'week', week+1
  this_week, pairs = choose_nonoverlapping(pairs)
  print ', '.join(map(lambda t: ' vs '.join(t), this_week
  [:court_count]))
  pairs[0:0] = this_week[court_count:]

 snip

 Your idea arrives at a sub-optimal solution on players= 'abcdef',
 court_count= 3.

 Correct, by hand (5 weeks):

 ab cd ef
 ac be df
 ad ce bf
 ae bd cf
 af bc de

 Program (7 weeks):

[snip]

 However, you do correctly arrive at all the combinations, in better
 than the naive 'one pair per week' solution.  Further, you produced
 the correct solution for players= 'abcdef', for court_count= 1 and
 court_count= 2, which I also tested.  Damage report?

It does work better when there are a limited number of courts, but
since that was in the original description, I didn't worry too much.

One could product several random shuffles of the list of combinations
and see which produced the shortest list of results.  That would add
indeterminancy without guaranteeing an optimal result.  But I think
that other people have algorithms for that case, so I'm not too
worried.

I've tried generalizing to competitions  with more than two player
(e.g. the Pinewood Derby, where up four cars are in each heat), but
the algorithm falls apart, mostly due to the way
itertools.combinations returns its results.
--
http://mail.python.org/mailman/listinfo/python-list


Re: sharing/swapping items between lists

2009-04-15 Thread samwyse
On Apr 15, 8:56 am, Aaron Brady castiro...@gmail.com wrote:

 The randomizing solution isn't quite suitable for 16 teams.  With 5
 teams/1 court, and 5 teams/2 courts, 6 teams/2 courts, the solution
 comes within seconds.  For 7 teams/3 courts, the solution takes a few
 minutes.

7 teams/3 courts is the same as 8 teams/4 courts, where the extra
player is named bye.  In other words, it's an uncontrained problem.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Problems Returning an HTTP 200 Ok Message

2008-07-14 Thread samwyse
On Jul 10, 4:10 pm, Guy Davidson [EMAIL PROTECTED] wrote:

 I try to send the following message, using the socket.send() command:

 'HTTP/1.1 200 OK\r\nDate: Thu, 10 July 2008 14:07:50 GMT\r\nServer:
 Apache/2.2.8 (Fedora)\r\nX-Powered-By: PHP/5.2.4\r\nContent-Length: 4\r
 \nConnection: close\r\nContent-Type: text/html; charset=UTF-8\r\n\r
 \n[0]\n'

 However, when I snoop on the packets in wireshark, here's what I see:

 HTTP/1.1 200 Ok:

 HTTP/1.1 200 OK
 Date: Wed, 09 July 2008 14:55:50 GMT
 Server: Apache/2.2.8 (Fedora)
 X-Powered-By:

 Continuation or non-HTTP traffic:

 PHP/5.2.4
 Content-Length: 4
 Connection: close
 Content-Type: text/html; charset=UTF-8

 [0]

 It splits into two packages, which the meter can't read, and the
 communication breaks down there.

OK, it looks like a single TCP segment is being sent by you (which is
consistent with only one socket.send() command being needed), but
something along the way to the meter is using an MTU (Maximum
Transmission Unit) of about 100 bytes, producing two IP datagrams.
How many hops are there between the PC and the meter?  Are you
sniffing on the same subnet as the PC, the meter, or somewhere in
between?  MTU is normally set to about 1500 (and MSS is generally
MTU-40), but you can generally change these values.

You should be able to set the do-not-fragment flag on your IP packets,
but that may cause them to get dropped instead of sent.  You could
also try setting a smaller ICP MSS (Maximum Segment Size), which the
meter might be able to assemble, even if it can't handle fragmented IP
datagrams.  Try 
http://www.cisco.com/en/US/tech/tk870/tk877/tk880/technologies_tech_note09186a008011a218.shtml#prob_desc
for more help.

You really need to get a networking guru involved if you want to go
down this path.  I used to be one, but that was years ago.  Or, you
can take Gabriel Genellina's advice and try coding smaller responses.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Problems Returning an HTTP 200 Ok Message

2008-07-14 Thread samwyse
On Jul 11, 3:46 am, Gabriel Genellina [EMAIL PROTECTED]
wrote:

 As Guy Davidson has already pointed out, this is a problem in the meter  
 TCP implementation, and you should ask the vendor to fix it.

That would have been me, not Guy.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Loading just in time

2008-07-10 Thread samwyse
On Jul 10, 9:45 am, D'Arcy J.M. Cain [EMAIL PROTECTED] wrote:
 I am trying to create a utility module that only loads functions when
 they are first called rather than loading everything.  I have a bunch
 of files in my utility directory with individual methods and for each I
 have lines like this in __init__.py:

 def calc_tax(*arg, **name):
     from calc_tax import calc_tax as _func_
     calc_tax = _func_
     return _func_(*arg, **name)

This doesn't do what you think.  The line calc_tax = _func_ is
probably modifying a local variable that is then thrown away.  I've
got a slightly different (simpler) version to illustrate:

=== main.py ===

def calc_tax(*arg, **name):
from calc_tax import calc_tax as _func_
#global calc_tax
calc_tax = _func_
print '_func_ is', repr(_func_)
print 'calc_tax is', repr(calc_tax)
return _func_(*arg, **name)

print 'before: calc_tax is', repr(calc_tax)
result = calc_tax()
print 'after: calc_tax is', repr(calc_tax)

=== calc_tax.py ===

def calc_tax(*arg, **name):
return 42

=== end of files ===

Running main.py gives this:

  before: calc_tax is function calc_tax at 0x015049F0
  _func_ is function calc_tax at 0x014950B0
  calc_tax is function calc_tax at 0x014950B0
  after: calc_tax is function calc_tax at 0x015049F0

Note that the value of calc_test is the same in the first and last
lines.

If you uncomment the line #global calc_tax and run it again, you get
this:

  before: calc_tax is function calc_tax at 0x01504A30
  _func_ is function calc_tax at 0x014950B0
  calc_tax is function calc_tax at 0x014950B0
  after: calc_tax is function calc_tax at 0x014950B0

Interestingly, neither version gives me a TypeError, no matter how
many times I call calc_tax.

(BTW, you might want to look up Memoization; it's very similar to what
you want to do, and might give you a way to more efficiently code
things.)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Problems Returning an HTTP 200 Ok Message

2008-07-10 Thread samwyse
On Jul 10, 1:50 pm, Guy Davidson [EMAIL PROTECTED] wrote:
 Hi Folks,

 I'm having some issues with an small socket based server I'm writing,
 and I was hoping I could get some help.

 My code (attached below) us supposed to read an HTTP Post message
 coming from a power meter, parse it, and return a proper HTTP 200 Ok
 message. The problem is that the socket fails to send the entire
 message as one message, creating a fragmented message which the power
 meter then fails to read and accept.

 Is there any way to force the socket to send the entire message at
 once? Am I doing anything wrong? Is there an easier way to implement
 this functionality?

By 'message', do you mean a single IP datagram?  In general, the
answer is no.  Each call to 'connection.send()' will (in general, see
the next paragraph) transmit as much data as will fit into a single IP
datagram, given the current MTU for the transmission circuit.  The
fact that you're calling it in a loop indicates that the data being
sent may be larger than will fit into a datagram.

Or, by 'message', do you mean a single TCP segment?  Again, the answer
is no.  Your network stack will try to make the TCP segments the right
size to fit within a single IP datagram, leading to the same result as
above.

From your description, I get the feeling that your power meter has a
broken network stack, and you're trying to program around it.  You
need to repair the meter.
--
http://mail.python.org/mailman/listinfo/python-list


Re: numeric emulation and __pos__

2008-07-09 Thread samwyse
On Jul 8, 12:34 pm, Ethan Furman [EMAIL PROTECTED] wrote:

 Anybody have an example of when the unary + actually does something?
 Besides the below Decimal example.  I'm curious under what circumstances
 it would be useful for more than just completeness (although
 completeness for it's own sake is important, IMO).

Well, as in Decimal, it would be a good operator to use for
canonization.  Let's say you implement complex numbers as an angle and
radius.  Then, unary plus could be used to normalize the angle to +/-
Pi and the radius to a positive number (by inverting the angle).
--
http://mail.python.org/mailman/listinfo/python-list


Re: Logging to zero or more destinations

2008-07-09 Thread samwyse
On Jul 8, 3:01 pm, Rob Wolfe [EMAIL PROTECTED] wrote:
 samwyse [EMAIL PROTECTED] writes:

  P.S.  I tried researching this further by myself, but the logging
  module doesn't come with source (apparently it's written in C?) and I
  don't have the time to find and download the source to my laptop.

 Hmmm... that's strange. It is a pure Python package.

 $ ls /usr/lib/python2.5/logging/
 config.py  config.pyc  handlers.py  handlers.pyc  __init__.py  __init__.pyc

 HTH,
 Rob

Oops, my bad.  I was using IDLE and tried the Open Module... command
on logging, not logging.something.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Impossible to change methods with special names of instances of new-style classes?

2008-07-09 Thread samwyse
On Jul 8, 4:56 pm, Joseph Barillari [EMAIL PROTECTED] wrote:

 My question is: did something about the way the special method names are
 implemented change for new-style classes?

Just off the top of my head, I'd guess that it's due to classes
already having a default __call__ method, used when you instatiate.
Remember, the Python compiler doesn't know the difference between
this:
  a = MyClass
  instance = a()
and this:
  a = myFunc
  result = a()
--
http://mail.python.org/mailman/listinfo/python-list


Re: re.search much slower then grep on some regular expressions

2008-07-09 Thread samwyse
On Jul 8, 11:01 am, Kris Kennaway [EMAIL PROTECTED] wrote:
 samwyse wrote:

  You might want to look at Plex.
 http://www.cosc.canterbury.ac.nz/greg.ewing/python/Plex/

  Another advantage of Plex is that it compiles all of the regular
  expressions into a single DFA. Once that's done, the input can be
  processed in a time proportional to the number of characters to be
  scanned, and independent of the number or complexity of the regular
  expressions. Python's existing regular expression matchers do not have
  this property. 

 Hmm, unfortunately it's still orders of magnitude slower than grep in my
 own application that involves matching lots of strings and regexps
 against large files (I killed it after 400 seconds, compared to 1.5 for
 grep), and that's leaving aside the much longer compilation time (over a
 minute).  If the matching was fast then I could possibly pickle the
 lexer though (but it's not).

That's funny, the compilation is almost instantaneous for me.
However, I just tested it to several files, the first containing
4875*'a', the rest each twice the size of the previous.  And you're
right, for each doubling of the file size, the match take four times
as long, meaning O(n^2).  156000*'a' would probably take 8 hours.
Here are my results:

compile_lexicon() took 0.0236021580595 secs
test('file-0.txt') took 24.8322969831 secs
test('file-1.txt') took 99.3956799681 secs
test('file-2.txt') took 398.349623132 secs

And here's my (probably over-engineered) testbed:

from __future__ import with_statement
from os.path import exists
from timeit import Timer

from Plex import *

filename = file-%d.txt

def create_files(n):
for x in range(0,n):
fname = filename % x
if not exists(fname):
print 'creating', fname
with open(fname, 'w') as f:
print f, (4875*2**x)*'a',

def compile_lexicon():
global lexicon
lexicon = Lexicon([
(Rep(AnyBut(' ='))+Str('/'),  TEXT),
(AnyBut('\n'), IGNORE),
])

def test(fname):
with open(fname, 'r') as f:
scanner = Scanner(lexicon, f, fname)
while 1:
token = scanner.read()
#print token
if token[0] is None:
break

def my_timed_test(func_name, *args):
stmt = func_name + '(' + ','.join(map(repr, args)) + ')'
t = Timer(stmt, from __main__ import +func_name)
print stmt, 'took', t.timeit(1), 'secs'

if __name__ == '__main__':
create_files(6)
my_timed_test('compile_lexicon')
for x in range(0,4):
my_timed_test('test', filename%x)
--
http://mail.python.org/mailman/listinfo/python-list


Logging to zero or more destinations

2008-07-08 Thread samwyse
In the Python 2.5 Library Reference, section 14.5.3 (Logging to
multiple destinations), an example is given of logging to both a file
and the console.  This is done by using logging.basicConfig() to
configure a log file, and then calling
logging.getLogger('').addHandler(console) to add the console.

However, in section 14.5.4 (Sending and receiving logging events
across a network), a call is made to
rootLogger.addHandler(socketHandler), and later it is stated that On
the client side, nothing is printed on the console.

Finally, back in section 14.5.2 (Basic example), it's stated that by
default, the root logger is configured to only handle messages with a
severity of WARNING or above. The message format is also a
configuration default, as is the output destination of the messages -
sys.stderr.

The only way that I can see for all three statements to be consistent
is that the root logger starts with an empty list of handlers, and
doesn't instantiate a default handler until either
logging.basicConfig()  is called, or the first time that a message is
logged.  This would also seem to imply that there's no way to use an
empty handler list (say, if you want to suppress all logging), because
the root handler will instantiate a handler for you.  Is this correct?

P.S.  I tried researching this further by myself, but the logging
module doesn't come with source (apparently it's written in C?) and I
don't have the time to find and download the source to my laptop.

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


Re: re.search much slower then grep on some regular expressions

2008-07-08 Thread samwyse
On Jul 4, 6:43 am, Henning_Thornblad [EMAIL PROTECTED]
wrote:
 What can be the cause of the large difference between re.search and
 grep?

 While doing a simple grep:
 grep '[^ =]*/' input                  (input contains 156.000 a in
 one row)
 doesn't even take a second.

 Is this a bug in python?

You might want to look at Plex.
http://www.cosc.canterbury.ac.nz/greg.ewing/python/Plex/

Another advantage of Plex is that it compiles all of the regular
expressions into a single DFA. Once that's done, the input can be
processed in a time proportional to the number of characters to be
scanned, and independent of the number or complexity of the regular
expressions. Python's existing regular expression matchers do not have
this property. 

I haven't tested this, but I think it would do what you want:

from Plex import *
lexicon = Lexicon([
(Rep(AnyBut(' ='))+Str('/'),  TEXT),
(AnyBut('\n'), IGNORE),
])
filename = my_file.txt
f = open(filename, r)
scanner = Scanner(lexicon, f, filename)
while 1:
token = scanner.read()
print token
if token[0] is None:
break
--
http://mail.python.org/mailman/listinfo/python-list


Re: numeric emulation and __pos__

2008-07-08 Thread samwyse
On Jul 7, 6:12 pm, Ethan Furman [EMAIL PROTECTED] wrote:
 Greetings, List!

 I'm working on a numeric data type for measured values that will keep
 track of and limit results to the number of significant digits
 originally defined for the values in question.

 I am doing this primarily because I enjoy playing with numbers, and also
 to get some experience with unit testing.

 At this point I have the __init__ portion finished, and am starting on
 the various operator functions.

 Questions for the group:

 1) Any reason to support the less common operators?
         i.e. , , , ^, |

 2) What, exactly, does .__pos__() do?  An example would help, too.

1)  Those make much less sense for non-integers.  I'd say skip them.

2)  It's an overridable no-op that implements the unary plus
operator.  Unary plus returns its value unchanged, as does __pos__.
--
http://mail.python.org/mailman/listinfo/python-list


[issue1491] BaseHTTPServer incorrectly implements response code 100

2008-05-10 Thread samwyse

samwyse [EMAIL PROTECTED] added the comment:

Although any given implementation of an HTTP server is likely to serve
up its headers in a predicable, repeatable, order, I don't think that
we should specify a particular order in the test suite.  Section 4.2
of RFC 2616 specifically states, The order in which header fields
with differing field names are received is not significant.  So, I
dislike these (and similar) statements in the patch:

+self.assertTrue(result[1].startswith('Server: '))
+self.assertTrue(result[2].startswith('Date: '))
+self.assertTrue(result[3].startswith('Content-Type: '))

I think it would be better to say this:

+self.assertEqual(sum(r.startswith('Server: ') for r in
result[1:-1]), 1)
+self.assertEqual(sum(r.startswith('Date: ') for r in result[1:-1]), 1)
+self.assertEqual(sum(r.startswith('Content-Type: ') for r in
result[1:-1]), 1)

or even this:

+# Verify that certain message headers occur exactly once.
+for fieldName in 'Server: ', 'Date: ', 'Content-Type: ':
+self.assertEqual(sum(r.startswith(fieldName) for r in
result[1:-1]), 1)

(Note that in test_with_continue_1_1, you'd need to say result[2:-1].)

On Sat, May 10, 2008 at 2:34 PM, Jeremy Thurgood [EMAIL PROTECTED] wrote:

 Jeremy Thurgood [EMAIL PROTECTED] added the comment:

 Added handling for Expect: 100-continue header to
 BaseHTTPRequestHandler. By default, any request that has this header
 gets a 100 Continue response (with no other headers) before
 do_WHATEVER() is called. By overriding handle_expect_100(), you can
 reject incoming requests instead of sending a 100 Continue if you so desire.

 Refactoring as per comments above was also performed.

 Note: This patch changes the default behaviour in the case where both
 the client and the server claim to support HTTP/1.1 from doing nothing
 in the case of an Expect: 100-continue header on the request to
 sending a 100 Continue response and then completing the request as normal.

 --
 keywords: +patch
 nosy: +jerith
 Added file: http://bugs.python.org/file10269/BaseHTTPServer_continue.patch

 __
 Tracker [EMAIL PROTECTED]
 http://bugs.python.org/issue1491
 __


__
Tracker [EMAIL PROTECTED]
http://bugs.python.org/issue1491
__
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1491] BaseHTTPServer incorrectly implements response code 100

2008-05-10 Thread samwyse

samwyse [EMAIL PROTECTED] added the comment:

In the attached file, I've refactored the entire
BaseHTTPRequestHandlerTestCase class.  In doing so, I couldn't help but
notice that we're expecting HTTP/1.1 responses when sending HTTP/1.0
requests.  RFC 2616 is unclear about whether this is legitimate, but I'm
not going to tackle it tonight.

Added file: http://bugs.python.org/file10278/BaseHTTPRequestHandlerTestCase.py

__
Tracker [EMAIL PROTECTED]
http://bugs.python.org/issue1491
__
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



  1   2   >