[issue7160] Crash when returning a 64-bit char pointer in Python 2.6.3 ctypes

2009-10-19 Thread Michael J. Fromberger

Michael J. Fromberger michael.j.fromber...@gmail.com added the comment:

Thank you for setting me straight. 

I see now that I misunderstood the scope of `CFUNCTYPE`, as I was using 
it as a general wrapper when in fact it's only needed for callbacks. 
Mistakenly, I inferred from reading section 16.15.2.4 of the ctypes 
manual [1] that it would be necessary to create prototype wrappers for 
calls into the foreign library as well. Obviously that is not the case, 
since your described solution works fine.

[1] http://www.python.org/doc/2.6.3/library/ctypes.html#function-
prototypes

--
status: open - closed

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



[issue7160] Crash when returning a 64-bit char pointer in Python 2.6.3 ctypes

2009-10-17 Thread Michael J. Fromberger

New submission from Michael J. Fromberger michael.j.fromber...@gmail.com:

A segmentation fault is generated in _ctypes.so when calling a function that 
returns a char pointer on a system 
with 64-bit pointer types.  The attached crash dump is from a Python 2.6.3 
built from MacPorts (port install 
python26 +no_tkinter), but the same behaviour occurs with the Python 2.6.1 
installed by Apple.

To reproduce, build the attached sample program (testlib.c):

% gcc -Wall -c testlib.o
% ld -dylib -o testlib.so testlib.o

Then, in Python:

# Common setup for each of the cases below.
 from ctypes import *
 lib = CDLL('testlib.so')

# Case 1: Integer return value (no crash).
 get_value = CFUNCTYPE(c_int)(lib.get_value)
 get_value()
12345

# Case 2: Pointer argument value (no crash).
 buf = create_string_buffer(256)
 copy_message = CFUNCTYPE(None, c_char_p)(lib.copy_message)
 copy_message(buf)

# Case 3: Pointer return value (crash).
 get_message = CFUNCTYPE(c_char_p)(lib.get_message)
 get_message()
Segmentation fault

-- System information:

% uname -a
MacOS 10.6.1
Darwin gorion.local 10.0.0 Darwin Kernel Version 10.0.0: Fri Jul 31 22:47:34 
PDT 2009; root:xnu-
1456.1.25~1/RELEASE_I386 i386

% python
Python 2.6.3 (r263:75183, Oct 17 2009, 01:49:30) 
[GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin

% gcc --version
i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5646) (dot 1)

--
assignee: theller
components: ctypes
files: testlib.c
messages: 94181
nosy: creachadair, theller
severity: normal
status: open
title: Crash when returning a 64-bit char pointer in Python 2.6.3 ctypes
type: crash
versions: Python 2.6
Added file: http://bugs.python.org/file15154/testlib.c

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



[issue7160] Crash when returning a 64-bit char pointer in Python 2.6.3 ctypes

2009-10-17 Thread Michael J. Fromberger

Changes by Michael J. Fromberger michael.j.fromber...@gmail.com:


Added file: http://bugs.python.org/file15155/crash-report.txt

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



[issue7160] Crash when returning a 64-bit char pointer in Python 2.6.3 ctypes

2009-10-17 Thread Michael J. Fromberger

Michael J. Fromberger michael.j.fromber...@gmail.com added the comment:

I believe this error occurs because a pointer value is being truncated to 
32 bits.  The exception code is

KERN_INVALID_ADDRESS at 0x002fe020

If you add a diagnostic printout to the body of get_message(), you will 
see that its return value is 0x1002fe020, so in other words, the high-
order word 0x0001 is being discarded somewhere.

--

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



[issue5809] No such file or directory with framework build under MacOS 10.4.11

2009-05-08 Thread Michael J. Fromberger

Michael J. Fromberger michael.j.fromber...@gmail.com added the comment:

Ned Deily writes:
 --enable-framework and --enable-shared are mutually exclusive options. 

Aha, I did not realize that, though I suppose in retrospect it should have 
been obvious.  Removing --enable-shared from my build configuration does 
enable me to build with --enable-framework selected.

I agree that this should be either better documented, or checked for by 
the configurator.

--

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



[issue5809] No such file or directory with framework build under MacOS 10.4.11

2009-04-21 Thread Michael J. Fromberger

New submission from Michael J. Fromberger michael.j.fromber...@gmail.com:

Checkout:
http://svn.python.org/projects/python/tags/r262

Configure:
env CFLAGS=-I/opt/local/include LDFLAGS=-L/opt/local/lib sh 
./configure --enable-framework --enable-shared --enable-readline

Build:
make

The configuration step completes successfully, and the build proceeds; 
however, the following error occurs:

gcc -L/opt/local/lib -u _PyMac_Error 
Python.framework/Versions/2.6/Python -o python.exe \
Modules/python.o \
-L. -lpython2.6 -ldl  

This appears to be a problem in the definition of the LINKFORSHARED 
variable in the Makefile, but I am not sure what the intended meaning of 
that variable is, so I'm not sure what the fix should be.

A complete make trace is attached.

--
components: Build
files: makedump.txt
messages: 86245
nosy: creachadair
severity: normal
status: open
title: No such file or directory with framework build under MacOS 10.4.11
type: compile error
versions: Python 2.6
Added file: http://bugs.python.org/file13728/makedump.txt

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



[issue5809] No such file or directory with framework build under MacOS 10.4.11

2009-04-21 Thread Michael J. Fromberger

Michael J. Fromberger michael.j.fromber...@gmail.com added the comment:

Sorry, I managed to not copy the error message.  It is:

i686-apple-darwin8-gcc-4.0.1: Python.framework/Versions/2.6/Python: No 
such file or directory
make: *** [python.exe] Error 1

--

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



Re: Python Exponent Question

2007-12-17 Thread Michael J. Fromberger
In article 
[EMAIL PROTECTED],
 databyss [EMAIL PROTECTED] wrote:

 I have a simple program and the output isn't what I expect.  Could
 somebody please explain why?
 
 Here's the code:
 
 #simple program
 print v = 2
 v = 2
 print v**v = 2**2 =, v**v
 print v**v**v = 2**2**2 =, v**v**v
 print v**v**v**v = 2**2**2**2 =, v**v**v**v
 #end program
 
 Here's the output:
 
 
 v = 2
 v**v = 2**2 = 4
 v**v**v = 2**2**2 = 16
 v**v**v**v = 2**2**2**2 = 65536
 
 
 I would expect 2**2**2**2 to be 256

Python's ** operator associates to the right, not to the left; thus, 

  2 ** 2 ** 2 ** 2

... really means

  2 ** (2 ** (2 ** 2))

... and not

  ((2 ** 2) ** 2) ** 2

... as you seem to expect.  As usual, you can enforce different 
associations by explicitly including the parentheses.

Cheers,
-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Loop three lists at the same time?

2007-11-14 Thread Michael J. Fromberger
In article [EMAIL PROTECTED],
 Davy [EMAIL PROTECTED] wrote:

 Hi all,
 
 I have three lists with the same length. Is there any method to loop
 the three lists without a loop counter?
 
 Best regards,
 Davy

Davy,

Look at the zip function, 

  http://docs.python.org/lib/built-in-funcs.html

Look near the bottom of the page.

Example:
  zip([1,2,3], [4,5,6], [7,8,9])
  == [(1, 4, 7), (2, 5, 8), (3, 6, 9)]

Cheers,
-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Iteration for Factorials

2007-10-22 Thread Michael J. Fromberger
In article [EMAIL PROTECTED],
 Py-Fun [EMAIL PROTECTED] wrote:

 I'm stuck trying to write a function that generates a factorial of a
 number using iteration and not recursion.  Any simple ideas would be
 appreciated.


Well, first think about the recursive implementation:

  def fact(n):
if n  0:
  return n * fact(n - 1)
else:
  return 1

To turn this into an iterative computation, you must first get rid of 
the deferred operations -- in this case, the multiplication step after 
the recursive call to fact(n - 1).  

Since multiplication commutes, we can re-write this recursion to keep an 
accumulating parameter instead of deferring the operation:

  def fact2(n, acc = 1):
if n  0:
  return fact2(n - 1, acc * n)
else:
  return acc

This is a little bit better, because now the recursive call is in tail 
position, and so in principle no state needs to be saved across 
recursive calls:  Once the inner call to fact2 is complete, its value is 
simply returned.  But we're not done yet, because Python _does_ save 
state across recursive calls, even in this construction.

By a gentle twist of perspective, the inner call to fact2(n - 1, acc * 
n) is really just a kind of jump back to the beginning of the 
function.  In another (hypothetical) language, you might write it like 
this:

  # Not legal Python code.
  def fact3(n, acc = 1):
TOP: 
  if n  0
n = n - 1
acc = acc * n
goto TOP
  else:
return acc

Naturally, of course, Python does not provide a goto statement.  But 
it does have one that's similar:

  while TEST: BODY

is equivalent in meaning to the pseudo-code:

  X: if TEST: 
   BODY  
   goto X

Can you now see how you would re-write fact3 into legal Python code,
using this equivalence?  Once you have done so, you will also be able to
get rid of the extra accumulating parameter, and then you will have what 
you wanted.

I hope this helps.

Cheers,
-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Last iteration?

2007-10-19 Thread Michael J. Fromberger
 additional setup.

Cheers,
-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sorting a list numbers stored as strings

2007-09-24 Thread Michael J. Fromberger
In article [EMAIL PROTECTED],
 [EMAIL PROTECTED] wrote:

 hi,
 
 I have the following list -
 
 [1, 11, 2, 22]
 
 how do I sort it like this -
 
 [1, 2, 11, 22]
 
 thanks,
 
 aine

Try:

  lst.sort(key = lambda s: int(s))

Assuming, of course, that lst is your original list; this will sort it 
in place.  Also, you should probably read about the sorted function 
here:

 http://docs.python.org/lib/built-in-funcs.html

Cheers,
-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: frange() question

2007-09-21 Thread Michael J. Fromberger
In article [EMAIL PROTECTED],
 George Trojan [EMAIL PROTECTED] wrote:

 A while ago I found somewhere the following implementation of frange():
 
 def frange(limit1, limit2 = None, increment = 1.):
  
  Range function that accepts floats (and integers).
  Usage:
  frange(-2, 2, 0.1)
  frange(10)
  frange(10, increment = 0.5)
  The returned value is an iterator.  Use list(frange) for a list.
  
  if limit2 is None:
  limit2, limit1 = limit1, 0.
  else:
  limit1 = float(limit1)
  count = int(math.ceil(limit2 - limit1)/increment)
  return (limit1 + n*increment for n in range(count))
 
 I am puzzled by the parentheses in the last line. Somehow they make 
 frange to be a generator:
   print type(frange(1.0, increment=0.5))
 type 'generator'
 But I always thought that generators need a keyword yield. What is 
 going on here?

Hi, George,

The expression returned is a generator expression, 

  return (limit1 + n*increment for n in range(count))

Thus, although frange itself is not written as a generator, it does 
return a generator as its result.  The syntax is like that of list 
comprehensions; see:

  http://docs.python.org/ref/genexpr.html

Cheers,
-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Parsing problems: A journey from a text file to a directory tree

2007-09-19 Thread Michael J. Fromberger
Hi, John,

Your comments below are all reasonable.  However, I would like to point 
out that the purpose of my example was to provide a demonstration of an 
algorithm, not an industrial-grade solution to every aspect of the 
original poster's problem.  I am confident the original poster can deal 
with these aspects of his problem space on his own.

In article [EMAIL PROTECTED],
 John Machin [EMAIL PROTECTED] wrote:

 [...]
  .while new_level  len(state):
  .state.pop()
 
 Hmmm ... consider rewriting that as the slightly less obfuscatory
 
 while len(state)  new_level:
 state.pop()

This seems to me to be an aesthetic consideration only; I'm not sure I 
understand your rationale for reversing the sense of the comparison.  
Since it does not change the functionality, it's hardly worthy of 
complaint, but I don't see any improvement, either.

 A warning message if there are too many - characters might be a good
 idea:
 
 [foo]
 |-bar
 |-zot
 |---plugh

Perhaps so.  Again, the original poster will have to decide what should 
be the correct response to input of this sort; at present, the 
implementation is tolerant of such variations, without loss of 
generality.

 And if the input line matches neither regex?

I believe it should be clear that such lines are ignored.  Again, this 
is an opportunity for the original poster to determine an alternative 
response -- perhaps an exception could be raised, if that is his desire.  
The problem specification did not constrain this case.

  To call this, pass a file-like object to parse_folders(), e.g.:
 
  test1 = '''
  [New client].
 
 Won't work with the dot on the end.

My mistake.  The period was a copy-and-paste artifact, which I missed.

Cheers,
-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Parsing problems: A journey from a text file to a directory tree

2007-09-18 Thread Michael J. Fromberger
In article [EMAIL PROTECTED],
 Martin M. [EMAIL PROTECTED] wrote:

 Hi everybody,
 
 Some of my colleagues want me to write a script for easy folder and
 subfolder creation on the Mac.
 
 The script is supposed to scan a text file containing directory trees
 in the following format:
 
 [New client]
 |-Invoices
 |-Offers
 |--Denied
 |--Accepted
 |-Delivery notes
 
 As you can see, the folder hierarchy is expressed by the amounts of
 minuses, each section header framed by brackets (like in Windows
 config files).
 
 After the scan process, the script is supposed to show a dialog, where
 the user can choose from the different sections (e.g. 'Alphabet',
 'Months', 'New client' etc.). Then the script will create the
 corresponding folder hierarchy in the currently selected folder (done
 via AppleScript).
 
 But currently I simply don't know how to parse these folder lists and
 how to save them in an array accordingly.
 
 First I thought of an array like this:
 
 dirtreedb = {'New client': {'Invoices': {}, 'Offers': {'Denied': {},
 'Accpeted': {}}, 'Delivery notes': {}}}
 
 But this doesn't do the trick, as I also have to save the hierarchy
 level of the current folder as well...
 
 Argh, I really don't get my head around this problem and I need your
 help. I have the feeling, that the answer is not that complicated, but
 I just don't get it right now...

Hello, Martin,

A good way to approach this problem is to recognize that each section of 
your proposed configuration represents a kind of depth-first traversal 
of the tree structure you propose to create.  Thus, you can reconstruct 
the tree by keeping track at all times of the path from the root of 
the tree to the current location in the tree.

Below is one possible implementation of this idea in Python.  In short, 
the function keeps track of a stack of dictionaries, each of which 
represents the contents of some directory in your hierarchy.  As you 
encounter |-- lines, entries are pushed to or popped from the stack 
according to whether the nesting level has increased or decreased.

This code is not heavily tested, but hopefully it should be clear:

.import re
.
.def parse_folders(input):
.Read input from a file-like object that describes directory
.structures to be created.  The input format is:
.
.[Top-level name]
.|-Subdirectory1
.|--SubSubDirectory1
.|--SubSubDirectory2
.|---SubSubSubDirectory1
.|-Subdirectory2
.|-Subdirectory3
.
.The input may consist of any number of such groups.  The result is
.a dictionary structure in which each key names a directory, and
.the corresponding value is a dictionary structure showing the
.contents of that directory, possibly empty.
.
.
.# This expression matches header lines, defining a new section.
.new_re  = re.compile(r'\[([\w ]+)\]\s*$')
.
.# This expression matches nesting lines, defining subdirectories.
.more_re = re.compile(r'(\|-+)([\w ]+)$')
.
.out = {}# Root:  Maps section names to subtrees.
.state = [out]   # Stack of dictionaries, current path.
.
.for line in input:
.m = new_re.match(line)
.if m:   # New section begins here...
.key = m.group(1).strip()
.out[key] = {}
.state = [out, out[key]]
.continue
.
.m = more_re.match(line)
.if m:   # Add a directory to an existing section
.assert state
.
.new_level = len(m.group(1))
.key = m.group(2).strip()
.
.while new_level  len(state):
.state.pop()
.
.state[-1][key] = {}
.state.append(state[-1][key])
.
.return out

To call this, pass a file-like object to parse_folders(), e.g.:

test1 = '''
[New client].
|-Invoices
|-Offers
|--Denied
|--Accepted
|---Reasons
|---Rhymes
|-Delivery notes
'''

from StringIO import StringIO
result = parse_folders(StringIO(test1))

As the documentation suggests, the result is a nested dictionary 
structure, representing the folder structure you encoded.  I hope this 
helps.

Cheers,
-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: fcntl problems

2007-08-31 Thread Michael J. Fromberger
In article [EMAIL PROTECTED],
 mhearne808[insert-at-sign-here]gmail[insert-dot-here]com 
 [EMAIL PROTECTED] wrote:

 Looking at my flock(3) man page, I'm guessing that 35 is the error
 code for EWOULDBLOCK.  Which system header file am I supposed to look
 in to figure that magic number out?

On a MacOS system, you can find them in /usr/include/sys/errno.h
On a Linux system, try /usr/include/asm-generic/errno.h

However, if you're writing in Python, you will probably have an easier 
time using the errno module, e.g., 

 ] import errno
 ] errno.errorcode[35]
 'EDEADLOCK'

Note that some codes have multiple names (e.g., EAGAIN and EWOULDBLOCK) 
so that this lookup may not return exactly the name you're expecting.

Cheers,
-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: tempfile.mkstemp and os.fdopen

2007-08-28 Thread Michael J. Fromberger
In article [EMAIL PROTECTED],
 billiejoex [EMAIL PROTECTED] wrote:

 Hi there.
 I'm trying to generate a brand new file with a unique name by using
 tempfile.mkstemp().
 In conjunction I used os.fdopen() to get a wrapper around file
 properties (write  read methods, and so on...) but 'name' attribute
 does not contain the correct file name. Why?
 
  import os
  import tempfile
  fileno, name = tempfile.mkstemp(prefix='ftpd.', dir=os.getcwd())
  fd = os.fdopen(fileno, 'wb')
  fd.name
 fdopen
 
 Moreover, I'd like to know if I'm doing fine. Does this approach avoid
 race conditions or other types of security problems?
 
 Thanks in advance

In brief, since os.fdopen() only has access to the file descriptor, it 
does not have a convenient way to obtain the file's name.  However, you 
might also want to look at the TemporaryFile and NamedTemporaryFile 
classes in the tempfile module -- these expose a file-like API, 
including a .name attribute.

Assuming tempfile.mkstemp() is implemented properly, I think what you 
are doing should be sufficient to avoid the obvious file-creation race 
condition.

Cheers,
-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Image manipulation

2007-08-28 Thread Michael J. Fromberger
In article [EMAIL PROTECTED],
 Slava [EMAIL PROTECTED] wrote:

 I need to add dynamic text to animated GIF images.
 What is a best way to do it?
 
 Thanks.

One possibility is the GD library, which supports animated GIF.  There 
is a Python binding:

 http://newcenturycomputers.net/projects/gdmodule.html

I do not know, however, whether or not the Python wrapper supports the 
animated GIF portions of the library.  You'll probably have to do some 
digging.

Cheers,
-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Regular Expression Grouping

2007-08-12 Thread Michael J. Fromberger
In article [EMAIL PROTECTED],
 [EMAIL PROTECTED] wrote:

 Fairly new to this regex thing, so this might be very juvenile but
 important.
 
 I cannot understand and why 'c' constitutes a group here without being
 surrounded by ( ,) ?
 
 import re
  m = re.match(([abc])+, abc)
  m.groups()
 ('c',)
 
 Grateful for any clarity.

Hello!

I believe your confusion arises from the placement of the + operator 
in your expression.  You wrote:

  '([abc])+'

This means, in plain language, one or more groups in which each group 
contains a string of one character from the set {a, b, c}.

Contrast this with what you probably intended, to wit:

  '([abc]+)'

The latter means, in plain language, a single group containing a string 
of one or more characters from the set {a, b, c}.

In the former case, the greedy property of matching attempts to maximize 
the number of times the quantified expression is matched -- thus, you 
match the group three times, once for each character of abc, and the 
result shows you only the last occurrence of the matching. 

Compare this with the following:

] import re
] m = re.match('([abc]+)', 'abc')
] m.groups()
= ('abc',)

I suspect the latter is what you are after.

Cheers,
-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re��: ��get wikipedia source failed�� (��urrlib2��)

2007-08-07 Thread Michael J��. ��Fromberger
In article‭ [EMAIL PROTECTED],‬
‭ [EMAIL PROTECTED] wrote‭:‬

‭ ‬Hi‭,‬
‭ ‬I'm trying to get wikipedia page source with urllib2‭:‬
‭ ‬usock‭ = 
‬urllib2‭.‬urlopen‭(‬http‭://‬en.wikipedia.org/wiki‭/‬
‭ ‬Albert_Einstein‭)‬
‭ ‬data‭ = ‬usock.read‭();‬
‭ ‬usock.close‭();‬
‭ ‬return data
‭ ‬I got exception because HTTP 403‭ ‬error‭. ‬why‭? ‬with my 
browser i can't
‭ ‬access it without any problem‭?‬
‭ ‬
‭ ‬Thanks‭,‬
‭ ‬Shahar‭.‬

It appears that Wikipedia may inspect the contents of the User-Agent‭ ‬
HTTP header‭, ‬and that it does not particularly like the string it‭ ‬
receives from Python's urllib‭.  ‬I was able to make it work with urllib‭ 
‬
via the following code‭:‬

import urllib

class CustomURLopener‭ (‬urllib.FancyURLopener‭):‬
‭  ‬version‭ = '‬Mozilla/5.0‭'‬

urllib‭.‬_urlopener‭ = ‬CustomURLopener‭()‬

u‭ = 
‬urllib.urlopen‭('‬http‭://‬en.wikipedia.org/wiki/Albert_Einstein‭')‬
data‭ = ‬u.read‭()‬

I'm assuming a similar trick could be used with urllib2‭, ‬though I 
didn't‭ ‬
actually try it‭.  ‬Another thing to watch out for‭, ‬is that some 
sites‭ ‬
will redirect a public URL X to an internal URL Y‭, ‬and will check that‭ 
‬
access to Y is only permitted if the Referer field indicates coming from‭ ‬
somewhere internal to the site‭.  ‬I have seen both of these techniques‭ 
‬
used to foil screen-scraping‭.‬

Cheers‭,‬
‭-‬M

‭-- ‬
Michael J‭. ‬Fromberger‭ | ‬Lecturer‭, ‬Dept‭. ‬of 
Computer Science
http‭://‬www.dartmouth.edu‭/‬~sting‭/  | ‬Dartmouth College‭, 
‬Hanover‭, ‬NH‭, ‬USA
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: boolean operations on sets

2007-08-06 Thread Michael J. Fromberger
In article [EMAIL PROTECTED],
 Diez B. Roggisch [EMAIL PROTECTED] wrote:

 Flavio wrote:
 
  Hi, I have been playing with set operations lately and came across a
  kind of surprising result given that it is not mentioned in the
  standard Python tutorial:
  
  with python sets,   intersections  and unions are supposed to be done
  like this: 
  [...]
  
  The results are not what you would expect from an AND  or OR
  operation, from the mathematical point of view! aparently the and
  operation is returning the the second set, and the or operation is
  returning the first.
  [...]
 
 It has nothing to do with sets - it stems from the fact that certain values
 in python are considered false, and all others true. And these semantics
 were introduced at a point where there was no explicit True/False, so the
 operators were defined in exact the way you observed.
 
 Consider this:
 
 foo or bar - foo
 
 So - nothing to do with sets.

In addition to what Diez wrote above, it is worth noting that the 
practise of returning the value of the determining expression turns out 
to be convenient for the programmer in some cases.  Consider the 
following example:

  x = some_function(a, b, c) or another_function(d, e)

This is a rather nice shorthand notation for the following behaviour:

  t = some_function(a, b, c)
  if t:
x = t
  else:
x = another_function(d, e)

In other words, the short-circuit behaviour of the logical operators 
gives you a compact notation for evaluating certain types of conditional 
expressions and capturing their values.  If the or operator converted 
the result to True or False, you could not use it this way.  

Similarly,

  x = some_function(a, b, c) and another_function(d, e)

... behaves as if you had written:

  x = some_function(a, b, c)
  if x:
x = another_function(d, e)

Again, as above, if the results were forcibly converted to Boolean 
values, you could not use the shorthand.

Now that Python provides an expression variety of if, this is perhaps 
not as useful as it once was; however, it still has a role.  Suppose, 
for example, that a call to some_function() is very time-consuming; you 
would not want to write:

  x = some_function(a, b, c) \
  if some_function(a, b, c) else another_function(d, e)

... because then some_function would get evaluated twice.  Python does 
not permit assignment within an expression, so you can't get rid of the 
second call without changing the syntax.

Also, it is a common behaviour in many programming languages for logical 
connectives to both short-circuit and yield their values, so I'd argue 
that most programmers are proabably accustomed to it.  The  and || 
operators of C and its descendants also behave in this manner, as do the 
AND and OR of Lisp or Scheme.  It is possible that beginners may find it 
a little bit confusing at first, but I believe such confusion is minor 
and easily remedied.

Cheers,
-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: a dict trick

2007-08-02 Thread Michael J. Fromberger
In article [EMAIL PROTECTED],
 james_027 [EMAIL PROTECTED] wrote:

 hi
 
 for example I have this dictionary
 
 dict = {'name':'james', 'language':'english'}
 
 value = 'sex' in dict and dict['sex'] or 'unknown'
 
 is a right pythonic of doing this one? I am trying to get a value from
 the dict, but if the key doesn't exist I will provide one.

Hi, James,

You might prefer:

  d = {'name': 'James', 'language': 'English'}

  value = d.get('sex', 'unknown')

This accomplishes what your above code does, using a method of the 
built-in dict object.

If you also wish to ADD the new value to the dictionary, you may also 
use the following:

  value = d.setdefault('sex', 'unknown')

This returns the same value as the above, but also adds the key 'sex' to 
the dictionary as a side-effect, with value 'unknown'.

Cheers,
-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to initialize a table of months.

2007-04-16 Thread Michael J. Fromberger
In article [EMAIL PROTECTED],
 Steven W. Orr [EMAIL PROTECTED] wrote:

 I'm reading a logfile with a timestamp at the begging of each line, e.g.,
 
 Mar 29 08:29:00
 
 I want to call datetime.datetim() whose arg2 is a number between 1-12 so I 
 have to convert the month to an integer.
 I wrote this, but I have a sneaky suspicion there's a better way to do it.
 
 mons = {'Jan':1, 'Feb':2, 'Mar':3, 'Apr':4, 'May':5, 'Jun':6,
  'Jul':7, 'Aug':8, 'Sep':9, 'Oct':10, 'Nov':11, 'Dec':12 }
 
 def mon2int( mon ):
  global mons
  return mons[mon]
 
 Is there a generator expression or a list comprehension thingy that would 
 be *betterer*? (I realize it's probably not that important but I find lots 
 of value in learning all the idioms.)

There's no harm in your method as written, though it were probably wise 
to tolerate variations in capitalization.  But if you want to be cute 
about it, you could write something like this to set up your table:

  from datetime import date

  months = dict((date(1900, x+1, 1).strftime('%b').lower(), x+1) 
for x in xrange(12))

  def month2int(mName):
return months[mName.lower()]

If you don't like the lookup table, you can get a nicely portable result 
without it by using time.strptime(), e.g.,

  import time

  def month2int(mName):
return time.strptime(mName, '%b').tm_mon  # parses short names

Without knowing anything further about your needs, I would probably 
suggest the latter simply because it makes the hard work be somebody 
else's problem.

Cheers,
-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to remove multiple occurrences of a string within a list?

2007-04-04 Thread Michael J. Fromberger
In article [EMAIL PROTECTED],
 bahoo [EMAIL PROTECTED] wrote:

 Hi,
 
 I have a list like ['0024', 'haha', '0024']
 and as output I want ['haha']
 
 If I
 myList.remove('0024')
 
 then only the first instance of '0024' is removed.
 
 It seems like regular expressions is the rescue, but I couldn't find
 the right tool.

If you know in advance which items are duplicated, then there have been 
several simple solutions already proposed.  Here's another way to tackle 
the problem of removing ANY duplicated item from the list (i.e., any 
string that appears  1 time).

def killdups(lst):
  Filter duplicated elements from the input list, and return the 
  remaining (unique) items in their original order.
  
  count = {}
  for elt in lst:
count[elt] = count.get(elt, 0) + 1
  return [elt for elt in lst if count[elt] == 1]

This solution is not particularly tricky, but it has the nice properties 
that:

 1. It works on lists of any hashable type, not just strings,
 2. It preserves the order of the unfiltered items, 
 3. It makes only two passes over the input list.

Cheers,
-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to sort list

2006-11-23 Thread Michael J. Fromberger
In article [EMAIL PROTECTED],
 Lad [EMAIL PROTECTED] wrote:

 I have a list of emails and I would like to sorted that list by domains
 E.g.
 If the list is
 
 Emails=['[EMAIL PROTECTED]','[EMAIL PROTECTED]','[EMAIL PROTECTED]','[EMAIL 
 PROTECTED]',]
 
 after sorting I would like to have
 Emails=['[EMAIL PROTECTED]','[EMAIL PROTECTED]','[EMAIL PROTECTED]','[EMAIL 
 PROTECTED]',]
 
 What is the best/easiest way?

One reasonable option is to use the .sort() method of a list:

  Emails.sort(key = lambda s: list(reversed(s.split('@'

The key parameter specifies how to obtain a sort key from each element 
in the source list.

Cheers,
-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: +1 QOTW

2006-09-24 Thread Michael J. Fromberger
In article [EMAIL PROTECTED],
 Terry Reedy [EMAIL PROTECTED] wrote:

 [EMAIL PROTECTED] wrote in message 
 news:[EMAIL PROTECTED]
  Did anyone else crack up when Larry Wall described python with the
  statement:
 
  Python, as the anti-Perl, is heavily invested in maintaining Order.
 
  In the state of the onion address?
 
  http://www.perl.com/pub/a/2006/09/21/onion.html
 
 There is also this:
 'But I think the basic Perl paradigm is Whatever-oriented programming.'

But what this really means, in practise, is dis-oriented programming.

-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Python 2.5 and Unicode on MacOS

2006-09-21 Thread Michael J. Fromberger
I'm not sure whether this is a bug, or a feature that I do not fully 
understand.  I would therefore appreciate some advice, if you have any, 
on the following problem:

I have just installed Python 2.5 (release) under MacOS 10.4.7 (Tiger).  
Unfortunately, the unit test test_unicodefile.py (in Lib/tests/) 
exhibits a warning when invoked, as shown here:

[Lib/test] % python2.5 test_unicode_file.py
test_directories (__main__.TestUnicodeFiles) ... ok
test_unicode_file.py:103: UnicodeWarning: Unicode equal comparison 
failed to convert both arguments to Unicode - interpreting them as being 
unequal
  filename1==filename2
test_equivalent_files (__main__.TestUnicodeFiles) ... ok
test_single_files (__main__.TestUnicodeFiles) ... ok

--
Ran 3 tests in 0.252s

As you can see, the unit tests appear to pass, but I suspect there is a 
hidden problem, as a subsequent build of the latest version of 
BeautifulSoup now fails its unit tests, citing the same warning.  I 
can't tell whether this is a problem with the way BeautifulSoup is doing 
things, or whether there is a more general problem with Unicode support 
in the MacOS build of Python 2.5.

For reference, I have done a framework build of Python, and it seems to 
work fine for everything else I have tried.  Do you have any idea what 
might be causing this trouble?  I'd be grateful for your insights.

Cheers,
-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Semantics of propagated exceptions

2006-07-21 Thread Michael J. Fromberger
In article 
[EMAIL PROTECTED],
 Thomas Lotze [EMAIL PROTECTED] wrote:

 Suppose you have a function f which, as part of its protocol, raises some
 standard exception E under certain, well-defined circumstances. Suppose
 further that f calls other functions which may also raise E. How to best
 distinguish whether an exception E raised by f has the meaning defined by
 the protocol or just comes from details of the implementation?

My recommendation is to define your own exception type(s), and have f 
raise it (them) for errors that are truly generated by f.  Or, if you 
really want to use one of the existing exception types, then perhaps you 
can catch the exceptions from functions called by f, within f itself, 
and raise some other error (or suppress the errors, if appropriate).

Cheers,
-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dispatch with multiple inheritance

2006-07-19 Thread Michael J. Fromberger
In article [EMAIL PROTECTED],
 Michele Simionato [EMAIL PROTECTED] wrote:

 Michael J. Fromberger ha scritto:
 
  Consider the following class hierarchy in Python:
  snip
 
  Is there a better (i.e., more elegant) way to handle the case marked
  (**) above?
 
  Curious,
  -M
 
  --
  Michael J. Fromberger | Lecturer, Dept. of Computer Science
  http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
 
 Look at
 http://www.python.org/download/releases/2.3/mro

Hi, Michele,

I understand what Python's method resolution order is, and how it is 
constructed.  Is there something else in that article, that you hoped 
would address my original concern?  In particular, I am not asking WHY 
super(A, self).__init()__ yields the next method in order -- that is 
quite clear.  Rather, I want to know if there is a better way to invoke 
the ancestry of class E from D, than to explicitly denote the ancestry 
of C in the code.  The latter is an ugly violation of abstraction, and 
renders the class hierarchy brittle.

Of course, I could just bypass super, and explicitly invoke them as:

  C.__init__(self, ...)
  D.__init__(self, ...)

... but that seems to me to defeat the purpose of having super in the 
first place.

It seems as if perhaps there is no better solution at present.  That is 
a pity, albeit a mild one.  Thank you for taking the time to respond.

-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dispatch with multiple inheritance

2006-07-19 Thread Michael J. Fromberger
In article [EMAIL PROTECTED],
 Nick Vatamaniuc [EMAIL PROTECTED] wrote:

 Michael,
 You only need to call the __init__ method of the superclass if you need
 to do something special during initialization. 

Hi, Nick,

Thank you for responding.  I understand the purpose in invoking the 
superclasses' __init__ methods.  Let us take it as a given that I 
require this behaviour; the simple example does not show it, but I am 
using the same technique in a much more elaborate program, where in fact 
the superclass initialization is required.

 In general I just use the SuperClass.__init__(self,...) way of 
 calling the super class constructors.

Yes, and that certainly works just fine.  But it obviates the point of 
having super().  But perhaps that is the take-home message.

I do not want A's constructor to dispatch further along in the MRO for E 
for two reasons:  One, because the constructors along the (E D B) chain 
take different argument lists (in my real code) than the (E C A) path; 
and two, because I happen to care about the order in which the methods 
are invoked.

 In general note that  __init__ is NOT a constuctor it is an initializer

Yes, you're right; I apologize for the imprecision.  However, for the 
purposes of this example, the distinction is irrelevant.

Cheers,
-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dispatch with multiple inheritance

2006-07-19 Thread Michael J. Fromberger
In article [EMAIL PROTECTED],
 Michael Spencer [EMAIL PROTECTED] wrote:

 As others have pointed out, super, is designed to do something different from 
 what you want.  See 
 http://www.python.org/download/releases/2.2.3/descrintro/#cooperation for 
 GvR's 
   explanation of super's intent, limitations, and a pure-python 
   implementation 
 that you might alter for your purposes.

Indeed so.  The documentation for super() led me to an erroneous 
conclusion.  

 Given what you assert about your inheritance graph (two independent ancestor 
 chains, that do not propagate super calls from their respective base 
 classes), 
 what is objectionable about:
 C.__init__(self, ...)
 D.__init__(self, ...)

Well, since super() were much better labelled call_next_method, I must 
agree that the Class.__init__(self, ...) form is probably the best 
solution!

Thanks for your feedback.

Cheers,
-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Dispatch with multiple inheritance

2006-07-18 Thread Michael J. Fromberger
Consider the following class hierarchy in Python:

  class A (object):
def __init__(self):
  print cons A

  class B (object):
def __init__(self):
  print cons B

  class C (A):
def __init__(self):
  super(C, self).__init__()
  print cons C

  class D (B):
def __init__(self):
  super(D, self).__init__()
  print cons D

Now, suppose I would like to define a new class as follows:

  class E (C, D):
...

In the constructor for E, I would like to invoke the constructors of 
both parent classes.  The correct way to do this seems to be exemplified 
by the following:

  class E (C, D):
def __init__(self):
  super(E, self).__init__()  # calls C constructor
  super(A, self).__init__()  # calls D constructor (**)
  print cons E

This works, but I find it somewhat troubling.  It seems to me that one 
should not have to know (i.e., write down the names of) the ancestors 
of C in order to dispatch to superclass methods in D, since C and D 
share no common ancestors south of object.

Is there a better (i.e., more elegant) way to handle the case marked 
(**) above?

Curious,
-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Arrghh... Problems building Python from source on OS X --

2006-06-28 Thread Michael J. Fromberger
In article [EMAIL PROTECTED],
 J. Jeffrey Close [EMAIL PROTECTED] wrote:

 Hi all,
 
 I have been trying for some time to build Python 2.4.x
 from source on OS X 10.4.6.  I've found *numerous*
 postings on various mailing lists and web pages
 documenting the apparently well-known problems of
 doing so.  Various problems arise either in the
 ./configure step, with configure arguments that don't
 work, or in the compile, or in my case in the link
 step with libtool.
 
 The configure options I'm using are the following:
 --enable-framework --with-pydebug --with-debug=yes
 --prefix=/usr --with-dyld --program-suffix=.exe
 --enable-universalsdk
 
 I've managed to get past configure and can compile
 everything, but in the link I get the error Undefined
 symbols:  ___eprintf .  This appears to have
 something to do with dynamic library loading not
 properly pulling in libgcc.  I've tried with -lgcc in
 the LD options, but that produces a configure error
 cannot compute sizeof
 
 If I remove --enable-framework the complete build
 works, but unfortunately that is the one critical
 element that I need.
 
 The web pages I've found referring to this range from
 2001 to present -- still apparently everybody is
 having problems with this.  Does *anybody* here have
 Python built from source on this OS?

Hi, Jeffrey,

Yes, I use Python 2.4.3 built this way.  I did not have any significant 
troubles building Python on my 10.4 system.  My configuration step was a 
little different from yours, but basically I just checked out the 2.4.3 
source from Subversion, and this is how I configured it:

 env CFLAGS=-I/usr/local/include 
-I/Library/Frameworks/Tk.framework/Headers \
 LDFLAGS=-L/usr/local/lib \
 configure --enable-framework --enable-shared

I included /usr/local/include and /usr/local/lib so that the build could 
use the version of GNU readline I installed via Darwin ports.  The Tk 
headers allow pythonw to build properly. 

Having configured, I built and installed via:

  make
  sudo make frameworkinstall

I hope this may be helpful to you.

-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Finding yesterday's date with datetime

2006-05-15 Thread Michael J. Fromberger
In article [EMAIL PROTECTED],
 Thierry Lam [EMAIL PROTECTED] wrote:

 Is there an easy way to determine the yesterday's date(year-month-day)
 from the python datetime library if I know today's date?

from datetime import datetime, timedelta

today = datetime.today()
yesterday = today - timedelta(1)

# See the .month, .day, and .year fields of yesterday

Cheers,
-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Enumerating Regular Expressions

2006-05-09 Thread Michael J. Fromberger
In article [EMAIL PROTECTED],
 James Stroud [EMAIL PROTECTED] wrote:

 [EMAIL PROTECTED] wrote:
  James Stroud wrote:
  
 You mean like re.compile(r'.*') ?
  
  
  No.  I mean like:
  
 regex = re.compile(r'a|b')
 regex.enumerate()
  
  a
  b
 
 You see the difficulty don't you? How will the computer know in advance 
 that the regex matches only a finite set of possible strings?

You don't.  Hence, you want something that behaves like a generator, and 
will produce the strings one at a time.  Preferably, for the purposes of 
useful computation, in some canonical order.  

I'm sorry to say I don't know of an existing Python module to do this, 
although you could write one for at least the basic regular expression 
operators if you wanted.  The basic problem isn't all that hard to 
solve, though the full generality of the re module's input syntax is far 
more expressive than truly regular expressions from language theory.

Cheers,
-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Splice two lists

2006-05-06 Thread Michael J. Fromberger
In article [EMAIL PROTECTED],
 [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:

 Is there a good way to splice two lists together without resorting to a
 manual loop? Say I had 2 lists:
 
 l1 = [a,b,c]
 l2 = [1,2,3]
 
 And I want a list:
 
 [a,1,b,2,c,3] as the result.
 
 I've been searching around but I can't seem to find a good example.

Here's one possibility:

  list(reduce(lambda s, t: s + t, zip(L1, L2), ()))

-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to do an 'inner join' with dictionaries

2006-02-27 Thread Michael J. Fromberger
In article [EMAIL PROTECTED],
 [EMAIL PROTECTED] wrote:

 Let's say I have two dictionaries:
 dict1 is  1:23,  2:76,  4:56
 dict2 is  23:A, 76:B,  56:C
 
 How do I get a dictionary that is
 1:A, 2:B, 4:C

The following should work:

  j = dict((k, dict2[dict1[k]]) for k in dict1 if dict1[k] in dict2)

Cheers,
-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: can't solve an exercise-help me with it

2006-01-11 Thread Michael J. Fromberger
In article [EMAIL PROTECTED],
 hossam [EMAIL PROTECTED] wrote:

 I'm studying python newly and have an exercise that is difficult for me as a 
 beginner.Here it is :
 Write a program that approximates the value of pi by summing the terms of 
 this series:
 4/1-4/3+4/5-4/7+4/9-4/11+ The program should prompt the user for n, the 
 number of terms to sum and then output the sum of the first n terms of this 
 series.
 
 any help would be appreciated.
 
 benni

The series you're describing is basically expanding the Taylor series 
for 4 * atan(1), where

   i
  oo -1
   atan(1) =  SUM   --
 i = 0  2i + 1

(or in LaTeX:  \sum_{i=0}^{\infty}\frac{-1^i}{2i+1})

To enumerate the terms of this series, you can treat i as a counter, and 
compute the numerator and denominator either directly or from their 
previous values.  The sum can be accumulated into a separate variable.  
So, for instance,

. sum = 0. ; num = 1. ; den = 1.
. for i in xrange(n):
.   sum += num / den
.   num = -num
.   den += 2
. 
. pi = 4 * sum   # approximately...

Keep in mind that this approximation for atan(1) converges very slowly, 
so you will need to do quite a lot of terms before it will converge (you 
need about a thousand terms to get 2 significant figures after the 
decimal point).  You might have better results using Machin's formula, 

  atan(1) = 4 * atan(1/5) - atan(1/239)

Or, pi = 16 * atan(1/5) - 4 * atan(1/239).  The Taylor series will 
converge more quickly for atan(1/5) and atan(1/239).

(LaTeX: \mathrm{atan}(x) = \sum_{i=0}^\infty\frac{(-1^i)x^i}{2i+1})

Cheers,
-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Burrows-Wheeler (BWT) Algorithm in Python

2005-11-03 Thread Michael J. Fromberger
In article [EMAIL PROTECTED],
 [EMAIL PROTECTED] wrote:

 Michael J. Fromberger:
  I can send you a Python implementation I wrote, if you like; but if 
  you're interested in better understanding how the transform works, 
  I would recommend you try writing your own implementation.
 
 I'd like to see it, if you want you can put it somewhere or send it 
 directly.

You can grab a copy here:
  http://www.cs.dartmouth.edu/~sting/sw/bwt.py

Cheers,
-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Burrows-Wheeler (BWT) Algorithm in Python

2005-11-02 Thread Michael J. Fromberger
In article [EMAIL PROTECTED],
 DENG [EMAIL PROTECTED] wrote:

 Hi, all
 
 I've used Python Bz2 module for times and want to kown something about
 Burrows-Wheeler (BWT) algorithm, the Bz2 module is wrriten in C, is
 there a version in Python too?
 
 BWT
 http://gatekeeper.dec.com/pub/DEC/SRC/research-reports/abstracts/src-rr-124.ht
 ml
 Python Bz2 module
 http://labix.org/python-bz2

It is perfectly possible to implement the BWT in Python.  I can send you 
a Python implementation I wrote, if you like; but if you're interested 
in better understanding how the transform works, I would recommend you 
try writing your own implementation.  It's not very difficult to do, 
though for large inputs you may find performance to be an issue.

Mark Nelson wrote a nice user-friendly article on the BWT for the Sep. 
1996 issue of Dr. Dobbs, you might have a look:

  http://www.dogma.net/markn/articles/bwt/bwt.htm

I hope this helps you get started.

-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Where to find python c-sources

2005-09-29 Thread Michael J. Fromberger
In article [EMAIL PROTECTED],
 Tor Erik Sønvisen [EMAIL PROTECTED] wrote:

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

I recommend you look in the Modules subdirectory of the Python source 
tree.

Cheers,
-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Parser suggestion

2005-09-29 Thread Michael J. Fromberger
In article [EMAIL PROTECTED], Jorge Godoy [EMAIL PROTECTED] 
wrote:

 I'm needing a parser to retrieve some information from source code --
 including parts of code -- from Fortran, to use in a project with a
 documentation system.
 
 Any recommendations on a Python app or parser that I could use for that? 

There seems to be a great diversity of parsing tools available for 
Python programmers.  Here are a few suggestions to get you started:

PLY (Python Lex/Yacc)
  http://www.dabeaz.com/ply/

PyParsing
  http://pyparsing.sourceforge.net/

SPARK (Scanning Parsing And Rewriting Kit)
  http://pages.cpsc.ucalgary.ca/~aycock/spark/

You might also find the following an interesting read, if this sort of 
thing interests you:
  http://www.python.org/sigs/parser-sig/towards-standard.html

Cheers,
-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Crypto.Cipher.ARC4, bust or me doing something wrong?

2005-09-20 Thread Michael J. Fromberger
In article [EMAIL PROTECTED],
 Michael Sparks [EMAIL PROTECTED] wrote:

 Hi,
 
 
 I suspect this is a bug with AMK's Crypto package from
 http://www.amk.ca/python/code/crypto , but want to
 check to see if I'm being dumb before posting a bug
 report.
 
 I'm looking at using this library and to familiarise myself writing
 small tests with each of the ciphers. When I hit Crypto.Cipher.ARC4 I've
 found that I can't get it to decode what it encodes. This might be a
 case of PEBKAC, but I'm trying the following:
 
  from Crypto.Cipher import ARC4 as cipher
  key = 
  obj = cipher.new(key)
  obj.encrypt(This is some random text)
 ')f\xd4\xf6\xa6Lm\x9a%}\x8a\x95\x8ef\x00\xd6:\x12\x00!\xf3k\xafX'
  X=_
  X
 ')f\xd4\xf6\xa6Lm\x9a%}\x8a\x95\x8ef\x00\xd6:\x12\x00!\xf3k\xafX'
  obj.decrypt(X)
 '\x87\xe1\x83\xc1\x93\xdb\xed\x93U\xe4_\x92}\x9f\xdb\x84Y\xa3\xd4b\x9eHu~'
 
 Clearly this decode doesn't match the encode. Me being dumb or bug?
 
 Any comments welcome :)

Michael,

Since ARC4 is a stream cipher, the keystream changes over time -- with 
ARC4, after each character enciphered.  To decrypt successfully, you 
need to make sure the decrypting keystream exactly matches the 
encrypting one.  

In your example, you used a different keystream to decrypt than you used 
to encrypt -- in this case, a little further downstream of the original 
encryption key.

Contrast your experience above with the following transcript:

 from Crypto.Cipher import ARC4 as cipher
 enc = cipher.new(abcdefgh)
 dec = cipher.new(abcdefgh)
 x = enc.encrypt(This is some random text)
 x
\x05o\xd5XH|\xa4\xfc\xf7z\xecd\xe92\xfb\x05rR'\xbf\xc0F\xfc\xde
 y = dec.decrypt(x)
 y
'This is some random text'
 enc.decrypt(x)
'M|[bI\x1ciG6A]\x13Hz\xb0\x19\xca\xf1-\x9a\x1a2\x9e%'

I hope this helps clear up your confusion.

Cheers,
-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: encryption with python

2005-09-07 Thread Michael J. Fromberger
In article [EMAIL PROTECTED],
 [EMAIL PROTECTED] wrote:

 Hi!
 
 I was wondering if someone can recommend a good encryption algorithm
 written in python. My goal is to combine two different numbers and
 encrypt them to create a new number that cann't be traced back to the
 originals.
 
 It would be great if there exists a library already written to do this,
 and if there is, can somebody please point me to it??

I recommend you investigate PyCrypto:
  http://www.amk.ca/python/code/crypto
  http://sourceforge.net/projects/pycrypto

Cheers,
-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Proposal: add sys to __builtins__

2005-09-06 Thread Michael J. Fromberger
In article [EMAIL PROTECTED],
 Rick Wotnaz [EMAIL PROTECTED] wrote:

 You're right that there is no necessity for such a change. I was 
 not actually talking about importing *any* module in every case, 
 but rather about importing, say, 'sys' when, for example, sys.argv 
 appeared in the code and no import had been specified.

I think I must have missed that post; I will go back and look at it.  
However, while I'm here, how would your proposal deal with code like 
this:

   import foobar

   # ... some while later ...
   def f( ... ):
  ...
  global foobar, sys
  sys = foobar
  ...

   # ... some while even later ...
   f( ... )
   sys.wallaby(Fear and loathing!)

In particular, we have no import of sys, but the name sys is 
meaningful as a local alias for a different module.  I'm not saying you 
couldn't deal with this, but it rules out some of the more obvious ways 
of detecting and automatically handling this kind of substitution.

Naturally, you might well ask, why would you do such a fool thing?  To 
this I can only respond:  Never underestimate the ingenuity of fools.

-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Proposal: add sys to __builtins__

2005-09-05 Thread Michael J. Fromberger
In article [EMAIL PROTECTED],
 Rick Wotnaz [EMAIL PROTECTED] wrote:

 Michael Hoffman [EMAIL PROTECTED] wrote in
 news:[EMAIL PROTECTED]: 
 
  What would people think about adding sys to __builtins__ so that
  import sys is no longer necessary? This is something I must
  add to every script I write that's not a one-liner since they
  have this idiom at the bottom:
  
  if __name__ == __main__:
   sys.exit(main(sys.argv[1:]))
  
  [...]
  
  In short, given the wide use of sys, its unambiguous nature, and
  the fact that it really is built-in already, although not
  exposed as such, I think we would be better off if sys were
  always allowed even without an import statement.
 
 +1 here. As far as I'm concerned, both os and sys could be special-
 cased that way. That said, I would guess the likelihood of that 
 happening is 0.

While I'm mildly uncomfortable with the precedent that would be set by 
including the contents of sys as built-ins, I must confess my 
objections are primarily aesthetic:  I don't want to see the built-in 
namespace any more cluttered than is necessary -- or at least, any more 
than it already is.

But os is another matter -- the os module contains things which 
might well not be present in an embedded Python environment (e.g., file 
and directory structure navigation, stat).  Do you really want to force 
everyone to deal with that?  Is it so much more work to add import os 
to those Python programs that require it?

Of course, you might counter why should we force everybody else to type 
`import os' just in case somebody wants to imbed Python?  But then, why 
don't we just include the whole standard library in __builtins__?  Or, 
since that would be too much, maybe we survey the user community and 
include the top fifteen most included modules!  Where do you draw the 
line?  Do you really want to hard-code user opinions into the language?

Right now, we have a nice, simple yet effective mechanism for 
controlling the contents of our namespaces.  I don't think this would be 
a worthwhile change.  -1.

-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Replacing large number of substrings

2005-09-04 Thread Michael J. Fromberger
In article [EMAIL PROTECTED],
 Will McGugan [EMAIL PROTECTED] wrote:

 Hi,
 
 Is there a simple way of replacing a large number of substrings in a 
 string? I was hoping that str.replace could take a dictionary and use it 
 to replace the occurrences of the keys with the dict values, but that 
 doesnt seem to be the case.
 
 To clarify, something along these lines..
 
   dict_replace( a b c, dict(a=x, b=y) )
 x y c

Hi, Will,

Perhaps the following solution might appeal to you:

. import re
. 
. def replace_many(s, r):
. Replace substrings of s.  The parameter r is a dictionary in   
. which each key is a substring of s to be replaced and the
. corresponding value is the string to replace it with.
. 
. exp = re.compile('|'.join(re.escape(x) for x in r.keys()))
. return exp.sub(lambda m: r.get(m.group()), s)

Cheers,
-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Permutation Generator

2005-08-12 Thread Michael J. Fromberger
In article [EMAIL PROTECTED],
 Talin [EMAIL PROTECTED] wrote:

 I'm sure I am not the first person to do this, but I wanted to share 
 this: a generator which returns all permutations of a list:
 
 def permute( lst ):
 if len( lst ) == 1:
 yield lst
 else:
 head = lst[:1]
 for x in permute( lst[1:] ):
 yield head + x
 yield x + head
 return

You're right that you're not the first person to do this:  Many others 
have also posted incorrect permutation generators.

Have you tried your code on some simple test cases?

  list(permute([1, 2, 3]))
  == [[1, 2, 3], [2, 3, 1], [1, 3, 2], [3, 2, 1]]

Notably absent from this list are [2, 1, 3] and [2, 3, 1].  The problem 
gets worse with longer lists.  The basic problem is that x needs to be 
able to occur in ALL positions, not just the beginning and the end.

Cheers,
-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Loop until condition is true

2005-06-18 Thread Michael J. Fromberger
In article [EMAIL PROTECTED],
 Andrea Griffini [EMAIL PROTECTED] wrote:

 On Sat, 18 Jun 2005 13:35:16 -, Grant Edwards [EMAIL PROTECTED]
 wrote:
 
 AFAICT, the main use for do/while in C is when you want to
 define a block of code with local variables as a macro:
 
 When my job was squeezing most out of the CPU (videogame
 industry) I remember that the asm code generated by
 
while (sz--  0)
{
  /* do some stuff */
}
 
 was indeed worse than
 
do
{
  /* do some stuff */
} while (--sz);
 
 because of the initial empty-loop test (conditional jumps
 were bad, and forward conditional jumps were worse).
 So where at least one iteration was guaranteed the do-while
 loop was a better choice.

Hmm.  I don't know what compiler you were using, but in my experience 
it's fairly typical to compile while(test ...) { body ... } as

  j test
body: body
  ...
test: test
  ...
  je body  ;; or whatever your condition is

To turn this into a do { body ... } while(test ...), you need only 
remove the initial j test instruction.  Even if forward conditional 
jumps are bad for your particular architecture, this method avoids the 
problem neatly.

Personally, I'd be pretty suspicious of the quality of a compiler that 
produced radically different code for these two constructs without some 
damned good reason.

-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unique Elements in a List

2005-05-09 Thread Michael J. Fromberger
In article [EMAIL PROTECTED],
 [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:

 Is there an easy way to grab the Unique elements from a list?
 For Example:
 data = [0.1,0.5,0.6,0.4,0.1,0.5,0.6,0.9]
 
 what I am looking for is the unique elements 0.4 and 0.9 with their
 index from the list.
 Probably something like a Hash Table approach!!
 I would like to get this done without unnecessary overhead.And the list
 could be essentially anything strings,floats,int etc...
 
 Or is it already avaliable as an attr to a list or an array?
 I dont remember seeing anything like that.
 

From your comments downthread, it seems you want to find those elements 
of the input sequence which occur exactly once, and return not only 
these elements, but also their positions.

One reasonable solution might be as follows:

  def unique_elts(seq):
elts = {}
for pos, elt in enumerate(seq):
  elts.setdefault(elt, []).append(pos)

return [ (x, p[0]) for (x, p) in elts.iteritems()
 if len(p) == 1 ]

This returns a list of tuples of the form (x, pos), where x is an 
element of seq that occurs exactly once, and pos is its index in the 
original sequence.  This implementation traverses the input sequence 
exactly once, and requires storage proportional to the length of the 
input.

-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: variables exist

2005-04-17 Thread Michael J. Fromberger
In article [EMAIL PROTECTED],
 [EMAIL PROTECTED] (fabian) wrote:

 how testing if a variable exists in python as isset in php??
 

Would the following be a satisfactory implementation?

  def isset(varname, lloc = locals()):
return varname in lloc or varname in globals()

I believe this works as desired:

   x = 5
   def f(y):
  ...   z = 10
  ...   print isset('z')   ## == True
  ...   print isset('y')   ## == True
  ...   print isset('x')   ## == True
  ...

Tests:
   f(1)  ## As shown above
   print isset('z')   ## == False
   print isset('y')   ## == False
   print isset('x')   ## == True
   print isset('varname') ## == False
   print isset('lloc')## == False
   lloc = foo!
   print isset('lloc')## == True

Perhaps this is not the most elegant solution, but I believe it handles 
scoping correctly.

-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list