PEP proposal: sequence expansion support for yield statement: yield *

2016-04-20 Thread Ken Seehart
Currently the common pattern for yielding the elements in a sequence is as 
follows:

  for x in sequence: yield x

I propose the following replacement (the result would be identical):

  yield *sequence

The semantics are somewhat different from argument expansion (from which the 
syntax is borrowed), but intuitive: yield all of the elements of a sequence (as 
opposed to yield the sequence as a single item). This doesn't appear to have 
any syntactical collisions, as it is currently a syntax error.

Motivation: More compact notation, and the compiler can produce more efficient 
bytecode than the former representation (the loop overhead is omitted). This 
pattern is very common in recursive generators, so a compact notation would be 
nice.

Also, there is precedent: the proposed notation is implemented in javascript 
with identical semantics (though in javascript, the conventional spacing is 
different: yield* sequence ).

Examples:
  yield *(1,2,3)
... instead of :
  yield 1; yield 2; yield 3
... or:
  for x in (1,2,3): yield x


  yield *chain(seq1, seq2)
... instead of :
  for x in chain(seq1, seq2) yield x

~ Ken Seehart

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


Re: Trees

2015-01-21 Thread Ken Seehart

Hash Table, Christiania
(a table with many kinds of hash)

On 1/20/2015 12:19 PM, Devin Jeanpierre wrote:

There are similarly many kinds of hash tables.

For a given use case (e.g. a sorted dict, or a list with efficient
removal, etc.), there's a few data structures that make sense, and a
library (even the standard library) doesn't have to expose which one
was picked as long as the performance is good.

-- Devin

On Tue, Jan 20, 2015 at 12:15 PM, Ken Seehart k...@seehart.com wrote:

Exactly. There are over 23,000 different kinds of trees. There's no way you
could get all of them to fit in a library, especially a standard one.
Instead, we prefer to provide people with the tools they need to grow their
own trees.

http://caseytrees.org/programs/planting/ctp/
http://www.ncsu.edu/project/treesofstrength/treefact.htm
http://en.wikipedia.org/wiki/Tree

On 1/19/2015 3:01 PM, Mark Lawrence wrote:

On 19/01/2015 22:06, Zachary Gilmartin wrote:

Why aren't there trees in the python standard library?


Probably because you'd never get agreement as to which specific tree and
which specific implementation was the most suitable for inclusion.


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


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


Re: Trees

2015-01-20 Thread Ken Seehart
Exactly. There are over 23,000 different kinds of trees. There's no way 
you could get all of them to fit in a library, especially a standard 
one. Instead, we prefer to provide people with the tools they need to 
grow their own trees.


http://caseytrees.org/programs/planting/ctp/
http://www.ncsu.edu/project/treesofstrength/treefact.htm
http://en.wikipedia.org/wiki/Tree

On 1/19/2015 3:01 PM, Mark Lawrence wrote:

On 19/01/2015 22:06, Zachary Gilmartin wrote:

Why aren't there trees in the python standard library?



Probably because you'd never get agreement as to which specific tree 
and which specific implementation was the most suitable for inclusion.




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


Re: Yet another attempt at a safe eval() call

2013-03-14 Thread Ken Seehart
On 1/4/2013 5:33 AM, Steven D'Aprano wrote:
 On Fri, 04 Jan 2013 07:24:04 -0500, Terry Reedy wrote:

 On 1/3/2013 6:25 PM, Grant Edwards wrote:
 I've written a small assembler in Python 2.[67], and it needs to
 evaluate integer-valued arithmetic expressions in the context of a
 symbol table that defines integer values for a set of names.  The
 right thing is probably an expression parser/evaluator using ast, but
 it looked like that would take more code that the rest of the assembler
 combined, and I've got other higher-priority tasks to get back to.
 Will ast.literal_eval do what you want?
 No. Grant needs to support variables, not just literal constants, hence 
 the symbol table.


Apologies for the delayed response...

Seems like it would be a bit safer and easier to approach this problem
by stretching the capability of ast.literal_eval() rather than
attempting to sandbox eval().

How about ast.literal_eval after performing lexical substitution using
the symbol table?

Assignment into the symbol table, and error handling, are exercises left
to the reader.

Something vaguely like this:

/pseudocode:/

def safe_eval(s, symbols={}):
while search(s, r'\w+'):
replace match with '('+repr(symbols[match])+')' in s
return ast.literal_eval(s)

- Ken

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


Re: avoid the redefinition of a function

2012-09-12 Thread Ken Seehart
Use lambda expressions to define some constraints:


gt = lambda x: lambda y: xy
eq = lambda x: lambda y: x==y

constraints = [gt(2), eq(1)]
data = [3,1]

for i,c in enumerate(constraints):
print c(data[i])
   
   

On 9/12/2012 5:56 AM, Jabba Laci wrote:
 Hi,

 I have an installer script that contains lots of little functions. It
 has an interactive menu and the corresponding function is called. Over
 time it grew long and when I want to add a new function, I should give
 a unique name to that function. However, Python allows the
 redefinition of functions:

 #!/usr/bin/env python

 def step_1():
 print 1

 def step_1():
 print 2

 step_1()

 This will call the 2nd function. Now my functions are called step_ID
 (like step_27(), step_28(), etc.). How to avoid the danger of
 redefinition? Now, when I write a new function, I search for its name
 to see if it's unique but there must be a better way.

 Thanks,

 Laszlo
 P.S.: the script is here ( https://github.com/jabbalaci/jabbatron ) if
 you are interested. It's made for Ubuntu.




smime.p7s
Description: S/MIME Cryptographic Signature
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Boolean function on variable-length lists

2012-09-12 Thread Ken Seehart
Putting a few of peoples ideas together...


gt = lambda x: lambda y: xy
eq = lambda x: lambda y: x==y

def constrain(c,d):
return all({f(x) for f, x in zip(c, d)})

constraints = [gt(2), eq(1)]
data0 = [1,1]
data1 = [3,1]
   
print constrain(constraints, data0)
print constrain(constraints, data1)




On 9/12/2012 6:37 AM, Jussi Piitulainen wrote:
 Libra writes:
 On Wednesday, September 12, 2012 3:02:44 PM UTC+2, Jussi Piitulainen wrote:
  
 So you would associate each constraint with an index. You could
 maintain a list of constraints and apply it to the values as
 follows:
 Yes, even though there could be more constraints for each value in
 the list (at least 1 constraint for each value)
 Either you write more complex constraint functions, or you use more
 complex data structures to hold them.

 cs = [ lambda x : x = 1, lambda x : x = 3, lambda x : x == 2,
 ...lambda x : x = 3 ]

 { f(x) for f, x in zip(cs, [1,2,3,4]) }
 Just to understand, with f(x) you are defining a function f with
 argument x, right? I didn't know it was possible to define functions
 in this way. Is this a case of anonymous function?
 The value of each lambda expression is a function. f(x) is a function
 call, evaluated for each pair (f, x) from the list of pairs that the
 zip returns.

 { ... for ... in ... } creates a set of the values, no duplicates.
 [ ... for ... in ... ] creates a list of the values.

 {False, True}
 Actually, I don't understand the output. Why it is both False and
 True?
 It's a set containing False and True. The False comes from the f(x)
 where f = lambda x : x == 2, and x is 3. There is only one True
 because I requested a set of the values.




smime.p7s
Description: S/MIME Cryptographic Signature
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: something about performence

2011-06-21 Thread Ken Seehart
On 6/20/2011 11:56 PM, king6c...@gmail.com wrote:
 Thanks for your reply,Ken :)
 I found the two files ends with the same id……so I am lazy^-^
 I tried psyco,and unfortunately it costs nearly the same time as before.
 Is it true that we can only get str from files in Python?

Nope^_* . There are many applications such as image processing that
involve working with binary data.

(^_* well, technically yes actually: read() does in fact return str, but
the str can contain binary data)

But in order to do this, you need to use any of several modules that
allow python to operate on flat data.

Two standard modules exist for this purpose: *array *and *struct*. In
addition there are others such as *numpy *(for mathematical
applications) and *ctypes *(for interoperability between python and C/C++).

For your application, the *struct *module is sufficient.

 fout = open('junk.dat', 'wb') # open for writing binary
 fout.write(struct.pack('LL', 123,234))
 fout.write(struct.pack('LL', 123,234))
 fout.write(struct.pack('LL', 3,4))
 fout.close()

 fin = open('junk.dat', 'rb') # open for reading binary
 print struct.unpack('LL', fin.read(8))
(123, 234)
 print struct.unpack('LL', fin.read(8))
(123, 234)
 print struct.unpack('LL', fin.read(8))
(3, 4)
 print struct.unpack('LL', fin.read(8)) # raises struct.error at end
of file (because 0 bytes were read)
Traceback (most recent call last):
File string, line 1, in fragment
struct.error: unpack requires a string argument of length 8




 在 2011年6月21日 下午1:50,Ken Seehart k...@seehart.com
 mailto:k...@seehart.com写 道:

 On 6/20/2011 10:31 PM, Ken Seehart wrote:
 On 6/20/2011 7:59 PM, king6c...@gmail.com
 mailto:king6c...@gmail.com wrote:
 Hi,
 I have two large files,each has more than 2 lines,and
 each line consists of two fields,one is the id and the other a
 value,
 the ids are sorted.

 for example:

 file1
 (uin_a y)
 1 1245
 2 12333
 3 324543
 5 3464565
 


 file2
 (uin_b gift)
 1 34545
 3 6436466
 4 35345646
 5 463626
 

 I want to merge them and get a file,the lines of which consists
 of an id and the sum of the two values in file1 and file2。
 the codes are as below:

 uin_y=open('file1')
 uin_gift=open(file2')

 y_line=uin_y.next()
 gift_line=uin_gift.next()

 while 1:
 try:
 uin_a,y=[int(i) for i in y_line.split()]
 uin_b,gift=[int(i) for i in gift_line.split()]
 if uin_a==uin_b:
 score=y+gift
 print uin_a,score
 y_line=uin_y.next()
 gift_line=uin_gift.next()
 if uin_auin_b:
 print uin_a,y
 y_line=uin_y.next()
 if uin_auin_b:
 print uin_b,gift
 gift_line=uin_gift.next()
 except StopIteration:
 break


 the question is that those code runs 40+ minutes on a server(16
 core,32G mem),
 the time complexity is O(n),and there are not too much operations,
 I think it should be faster.So I want to ask which part costs so
 much.
 I tried the cProfile module but didn't get too much.
 I guess maybe it is the int() operation that cost so much,but
 I'm not sure
 and don't know how to solve this.
 Is there a way to avoid type convertion in Python such as scanf
 in C?
 Thanks for your help :)

 Unfortunately python does not have a scanf equivalent AFAIK. Most
 use cases for scanf can be handled by regular expressions, but
 that would clearly useless for you, and just slow you down more
 since it does not perform the int conversion for you.

 Your code appears to have a bug: I would expect that the last
 entry will be lost unless both files end with the same index
 value. Be sure to test your code on a few short test files.

 I recommend psyco to make the whole thing faster.

 Regards,
 Ken Seehart

 Another thought (a bit of extra work, but you might find it
 worthwhile if psyco doesn't yield a sufficient boost):

 Write a couple filter programs to convert to and from binary data
 (pairs of 32 or 64 bit integers depending on your requirements).

 Modify your program to use the subprocess module to open two
 instances of the binary conversion process with the two input
 files. Then pipe the output of that program into the binary to
 text filter.

 This might turn out to be faster since each process would make use
 of a core. Also it gives you other options, such as keeping your
 data in binary form for processing, and converting to text only as
 needed.

 Ken Seehart


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



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


Re: something about performence

2011-06-20 Thread Ken Seehart
On 6/20/2011 7:59 PM, king6c...@gmail.com wrote:
 Hi,
 I have two large files,each has more than 2 lines,and each
 line consists of two fields,one is the id and the other a value,
 the ids are sorted.

 for example:

 file1
 (uin_a y)
 1 1245
 2 12333
 3 324543
 5 3464565
 


 file2
 (uin_b gift)
 1 34545
 3 6436466
 4 35345646
 5 463626
 

 I want to merge them and get a file,the lines of which consists of an
 id and the sum of the two values in file1 and file2。
 the codes are as below:

 uin_y=open('file1')
 uin_gift=open(file2')

 y_line=uin_y.next()
 gift_line=uin_gift.next()

 while 1:
 try:
 uin_a,y=[int(i) for i in y_line.split()]
 uin_b,gift=[int(i) for i in gift_line.split()]
 if uin_a==uin_b:
 score=y+gift
 print uin_a,score
 y_line=uin_y.next()
 gift_line=uin_gift.next()
 if uin_auin_b:
 print uin_a,y
 y_line=uin_y.next()
 if uin_auin_b:
 print uin_b,gift
 gift_line=uin_gift.next()
 except StopIteration:
 break


 the question is that those code runs 40+ minutes on a server(16
 core,32G mem),
 the time complexity is O(n),and there are not too much operations,
 I think it should be faster.So I want to ask which part costs so much.
 I tried the cProfile module but didn't get too much.
 I guess maybe it is the int() operation that cost so much,but I'm not
 sure
 and don't know how to solve this.
 Is there a way to avoid type convertion in Python such as scanf in C?
 Thanks for your help :)

Unfortunately python does not have a scanf equivalent AFAIK. Most use
cases for scanf can be handled by regular expressions, but that would
clearly useless for you, and just slow you down more since it does not
perform the int conversion for you.

Your code appears to have a bug: I would expect that the last entry will
be lost unless both files end with the same index value. Be sure to test
your code on a few short test files.

I recommend psyco to make the whole thing faster.

Regards,
Ken Seehart

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


Re: something about performence

2011-06-20 Thread Ken Seehart
On 6/20/2011 10:31 PM, Ken Seehart wrote:
 On 6/20/2011 7:59 PM, king6c...@gmail.com wrote:
 Hi,
 I have two large files,each has more than 2 lines,and each
 line consists of two fields,one is the id and the other a value,
 the ids are sorted.

 for example:

 file1
 (uin_a y)
 1 1245
 2 12333
 3 324543
 5 3464565
 


 file2
 (uin_b gift)
 1 34545
 3 6436466
 4 35345646
 5 463626
 

 I want to merge them and get a file,the lines of which consists of an
 id and the sum of the two values in file1 and file2。
 the codes are as below:

 uin_y=open('file1')
 uin_gift=open(file2')

 y_line=uin_y.next()
 gift_line=uin_gift.next()

 while 1:
 try:
 uin_a,y=[int(i) for i in y_line.split()]
 uin_b,gift=[int(i) for i in gift_line.split()]
 if uin_a==uin_b:
 score=y+gift
 print uin_a,score
 y_line=uin_y.next()
 gift_line=uin_gift.next()
 if uin_auin_b:
 print uin_a,y
 y_line=uin_y.next()
 if uin_auin_b:
 print uin_b,gift
 gift_line=uin_gift.next()
 except StopIteration:
 break


 the question is that those code runs 40+ minutes on a server(16
 core,32G mem),
 the time complexity is O(n),and there are not too much operations,
 I think it should be faster.So I want to ask which part costs so much.
 I tried the cProfile module but didn't get too much.
 I guess maybe it is the int() operation that cost so much,but I'm not
 sure
 and don't know how to solve this.
 Is there a way to avoid type convertion in Python such as scanf in C?
 Thanks for your help :)

 Unfortunately python does not have a scanf equivalent AFAIK. Most use
 cases for scanf can be handled by regular expressions, but that would
 clearly useless for you, and just slow you down more since it does not
 perform the int conversion for you.

 Your code appears to have a bug: I would expect that the last entry
 will be lost unless both files end with the same index value. Be sure
 to test your code on a few short test files.

 I recommend psyco to make the whole thing faster.

 Regards,
 Ken Seehart

Another thought (a bit of extra work, but you might find it worthwhile
if psyco doesn't yield a sufficient boost):

Write a couple filter programs to convert to and from binary data (pairs
of 32 or 64 bit integers depending on your requirements).

Modify your program to use the subprocess module to open two instances
of the binary conversion process with the two input files. Then pipe the
output of that program into the binary to text filter.

This might turn out to be faster since each process would make use of a
core. Also it gives you other options, such as keeping your data in
binary form for processing, and converting to text only as needed.

Ken Seehart

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


Re: Function __defaults__

2011-04-25 Thread Ken Seehart

On 4/25/2011 4:59 AM, Colin J. Williams wrote:

On 24-Apr-11 13:07 PM, Ken Seehart wrote:

On 4/24/2011 2:58 AM, Steven D'Aprano wrote:

Consider this in Python 3.1:



def f(a=42):

... return a
...

f()

42

f.__defaults__ = (23,)
f()

23


Is this an accident of implementation, or can I trust that changing
function defaults in this fashion is guaranteed to work?


This is documented in python 3, so I would expect it to be stable (until
python 4, that is)
http://docs.python.org/py3k/whatsnew/3.0.html#operators-and-special-methods 


http://docs.python.org/py3k/library/inspect.html#types-and-members

The f.__defaults__ attribute was previously known as f.func_defaults (in
python 2.x), which has been around, documented and stable for quite a
while.

So it's probably just as safe as any other monkey patching technique. :)

Best of luck,
Ken



Wouldn't it make more sense to return a dictionary instead of a tuple?

Colin W.



I assume you mean making the value of f.__defaults__ a dictionary 
instead of a tuple.


A dictionary would be slower to process since it would have to iterate 
the dictionary keys and assign arguments by name.
Since argument defaults can only be applied to the rightmost contiguous 
sequence of zero or more parameters (excluding *args,**kwargs),  a tuple 
is sufficient to cover all cases, so a dictionary would provide no 
advantage.
Also, a dictionary would produce an unnecessary error case (if a key in 
the dictionary is not the name of an argument).


Good question though.

Cheers,
Ken

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


Re: Function __defaults__

2011-04-24 Thread Ken Seehart

On 4/24/2011 2:58 AM, Steven D'Aprano wrote:

Consider this in Python 3.1:



def f(a=42):

... return a
...

f()

42

f.__defaults__ = (23,)
f()

23


Is this an accident of implementation, or can I trust that changing
function defaults in this fashion is guaranteed to work?


This is documented in python 3, so I would expect it to be stable (until 
python 4, that is)

http://docs.python.org/py3k/whatsnew/3.0.html#operators-and-special-methods
http://docs.python.org/py3k/library/inspect.html#types-and-members

The f.__defaults__ attribute was previously known as f.func_defaults (in 
python 2.x), which has been around, documented and stable for quite a while.


So it's probably just as safe as any other monkey patching technique. :)

Best of luck,
Ken

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


Re: Function __defaults__

2011-04-24 Thread Ken Seehart
Good point, Benjamin.  I didn't think of testing on Jython before 
answering.  For practical purposes it's a really good idea to test 
obscure features against all potential target platforms.


In this case, I would argue that**Benjamin's test demonstrates a bug in 
Jython.


One could counter by pointing out that the documentation does not 
specify that the __defaults__ attribute is writable.  However, in that 
case, Jython should explicitly make that attribute read only (as well as 
any other magic attributes where modification is silently ignored).  
That way, at least it would raise an exception in Jython.


Therefore, I would also suggest that the behavior of writable magic 
attributes should be added to the documentation and the python test suite.


On the other hand, if for some reason it is decided that support for 
such functionality is not desired in Python, then write access to such 
attributes should be deprecated and later removed.  However, I happen to 
like the ability to do this kind of thing, so I would vote for 
specifying the current CPython 3 behavior (as demonstrated in Steven's 
initial post) in the Python documentation.


On 4/24/2011 10:02 AM, Benjamin Kaplan wrote:

On Sun, Apr 24, 2011 at 5:58 AM, Steven D'Aprano
steve+comp.lang.pyt...@pearwood.info  wrote:

Consider this in Python 3.1:



def f(a=42):

... return a
...

f()

42

f.__defaults__ = (23,)
f()

23


Is this an accident of implementation, or can I trust that changing
function defaults in this fashion is guaranteed to work?



--
Steven

Jython 2.5.1 (Release_2_5_1:6813, Sep 26 2009, 13:47:54)
[Java HotSpot(TM) 64-Bit Server VM (Apple Inc.)] on java1.6.0_24
Type help, copyright, credits or license for more information.

def f(a=42) :

...return a
...

f()

42

f.__defaults__ = (23,)
f()

42

I'm going to go with implementation detail.


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



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


Re: Function __defaults__

2011-04-24 Thread Ken Seehart

Oops, I must correct myself.  Please ignore my previous post.

As Daniel points out, Writable is specified in the Python 3 
documentation.  Apparently I was reading the documentation with only my 
right eye open, and the Writable tag fell on my blind spot.
I concur that this unambiguously implies that the attribute should work 
as advertised after being written.


This is not a bug in Jython.  As Daniel points out the attribute was 
renamed from func_defaults to __defaults__ in python 3.


Jython 2.5.2 (Release_2_5_2:7206, Mar 2 2011, 23:12:06)
[Java HotSpot(TM) Client VM (Sun Microsystems Inc.)] on java1.6.0_24
Type help, copyright, credits or license for more information.
 def foo(x=4):
...   print x
...
 foo()
4
 foo.func_defaults
(4,)
 foo.func_defaults = (3,)
 foo()
3


So it works correctly in Jython 2.x.

Conclusion: Not an implementation detail, and safe to use.

Ken

On 4/24/2011 10:18 AM, Daniel Kluev wrote:

http://docs.python.org/dev/reference/datamodel.html
Callable types
...
Special attributes:
...
__defaults__A tuple containing default argument values for those
arguments that have defaults, or None if no arguments have a default
value   Writable

I don't see any 'implementation detail' mark there, and 'Writable'
IMHO means it can be used.

On Mon, Apr 25, 2011 at 4:02 AM, Benjamin Kaplan
benjamin.kap...@case.edu  wrote:

Jython 2.5.1 (Release_2_5_1:6813, Sep 26 2009, 13:47:54)

In 2.x it was func_defaults (http://docs.python.org/reference/datamodel.html)
__defaults__ is 3.x feature



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


Re: Function __defaults__

2011-04-24 Thread Ken Seehart

Gotta love that email latency. :-D
Ken

On 4/24/2011 2:47 PM, Daniel Kluev wrote:

On Mon, Apr 25, 2011 at 8:21 AM, Ken Seehartk...@seehart.com  wrote:

Good point, Benjamin.  I didn't think of testing on Jython before
answering.  For practical purposes it's a really good idea to test obscure
features against all potential target platforms.

In this case, I would argue that Benjamin's test demonstrates a bug in
Jython.

It doesn't. __defaults__ was added in 3.x (and it is documented).
Prior to that, in 2.x, there was func_defaults.

Jython is not in 3.x yet, so you should not expect 3.x features there.
As for func_defaults, its there and supported as documented:

Jython 2.5.1+ (Release_2_5_1:exported, Mar 21 2010, 01:00:17)
[Java HotSpot(TM) Server VM (Sun Microsystems Inc.)] on java1.6.0_22
Type help, copyright, credits or license for more information.

def test(a=123):

... return a
...

test()

123

test.func_defaults

(123,)

test.func_defaults = (456,)
test()

456



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


Re: PyCon

2011-03-11 Thread Ken Seehart

On 3/11/2011 7:45 AM, Rita wrote:

http://us.pycon.org/2010/ http://us.pycon.org/2009/

Try the wayback machine:

http://replay.waybackmachine.org/20100701160843/http://us.pycon.org/2010/about/
--
http://mail.python.org/mailman/listinfo/python-list


IMAP mail filters tool

2010-03-04 Thread Ken Seehart
I'm thinking of possibly making a simple client-agnostic tool for 
filtering and processing IMAP email.  I'm a Thunderbird user, but I'm 
interested in a tool that is not client software specific.


So I am is checking for prior art.  Does anyone know of a filter tool 
with most of these features?


   * Written and customizable with python
   * IMAP based
   * Runs on client (i.e. without installing anything on the server)
   * Runs completely independent of client software (though could be
 launched from any email client that can support it)
   * Automatically suggests new rules based on manual movement of
 emails into folders (based on comparison of all header data across
 all existing folders)
   * Take arbitrary actions in response to rules (e.g. universally
 denying access to spam senders to all online cheese shops)
   * Capable of serving as a prototyping framework for things like spam
 filtering
   * General filtering functionality, not particularly focused on spam
   * Posts and updates rules to server via IMAP (i.e. by sending and
 automagically processing a special email message).

Typical work-flow would be aimed at users who like to automatically sort 
their email prior to reading it.  The goal is to have nearly all email 
sorted into folders so that most mail remaining in the Inbox is likely 
to be leftover spam missed by whatever spam filters are being used.  In 
other words, the filters tend to effectively serve as white lists.


Examples of unusual rules that don't seem to be supported by most email 
clients:


   * When you send an email, add the recipient to a filter such that
 email from that recipient goes to a Unfiltered Replies folder if
 it does not match any other filter.
   * Receiving email with certain fields matching certain regular
 expressions could automatically create a new folder and filter e.g.:
 o List-Id: SF Bay Area Python group baypiggies.python.org
   yields a new folder Lists/SF Bay Area Python group with a
   corresponding rule
 o X-Launchpad-Bug: product=phatch; milestone=... yields a
   new folder Bugs/Phatch with a corresponding rule
 o X-Bugzilla-Product: Shrubbery yields a new folder
   Bugs/Shrubbery with a corresponding rule


Thanks,
Ken

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


Re: python and http POST

2010-02-11 Thread Ken Seehart
Use tamperdata to view and modify HTTP/HTTPS headers and post 
parameters... 


https://addons.mozilla.org/en-US/firefox/addon/966

Enjoy,
Ken

galileo228 wrote:

Hey All,

Been teaching myself Python for a few weeks, and am trying to write a
program that will go to a url, enter a string in one of the search
fields, submit the search, and return the contents of the search
result.

I'm using httplib2.

My two particular questions:

1) When I set my 'body' var, (i.e. 'body = {'query':'search_term'}),
how do I know what the particular key should be? In other words, how
do I tell python which form on the web page I'm visiting I'd like to
fill in? Do I simply go to the webpage itself and look at the html
source? But if that's the case, which tag tells me the name of the
key?

2) Even once python fills in the form properly, how can I tell it to
'submit' the search?

Thanks all!

Matt
  


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


regex to match python string literal syntax

2010-02-10 Thread Ken Seehart


I found this:
  http://code.activestate.com/recipes/475109/

But it is incorrect in some cases, such as:
*
foo \ bar*  / (incorrectly matches foo \)/
*
'''* /(incorrectly matches the second two single quotes)/

* foo
 bar *  / (incorrectly matches quote containing newline/)

Anyone know a regular expression that correctly matches python string 
literals?


Thanks in advance,
Ken

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


Re: regex to match python string literal syntax

2010-02-10 Thread Ken Seehart

Found this in pypy!

# Match any flavor of string; /*the terminating quote is optional*/
# so that we're robust in the face of incomplete program text.

_match_stringre = re.compile(r
   \ [^\\]* (?:
(?: \\. | (?!) )
[^\\]*
)*
   (?: \ )?

|[^\\\n]* (?: \\. [^\\\n]* )* ?

|   ''' [^'\\]* (?:
  (?: \\. | '(?!'') )
  [^'\\]*
   )*
   (?: ''' )?

|   ' [^'\\\n]* (?: \\. [^'\\\n]* )* '?
, re.VERBOSE | re.DOTALL).match

Problem solved.

Ken


Ken Seehart wrote:


I found this:
   http://code.activestate.com/recipes/475109/

But it is incorrect in some cases, such as:
*
foo \ bar*  / (incorrectly matches foo \)/
*
'''* /(incorrectly matches the second two single quotes)/

* foo
  bar *  / (incorrectly matches quote containing newline/)

Anyone know a regular expression that correctly matches python string 
literals?


Thanks in advance,
Ken



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


Buffered streaming

2009-11-26 Thread Ken Seehart
I need to create a pipe where I have one thread (or maybe a generator) 
writing data to the tail while another python object is reading from the 
head.  This will run in real time, so the data must be deallocated after 
it is consumed.  Reading should block until data is written, and writing 
should block when the buffer is full (i.e. until some of the data is 
consumed).  I assume there must be a trivial way to do this, but I don't 
see it.  Any ideas or examples?


I'm using python 2.6.

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


Re: Help with database planning

2009-11-14 Thread Ken Seehart
Good idea to use Django.  I've just started using it and I really like 
it.  However, I should give you a heads-up: You will probably want to 
use a Django migration tool (I'm using South) because the alternative is 
basically to rebuild your database each time your model changes.  
Unfortunately, South can sometimes get confused when using anything less 
sophisticated than PostgreSQL (I switched from MySQL to PostgreSQL for 
this reason).  I don't know if South or the other various Django 
migration tools work with MySQL.


Applying the DRY (don't repeat yourself), you might even consider 
running the same code as a local web server instead of implementing a 
separate desktop version.  But it is just a suggestion; there are 
various reasons why you might not want to do that.


On to the DB design question...

One approach would be to make a Generic class that can represent a 
concept, slot, or filler, which would have a type field to identify 
which of these to use.


class Concept(models.Model):
   ...

class Slot(models.Model):
   ...

class Filler(models.Model):
   ...

class Facet(models.Model):
   ...

class Generic(models.Model):
   TYPE_CHOICES = (
   (u'c', u'concept'),
   (u's', u'slot'),
   (u'f', u'filler'),
   }

   # Only one of the following is used.  The other two are blank.
   concept = models.ForeignKey(Concept)
   slot = models.ForeignKey(Slot)
   filler = models.ForeignKey(Filler)

class ConceptDef(models.Model):
   concept = models.ForeignKey(Concept)
   slot = models.ForeignKey(Generic)
   facet = models.ForeignKey(Facet)
   filler = models.ForeignKey(Generic)



Juliano wrote:

Hello, everybody.

I'm a linguist with practical skills on computers/programming.

We've been working with an ontology at my department, and now I need
to create a GUI viewer for the flat file we have.
I tried to write an Ontology class which manages the data read and
parsed from the flat file, but it takes a relatively long time.
Besides, we have plans to set up a website for online display of said
ontology. So, I have been being pushed towards changing the basic plan
and build a DB so that data access will be faster and easier for both
the desktop GUI and the web app. Right now, I'm trying to work with
sqlite, since it can be used as a separate file for the GUI and as a
DB for Django (which may be the choice for the web interface).

I have been redaing some books on DBs but I kind of get stuck when it
comes to the normalization and the planning of the tables. The problem
is that we have basically four fields that can be arranged in a tree-
like structure. Eg:

Concept
  |- Slot
  |`-- Facet
  |  `-- Filler
  `- Slot
   `-- Facet
 `-- Filler
 `-- Filler
...

So, for ONE *concept*, we have, usually, MANY *slots*, each *slot* has
ONE *facet*, and each *facet* can have MORE THAN ONE *filler*.
Besides, some *slots* and *fillers* are themselves *concepts*,
creating a sort of recursive reference.

begin table
line_no conceptsslots   facets  fillers
--
0   ABANDON DEFINITION  VALUE   to leave or desert something or
someone
1   ABANDON IS-AVALUE   EXIT
2   ABANDON LEXEMAP-LEX leave behind-V1
3   ABANDON LEXEMAP-LEX abandon-V1
(...)
97420   ZULUDEFINITION  VALUE   a language or dialect spoken in south
africa and others
97421   ZULUINSTANCE-OF VALUE   OTHER-NIGER-KORDOFANIAN-LANGUAGE
97422   ZULULANGUAGE-OF INV LESOTHO
97423   ZULULANGUAGE-OF INV SOUTH-AFRICA
end table

I tried to create index tables for concepts, slots, facets and
fillers, which gave me the following table:

begin table
line_no conceptsslots   facets  fillers
--
0   cn_0sl_00048fc_7fl_07349
1   cn_0cn_02605fc_7cn_01768
2   cn_0sl_00121fc_2fl_04329
3   cn_0sl_00121fc_2fl_15009
(...)
97420   cn_05429sl_00048fc_7fl_01340
97421   cn_05429cn_02493fc_7cn_03526
97422   cn_05429cn_02750fc_1cn_02816
97423   cn_05429cn_02750fc_1cn_04580
end table

(cn_X from concept index, sl_X from slot index,
fc_X from facet index, fl_X from filler index.)

As you can see, only concepts and facets are populated by their own
type of data.
Whereas slots and fillers can be populated by their own types or by
concepts.

What would be a good way to create tables for this situation?
In fact, this is the first time I've ever tried to create a DB, so I'm
completely lost.

I'm looking forward to a reply...

Thank you very much,
Juliano
  


--

Re: Help with database planning

2009-11-14 Thread Ken Seehart

Oops, forgot the blank arg.  Anyway, this is of course untested code...

  # Only one of the following is used.  The other two are blank.
  concept = models.ForeignKey(Concept, blank=True)
  slot = models.ForeignKey(Slot, blank=True)
  filler = models.ForeignKey(Filler, blank=True)

Ken Seehart wrote:
Good idea to use Django.  I've just started using it and I really like 
it.  However, I should give you a heads-up: You will probably want to 
use a Django migration tool (I'm using South) because the alternative 
is basically to rebuild your database each time your model changes.  
Unfortunately, South can sometimes get confused when using anything 
less sophisticated than PostgreSQL (I switched from MySQL to 
PostgreSQL for this reason).  I don't know if South or the other 
various Django migration tools work with MySQL.


Applying the DRY (don't repeat yourself), you might even consider 
running the same code as a local web server instead of implementing a 
separate desktop version.  But it is just a suggestion; there are 
various reasons why you might not want to do that.


On to the DB design question...

One approach would be to make a Generic class that can represent a 
concept, slot, or filler, which would have a type field to identify 
which of these to use.


class Concept(models.Model):
   ...

class Slot(models.Model):
   ...

class Filler(models.Model):
   ...

class Facet(models.Model):
   ...

class Generic(models.Model):
   TYPE_CHOICES = (
   (u'c', u'concept'),
   (u's', u'slot'),
   (u'f', u'filler'),
   }

   # Only one of the following is used.  The other two are blank.
   concept = models.ForeignKey(Concept)
   slot = models.ForeignKey(Slot)
   filler = models.ForeignKey(Filler)

class ConceptDef(models.Model):
   concept = models.ForeignKey(Concept)
   slot = models.ForeignKey(Generic)
   facet = models.ForeignKey(Facet)
   filler = models.ForeignKey(Generic)   


Juliano wrote:

Hello, everybody.

I'm a linguist with practical skills on computers/programming.

We've been working with an ontology at my department, and now I need
to create a GUI viewer for the flat file we have.
I tried to write an Ontology class which manages the data read and
parsed from the flat file, but it takes a relatively long time.
Besides, we have plans to set up a website for online display of said
ontology. So, I have been being pushed towards changing the basic plan
and build a DB so that data access will be faster and easier for both
the desktop GUI and the web app. Right now, I'm trying to work with
sqlite, since it can be used as a separate file for the GUI and as a
DB for Django (which may be the choice for the web interface).

I have been redaing some books on DBs but I kind of get stuck when it
comes to the normalization and the planning of the tables. The problem
is that we have basically four fields that can be arranged in a tree-
like structure. Eg:

Concept
  |- Slot
  |`-- Facet
  |  `-- Filler
  `- Slot
   `-- Facet
 `-- Filler
 `-- Filler
...

So, for ONE *concept*, we have, usually, MANY *slots*, each *slot* has
ONE *facet*, and each *facet* can have MORE THAN ONE *filler*.
Besides, some *slots* and *fillers* are themselves *concepts*,
creating a sort of recursive reference.

begin table
line_noconceptsslotsfacetsfillers
-- 

0ABANDONDEFINITIONVALUEto leave or desert 
something or

someone
1ABANDONIS-AVALUEEXIT
2ABANDONLEXEMAP-LEXleave behind-V1
3ABANDONLEXEMAP-LEXabandon-V1
(...)
97420ZULUDEFINITIONVALUEa language or dialect spoken 
in south

africa and others
97421ZULUINSTANCE-OFVALUE
OTHER-NIGER-KORDOFANIAN-LANGUAGE

97422ZULULANGUAGE-OFINVLESOTHO
97423ZULULANGUAGE-OFINVSOUTH-AFRICA
end table

I tried to create index tables for concepts, slots, facets and
fillers, which gave me the following table:

begin table
line_noconceptsslotsfacetsfillers
-- 


0cn_0sl_00048fc_7fl_07349
1cn_0cn_02605fc_7cn_01768
2cn_0sl_00121fc_2fl_04329
3cn_0sl_00121fc_2fl_15009
(...)
97420cn_05429sl_00048fc_7fl_01340
97421cn_05429cn_02493fc_7cn_03526
97422cn_05429cn_02750fc_1cn_02816
97423cn_05429cn_02750fc_1cn_04580
end table

(cn_X from concept index, sl_X from slot index,
fc_X from facet index, fl_X from filler index.)

As you can see, only concepts and facets are populated by their own
type of data.
Whereas slots and fillers can be populated by their own types

Re: Authentication session with urllib2

2009-11-11 Thread Ken Seehart




Jon Clements wrote:

  On 11 Nov, 07:02, Ken Seehart k...@seehart.com wrote:
  
  
I'm having some difficulty implementing a client that needs to maintain
an authenticated https: session.

I'd like to avoid the approach of receiving a 401 and resubmit with
authentication, for two reasons:

1. I control the server, and it was easy for me to make a url that
receives a POST with username, password and authenticates the session. 
The server keeps a session correctly when I test with a browser. This
part works fine.

2. I don't want to send every request twice. Seehttp://bugs.python.org/issue7159There's no reason why I should have to
do this since I have the ability to keep the server simple.

What I would really like to do is send one request with the username and
password to establish the session, and then make multiple authenticated
requests where the session information remembers the authentication.

Is there a way to make this happen in python 2.5.2?

Keep in mind that this only needs to work with a particular server which
I control. It does not need to function as a general purpose browser. 
The server is powered by Django.

- Ken

  
  
How about http://docs.djangoproject.com/en/dev/topics/auth/ and using
a urllib2 opener with cookie support ala some examples on
http://personalpages.tds.net/~kent37/kk/00010.html ?

hth,
Jon.
  

Thanks Jon, for the examples at Kent's Korner (good stuff for me to
know in any case). Actually, I ended up just simplifying everything by
just sending username and password proactively on every request as POST
arguments which are processed directly by the handler, and not using
any session data after all.

- Ken



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


Authentication session with urllib2

2009-11-10 Thread Ken Seehart
I'm having some difficulty implementing a client that needs to maintain 
an authenticated https: session.


I'd like to avoid the approach of receiving a 401 and resubmit with 
authentication, for two reasons:


1. I control the server, and it was easy for me to make a url that 
receives a POST with username, password and authenticates the session.  
The server keeps a session correctly when I test with a browser.  This 
part works fine.


2. I don't want to send every request twice.  See 
http://bugs.python.org/issue7159  There's no reason why I should have to 
do this since I have the ability to keep the server simple.


What I would really like to do is send one request with the username and 
password to establish the session, and then make multiple authenticated 
requests where the session information remembers the authentication.


Is there a way to make this happen in python 2.5.2?

Keep in mind that this only needs to work with a particular server which 
I control.  It does not need to function as a general purpose browser.  
The server is powered by Django.


- Ken




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


cgi and POST

2009-08-28 Thread Ken Seehart
I can't seem to find an answer to this simple question on the web, and 
the documentation doesn't seem to indicate how to do this...


On the client I have:
   urllib.urlopen(uri, data)

This does a POST, but it's not obvious to me how this maps onto the 
various cgi examples which assume that the POST operation came from an 
html form with a file field.


In my case, there is no html form, and there is no file field, so please 
do not refer me to a page that just gives examples where the data is 
coming from a file transfer form.  I have already visited those pages.


My question is this:

Using cgi, how do I get the /data /(not the uri arguments) originating 
from a POST that did not originate from a form.


Thanks.

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


Re: cgi and POST

2009-08-28 Thread Ken Seehart

D'Arcy J.M. Cain wrote:

On Fri, 28 Aug 2009 13:59:57 -0700
Ken Seehart k...@seehart.com wrote:
  
Using cgi, how do I get the /data /(not the uri arguments) originating 
from a POST that did not originate from a form.



You don't care where it came from.  Just treat it exactly as if it came
from a form.
  
Thanks.  Anyway, the issue was that I made a mistake the urllib side of 
things.

Problem solved.

Ken

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


decorators - would be nice if...

2009-07-14 Thread Ken Seehart

Almost every time I use decorators, I find myself wishing I had access
to the local namespace of the context from which the decorator is
executed.  In practice, decorator is being applied to a method, so the
namespace in question would be the dictionary of the class being created.

Similarly, before decorators were around, I often found myself lamenting
the fact that a class is not created until after it's scope is
completed, since this makes certain kinds of metaprogramming more
difficult.  But after digging deeper, I realized why it is the way it
is, so I don't complain about that.

But it would have been a simple thing to add an argument 'locals' to the
decorator specification.  It's somewhat more difficult to add that now
because of compatibility issues, but perhaps there is a way to get the
same effect.

I can work around all of this by making my decorators just store data to
be processed by a metaclass, but that solution is less elegant.  Also,
it has the slight disadvantage of requiring the use of a metaclass which
would otherwise not be necessary.  I prefer to save metaclass
implementation as a last resort for various reasons (e.g. there does not
seem to be a good way to use multiple metaclasses in a class hierarchy,
and more generally you can't stack them on top of each other (you can
only have one metaclass per class)).

Thoughts?

- Ken


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


Re: Regarding Python is scripting language or not

2009-06-17 Thread Ken Seehart

abhishek goswami wrote:

Hi,
I have very basic question about Python that do we consider pyhton as 
script language.
I searched in google but it becomes more confusion for me. After some 
analysis I came to know that Python support oops .


Can anyone Guide me that Python is Oject oriented programming language 
or Script language


Abhishek Goswami
Chennai
Phone No -0996227099

ICC World Twenty20 England '09 exclusively on YAHOO! CRICKET 
http://in.rd.yahoo.com/tagline_cricket_3/*http://cricket.yahoo.com 
Both.  Especially if you define the terms object oriented and 
scripting in terms of positive space (attributes that are present) 
instead of negative space (attributes that are not present).


Python has all of the important features of OOP, but lacks some of the 
imposed constraints that some consider to be part of the definition of 
OOP (I personally do not consider the constraints to be central to a 
useful definition of OOP)


Python has all of the important features that one would want in a 
scripting language (although it has a larger memory footprint than some 
scripting languages), but lacks many of limitations associated with toy 
languages.


Python is somewhat unique in it's ability to fill both of these areas so 
well without being unnatural.  In my opinion, it is extremely useful to 
have one language that serves as both a highly scalable full-featured 
OOP language and as an extremely simple scripting language, because it 
allows one to build a large complex system within which one can easily 
do scripting tasks.


Proof that python is a scripting language:

   print Hello world

Proof that python is an OOP language benefiting from scripting capabilities:

  Django comes to mind.  There are hundreds of others out there, but 
instead of listing them, I think I will get back to programming.


Ken


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


Re: unsuccessful post request hangs, what gives?

2009-06-15 Thread Ken Seehart

Travis Altman wrote:
i'm trying to use a post request to authenticate to a web 
application.  let's say username = alice is valid but username = bob 
does not exits.  making the request with alice works like a charm but 
when i try username = bob it hangs?  any suggestions?

Can you show us the relevant code?
- Ken

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


xmlrpclib and generators

2009-06-10 Thread Ken Seehart
It would be really cool if an rpc call could return a generator.  I know 
that there are a few reasons why one would not expect it to be part of 
the library (e.g. the client would need to receive asynchronous 
messages, and it's not part of the rpc standard), however below I show a 
possible implementation, given that the client is also able to be a 
server...


I am aware of MultiCall, but that is actually something like the inverse 
of what I want.  I need a consumer.


Example:


# hypothetical client code (running at http://rpc.myserver.com;)

from fancyxmlrpc import FancyXMLRPCServer

server = FancyXMLRPCServer(('localhost', 9000))

def primes():
   n = 2
   p = []
   while True:
   if not any( n % f == 0 for f in p ):
   yield n
   p.append( n )
   n += 1

server.register_generator(primes)
server.serve_forever()


# hypothetical client code (running at http://www.mywebsite.com):

from fancyxmlrpc import FancyServerProxy

server_url = http://rpc.myserver.com;
local_url = http://www.mywebsite.com;

server = FancyServerProxy(server_url, local_url)

g = server.examples.getStateNames()

for x in g:
   print x


Okay, so to implement this, the trick would be to implement the 
generator wrapper as a hidden xmlrpc conversation.  On the client side, 
FancyServerProxy would encapsulate a hidden RPC server with a function 
that receives each item that is yielded by the server and queues them 
while the client generator consumes the queue and yields the items.


The practical constraints of my specific application are:
1. The rpc server is a highly specialized slave system that does heavy 
duty work.
2. The rpc client is itself a web server that dispatches work requests 
to the rpc server(s) and displays the current status of work done so far.
3. The generators will typically run for a long time (hours) and yield 
data periodically (perhaps once a minute).
4. Trusted users will write generators on the server and consumers on 
the client (web site) and use the web site to make requests.
5. It would be even better if my generator yields numpy arrays rather 
than lists.
6. It would be nice to be able to scale to allow the web site to 
dispatch to multiple work servers.



So my questions are:
1. Does using xmlrpc make any sense for this?
2. I am missing an easier way to do this?
3. Any good examples of things like this?


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


Re: xmlrpclib and generators

2009-06-10 Thread Ken Seehart

Hendrik van Rooyen wrote:

Ken Seehart  wrote:

8  implementation --

  

The practical constraints of my specific application are:
1. The rpc server is a highly specialized slave system that does heavy duty
work.

2. The rpc client is itself a web server that dispatches work requests to the

rpc server(s) and displays the current status of work done so far.

3. The generators will typically run for a long time (hours) and yield data 
periodically (perhaps once a minute).




If this yield can be made to be, or if it is, supply side driven, instead of
yielding on demand like a
generator, then I would set up a simple TCP/IP peer to peer socket link and just
send the result
back when it is ready.  If you have to serve more than one such link, it is a
simple matter
to keep a list of queues linking the different socket sets to the generator,
and to iterate over
the list, putting a copy of the thing that was just produced into each queue.
  
Of course, the thing you want to pass back must be serializable.
  
It is supply side driven, and the content would be serializable.  The 
only reason I want something high
level like xmlrpc or Pyro instead of rolling my own stuff with TCP/IP is 
that I want the client and server to be

easily customizable.

Have you looked at Pyro?
  
Not yet.  Looking at it now.  Thanks, it looks like Pyro may be very 
relevant.


Ken

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


Browser based Canvas UI?

2009-05-30 Thread Ken Seehart
A couple years ago I stumbled upon an interesting technology but I can't 
seem to find it, and I can remember what it is called.  Unfortunately 
this makes it difficult to search for.  I am am aware of several partial 
matches (items that meet a subset of the requirement listed below).  
Does anyone know what does /all/ of the following?


1. Works on at least FF and IE on XP and Linux out of the box, probably 
others


2. Does not require /any/ plugin download at all of any kind to view 
(this disqualifies flash, svg, silverlight, java, and others)


3. If you go to the web page for the first time on a freshly installed 
operating system, without admin privileges, you will see the 
functionality listed below immediately, and with no downloaded plugins 
and installers.  (I apologize for the redundancy, but I want to 
preemptively avoid a flood of non-applicable responses).


4. Graphics, including sprite animation

5. Dynamic response to mouse motion: dragging sprites for example

6. Programmable in Python, of course

Hints from what I can recall:
- Built from javascript as it's raw material under the hood (after all, 
it can't very well be anything else given requirements 1,2,3)
- Seems quite magical since I didn't know the necessary graphical raw 
materials existed in javascript
- I think it's based on Ajax, but I can't seem to find a relevant python 
demo of it due to too much clutter in my google searches


Ken

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


Re: How does Python's OOP feel?

2009-05-30 Thread Ken Seehart

Benjamin Kaplan wrote:



On Fri, May 29, 2009 at 9:41 PM, Lie Ryan lie.1...@gmail.com 
mailto:lie.1...@gmail.com wrote:


Ikon wrote:
 I'm rather new to Python. I have PHP for my main language and I do
 some Java. They all have a very strict OO schema. As I red through
 Python's tutorial it seams it has nothing of those rules. No
statical,
 abstract classes, functions, or variables.

 I wish someone, who has experience in both Java/PHP/C# and Python
 would tell me how mush better/worse is it to program in a language
 that misses most of the OO parts!

Some of the concepts that you are looking for might be present in Python 
but may look different than you expect.   If you are defining OO in 
terms of rules and constraints, Python might come up short.   But if you 
are looking for the /benefits/ of OOP, Python does quite well.


I have experience with all of those languages.  More Java than PHP and 
C#, so I can talk about Python and Java.


Originally, the definition of OOP was about concepts such as loose 
coupling, reusable components, focusing on things rather than 
procedures, etc.  Later the prevalent definition became more about what 
you are not allowed to do than how to do what you want to do.  So now we 
put more stress on ideas like strict encapsulation.  For example, some 
purists even suggest that you can't claim to implement encapsulation 
unless you guarantee that there is no possible way to determine 
implementation details from outside.  Python fails here because it has 
introspective features that allow you to bypass the limitations that are 
deemed necessary for some artificially strict definition of OOP.  For 
example strictly speaking we don't have private members, but we can 
start a name with underscore or double-underscore which for all 
practical purposes gives all the advantages of private members if we 
assume a certain level of maturity from our programmers.  A less 
restrictive notion of encapsulation might be that objects can be managed 
without the need to know the implementation details.


If you think in terms of the enabling qualities of OOP, but not the 
constraints, Python does in fact score quite high.  We have multiple 
inheritance, abstraction (in the non-fascist sense), encapsulation 
(again in the non-fascist sense), really easy polymorphism, and plenty 
of support for decoupling.


When people claim that python is not OO, they are for the most part only 
able to cite what python fails to prevent you from doing, not what 
python fails to allow you to do.  At the same time, I must admit that 
the constraints imposed by stricter languages, such as compile time 
detection of certain errors, and performance benefits associated with 
static typing.  Fortunately, tools such as pylint and psyco can close 
these two respective gaps to a significant degree.


Okay, enough theory and straw man arguments :)  Here's how it /feels/ to me.

What is missing from Python, as compared to Java, is an oppressive 
system to prevent you from writing bugs no matter what.  As it turn out, 
I found that I was quite capable of writing bugs in Java in spite of 
it's extreme efforts to save me from myself.  I also found that Java 
requires a great deal more lines of code to get to the desired results.  
Most of the lines of code are about making the compiler happy.  If I try 
to do anything metaprogrammingish in Java, I soon start pulling my hair 
out.  After switching to mostly python, I have grown nearly all of my 
hair back.


Once the Java compiler is happy with the syntax, certain kinds of bugs 
are prevented that might exist in corresponding python code.  This is a 
win for Java.  However, because the code is more complex in Java, and 
because it is harder to add clever introspective debugging 
instrumentation, the remaining bugs are harder to find.  I believe that 
all other things being equal, less code is more robust than more code.


I highly recommend using pylint, BTW.  If you use pylint, you are just 
about as safe as you would be with a more oppressive language.


When it is all done, I find that I can get to a bug free, scalable, 
flexible, object oriented result much more quickly in Python than in 
Java, but YMMV.


In summary, Python has all of the capabilities of OOP, but lacks some of 
the constraints of OOP.  This lack of constraints has it's advantages 
and disadvantages, but on the whole, I like it.  What else can I say?


See also:
 http://www.ferg.org/projects/python_java_side-by-side.html

Ken


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


Re: Browser based Canvas UI?

2009-05-30 Thread Ken Seehart

CTO wrote:

On May 30, 4:12 am, Ken Seehart k...@seehart.com wrote:
  

A couple years ago I stumbled upon an interesting technology but I can't
seem to find it, and I can remember what it is called.  Unfortunately
this makes it difficult to search for.  I am am aware of several partial
matches (items that meet a subset of the requirement listed below).  
Does anyone know what does /all/ of the following?


1. Works on at least FF and IE on XP and Linux out of the box, probably
others

2. Does not require /any/ plugin download at all of any kind to view
(this disqualifies flash, svg, silverlight, java, and others)

3. If you go to the web page for the first time on a freshly installed
operating system, without admin privileges, you will see the
functionality listed below immediately, and with no downloaded plugins
and installers.  (I apologize for the redundancy, but I want to
preemptively avoid a flood of non-applicable responses).

4. Graphics, including sprite animation

5. Dynamic response to mouse motion: dragging sprites for example

6. Programmable in Python, of course

Hints from what I can recall:
- Built from javascript as it's raw material under the hood (after all,
it can't very well be anything else given requirements 1,2,3)
- Seems quite magical since I didn't know the necessary graphical raw
materials existed in javascript
- I think it's based on Ajax, but I can't seem to find a relevant python
demo of it due to too much clutter in my google searches

Ken



Probably thinking of Pyjamas- URL: http://pyjamas.sourceforge.net/.
It lets you interact with canvas- URL: http://www.blobsallad.se/-
without writing any javascript.

Geremy Condra
  

Yeah, looks like pyjamas is probably it.  Thanks.

OMG,  http://www.blobsallad.se is way too cool, .but it's javascript, so 
I'm assuming the idea is that it could have been done in python and 
converted to javascript using pyjamas.


Anyway, I think I need to be more specific about the sprites:

7. Supports drawing semi-transparent bitmap sprites (preferably png)

Anyone happen to know where I can find an online example of animating 
with sprites (within the constraints of 1-7 above)?


Thanks,
- Ken

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


Re: What is the difference between init and enter?

2009-05-26 Thread Ken Seehart

Diez B. Roggisch wrote:

John wrote:

  

I'm okay with init, but it seems to me that enter is redundant since it
appears that anything you want to execute in enter can be done in init.



About what are you talking? 


Diez
  

Presumably, the 'with' statement.  http://www.python.org/dev/peps/pep-0343/

   *__enter__*(self)

   Enter the runtime context related to this object. The *with*
   http://effbot.org/pyref/with.htm statement will bind this method's
   return value to the target(s) specified in the *as*
   http://effbot.org/pyref/as.htm clause of the statement, if any.


Unlike __init__,  __enter__ can return a value, which is assigned to the 
variable (or tuple) following the 'as' keyword:


   with EXPR as VAR:
   BLOCK

Also, the object used in a with statement can be constructed prior to 
the with statement.  The __init__ method is called when the object is 
initialized, but the __enter__ method is called when the context is 
entered (i.e. when the 'with' statement is invoked).


Ken

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


Re: What text editor is everyone using for Python

2009-05-26 Thread Ken Seehart

Lacrima wrote:

I am new to python.
And now I am using trial version of Wing IDE.
But nobody mentioned it as a favourite editor.
So should I buy it when trial is expired or there are better choices?
  

It's my favorite.  Buy it.  I'm not aware of any better choices.

If you can afford the Pro version buy that, especially if you are 
planning any large projects.  But take a peek at the feature list before 
making that decision: http://www.wingware.com/wingide/features .  Some 
of my favorite Pro features are test suite support, advanced debugging, 
and code folding.  The debugger is way more powerful in the Pro version.


Of course, favorites are ultimately determined by religious preference.

Ken

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


Re: large array in a single line

2009-05-26 Thread Ken Seehart

Arnaud Delobelle wrote:

karthik...@gmail.com writes:

  

I would like to have a txt file of single line with
[1 2 3 .100]

I try something like
q=arange(100)
fl=file('tmp.ext','w')
fl.writelines(str(q))
fl.close()

Unfortunately my output is

[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22
23 24
 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
48 49
 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
73 74
 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
98 99]

ie there is automatic split in line after 76 characters. How do I
avoid it? Thanks.



You need to tell us more about the arange() function you use and what
object it returns.
  


That would be *numpy.arange

*Since numpy has it's own idea about formatting, you would have to roll 
your own.


How about:

   q=arange(100)
   *s = '[' + ' '.join([`x` for x in q]) + ']'*
   fl=file('tmp.ext','w')
   fl.*write*(s)
   fl.close()
 


See: http://docs.python.org/tutorial/datastructures.html#list-comprehensions

Incidentally, it's a funny thing that* writelines *coincidentally does 
what you expect it to do when given a string.  I suspect that you need 
to read the documentation on* writelines*:


file.writelines(/sequence/)
   Write a sequence of strings to the file. The sequence can be any
   iterable object producing strings, typically a list of strings.
   There is no return value. (The name is intended to match
   readlines(); writelines() does not add line separators.)

So you are asking it to effectively concatenate the characters in your 
string, (which gives you back your original string), and write the 
result.  Simply using* write *instead would be more efficient in this case.


Ken

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


Re: What text editor is everyone using for Python

2009-05-26 Thread Ken Seehart


Lacrima wrote:

I am new to python.
And now I am using trial version of Wing IDE.
But nobody mentioned it as a favourite editor.
So should I buy it when trial is expired or there are better choices?
  


Jean-Michel Pichavant wrote:

Why buy an IDE when you just need a text editor ? I don't get it.
Anyway gvim (aka vim aka vi) and emacs are the most powerful editors 
for years. Both have Windows and Linux version and most important, 
they both are very effective at editing any file type (python, C, 
latex, love letters...)
Emacs is more accessible to the newby but requires time to master its 
complex features.
Gvim may be complex for the newby due to its command/insertion mode 
constant switch, but is as powerful as emacs can be. Being a gvim 
adept, I should stat that gvim is far better but it would only feed 
the neverending war.


Jean-Michel


I've heard notepad is pretty good.  http://www.notepad.org/

:-) Ken


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


Re: Can I get a value's name

2009-05-15 Thread Ken Seehart

jalanb3 wrote:

Context for this question arises from some recent code. In particular the 
replace_line method, which takes in a regexp to look for, and a replacement 
for when it matches.

It is supposed to work for single lines only (we add ^ and $ to the regexp), so 
arguments which have '\n' in them are not accepted.

So at start of the method we search for such bad chars, and the code ends up 
something like this:

def replace_line(pattern,replacement):
errors = '\n' in pattern and [ 'pattern' ] or []
errors += '\n' in replacement and [ 'replacement' ] or []
values = [ locals()[e] for e in errors ]
# etc, etc, and eventually:
print 'Argument %s is bad : %s' % (errors[0],values[0])

And the question arises from that locals() line:
Given a variable name I can use locals() to get the value
Is there a way to do it the other way round
Given the value, can I get the variable name ?

For example, suppose I had started like this (using the variables, not strings 
with their names)

def replace_line(pattern,replacement):
values = '\n' in pattern and [ pattern ] or []
values += '\n' in replacement and [ replacement ] or []

Can I later get the name pattern via values[0]?

If this was an array in another language:
Of course not, values[0] is a copy of the value
so the connection to the variable is lost
But, AFAIK, in Python values[0] is just a rename of pattern
so there might be a way to get through to the original variable
  
No, values[0] gives a reference to the value stored at index 0 of the 
values array, which does not contain the name.
In particular, the name and all other information about the expression 
itself is lost.  A value generally does not contain the
pattern or any kind of name information (although class, method, and 
function instances contain some symbolic information in them).


Study the concept of 'references' and all of this will become more clear.

The only way to do something like what you want would be to search 
globals() or locals() for the value (depending on the context), but that 
would be an extremely ugly hack.  Chances are that if you want to do 
this, you need to rethink the problem instead.


# horribly bad, but sort of working, code follows:

foo = 'find me!'

def find_var(x):
   '''returns the name associated with a value'''
   for k,v in globals().items():
   if v == x:
   return k

print find_var(foo)

Note that a value can have several different names (and unnamed 
references) pointing at it, so the above code is not very general, but 
it may illustrate some interesting points.


My guess is that your entire approach may need rethinking.  What is the 
ultimate objective of your project?

Thank you for reading this far.
If you were now to start writing, I'd be very grateful indeed.

  


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


Re: Non-secure execution environment

2009-04-17 Thread Ken Seehart

roge...@gmail.com wrote:

Hi,

I am C++ guy for the most part and don't know much of Python, so,
please, bear with me if I am asking errrm..idiotic question.

Old rexec module provided kinda 'secure' execution environment. I am
not looking for security at this point. What I need an execution
environment which almost like rexec, but is non-secure.
  What I want is:
  separate global dictionary,
  separate list of imported modules,
  separate sys.path
  (optionaly) separate __builtins__

I might be able to get away without my own builtins, but the rest I
need.

If  it's any help, I plan to use it to execute embedded Python scripts
from C++.

Thanks,

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

  


As far as I know, you can't make multiple instances of the python 
environment from within python, but there is an easier way to get what 
you want, given that this is an embedding situation.


See: http://wingware.com/psupport/python-manual/1.5/api/initialization.html

You can use Py_NewInterpreter() to create multiple instances of python, 
which should give you the desired effect (though I have not tried this).


- Ken

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


Re: December 21, 2012: Judgment Day

2009-04-16 Thread Ken Seehart

MRAB wrote:

Mensanator wrote:

On Apr 16, 2:46 am, Thara tharasurya@gmail.com wrote:

Science can neither confirm nor discredit the validity of many
religiously or prophetically deemed judgment days of the future, the
soonest of which will be arriving December 21, 2012, the final day of
the Mayan Calendar.


No big deal, the last Harry Potter flick is scheduled for 2011.


Science can discredit just by waiting. It's worked so far! :-)
--
I'm sorry, but I have to disagree. We cannot discredit the assertion 
that the Harry Potter flick in 2011 will be the last one simply by 
waiting, since they can always make more Harry Potter flicks.  Unless, 
of course, judgment day intervenes.

- Ken

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


Re: Python inside C++

2009-04-14 Thread Ken Seehart

AJ Mayorga wrote:


Hello all,

 

I am looking for a way  to statically compile pythonxx.dll into my C++ 
application, so that I can use It as an internal scripting language 
and either run the native python code or take an ELF from 
py2exe/pyinstaller and run that.


The machines that will have my C++ app running on them do not have 
python and I cannot install it as part of my application. Any Ideas?


 


AJ



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

Sure, that's easy.

http://www.python.org/doc/2.5.2/ext/embedding.html

That should do what I think you want.

Picky note: The phrase statically compile pythonxx.dll into my C++ 
application is not quite correct.  A DLL is dynamically linked, not 
statically linked.  All this means is that you ship pythonxx.dll with 
your application along with any other DLLs and pyd files you might 
need.  But don't worry, you can do what you want.


Terms:
Embedding = Using python inside an application that is written in 
another language (usually C/C++).  This is what you are asking about.
Extending = Making your C/C++ code callable from python.  (there are 
actually several alternatives available, but that is another topic).


Enjoy,
Ken

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


Re: How do I count the distance between strings in a list?

2009-02-27 Thread Ken Seehart

collin wrote:

For example, if I were to have the code

randomlist = [1, 2, 3, 4]

And I want to count the distance between strings 1 and 4 which is
3, what command can I use to do this?
--
http://mail.python.org/mailman/listinfo/python-list

  

randomlist.index(4) - randomlist.index(1)

Ken

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


Re: some question about python2.6 and python3k

2009-01-04 Thread Ken Seehart

Michael Yang wrote:

Hi,guys

i am a new guy for python world,i have some question want to ask

1.should i learn about python2.6 or python3k?i heard of it has some 
difference from them

.
I think you should go directly to 3K to save your self the extra work of 
learning the differences.


The main advantage of using 2.6 is compatibility with the past.  People 
who have already developed major projects will use 2.6 for a while until 
they get around to converting (if ever).  But if you are new to python, 
this advantage is not very relevant to you, so you should start with 3K.

2.Do python3k has some good web framework(like web.py)?

I don't know whether web.py specifically works under python 3K, but it 
seems to be a fairly active project, so my uneducated guess is that it 
will soon if it doesn't already.  In any case 3K has plenty of direct 
support for all web related things.


Thanks !


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


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


Re: Structure using whitespace vs logical whitespace

2008-12-16 Thread Ken Seehart
 :-)  And I wouldn't even consider my 
proposed solutions to
be workarounds with respect to the alleged problem of syntactical 
whitespace.  I would want
to use the same approach in C or Java simply because I prefer a 
data-driven approach where
appropriate.  Python's container literals make it particularly easy to 
express data in your code.
It just so happens that there is a high correlation between the 
temptation to use indentation for
non-programmatic structure and the appropriateness of a data-driven 
implementation.


The problem with adding redundant syntactical forms (such as your 
proposed {{...}}), is that it

complicates the language.  This has two effects:

1. In order to master the language you have to learn more (mastering the 
language includes the

ability to read other peoples code as well as writing new code).

2. It produces greater variance in style based on personal preference.  
Generally, code is easier

to read when everyone uses consistent style.

One of the things that people like about python is the relative 
infrequency of special characters.
This gives python a certain flavor.  I happen to like this flavor a 
lot.  Some people don't, and I
recommend Perl to them.  But switching between two distinct dialects of 
python does not seem

like a wise idea.

I hope this helps.

Ken Seehart

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


Re: Guido's new method definition idea

2008-12-15 Thread Ken Seehart

-*ε*

Admittedly a tough call. I see the attraction of the proposed syntax.

Maybe somewhat more readable since the declaration syntax matches the 
usage syntax, which is nice. I think it would have been superior to the 
current syntax if it had been done that way in the first place. However, 
since newbies will still have to learn both syntaxes in order to read 
other peoples code, it does not simplify the language.


The main thing I don't like about it is that it violates this principle: 
There should be one-- and preferably only one --obvious way to do it.


Ken

Daniel Fetchinson wrote:

Hi folks,

The story of the explicit self in method definitions has been
discussed to death and we all know it will stay. However, Guido
himself acknowledged that an alternative syntax makes perfect sense
and having both (old and new) in a future version of python is a
possibility since it maintains backward compatibility. The alternative
syntax will be syntactic sugar for the old one. This blog post of his
is what I'm talking about:

http://neopythonic.blogspot.com/2008/10/why-explicit-self-has-to-stay.html

The proposal is to allow this:

class C:
def self.method( arg ):
self.value = arg
return self.value

instead of this:

class C:
def method( self, arg ):
self.value = arg
return self.value

I.e. explicit self stays only the syntax is slightly different and may
seem attractive to some. As pointed out by Guido classmethods would
work similarly:

class C:
@classmethod
def cls.method( arg ):
cls.val = arg
return cls.val

The fact that Guido says,

Now, I'm not saying that I like this better than the status quo. But
I like it a lot better than [...] but it has the great advantage that
it is backward compatible, and can be evolved into a PEP with a
reference implementation without too much effort.

shows that the proposal is viable.

I'd like this new way of defining methods, what do you guys think?
Anyone ready for writing a PEP?

Cheers,
Daniel

  


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


Memory problems

2008-11-27 Thread Ken Seehart
My beta testers are complaining about excessive memory usage.  It's a 
wxPython app with several embedded mozilla activex controls and a local 
web server.


Unfortunately, Python has some problems in this area.  In particular, 
since ubiquitous lists and dictionaries are dynamically resized as 
needed, memory fragmentation seems inevitable. 

Also, memory freed by python apparently is not returned to the OS 
according to this article:

http://effbot.org/pyfaq/why-doesnt-python-release-the-memory-when-i-delete-a-large-object.htm

Are there any good general countermeasures to make an application use 
less memory?  Any good overview articles on this subject?


Thanks (and Happy Thanksgiving),
- Ken Seehart

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


Re: Weirdness comparing strings

2008-09-30 Thread Ken Seehart
Instance comparison is not necessarily the same as string comparison.  
Neither __str__ nor __repr__ are implicitly used at all for comparison.


In fact, by default a pair of instances are not equal unless they are 
the same object.  To define comparison to mean something, you need to 
define __cmp__ or __eq__.


Trivial example of default comparison:

 class C:
...   pass
...
 c = C()
 d = C()
 c==d
False
 c==c
True

See http://docs.python.org/ref/customization.html for more details.

Ken


Mr.SpOOn wrote:

Hi,
I have this piece of code:

class Note():
  ...
  ...
 def has_the_same_name(self, note):
 return self == note

 def __str__(self):
 return self.note_name + accidentals[self.accidentals]

 __repr__ = __str__

 if __name__  == '__main__':
 n = Note('B')
 n2 = Note('B')
 print n
 print n2
 print n.has_the_same_name(n2)

I'd expect to get True, because their string representation is
actually the same, instead the output is:

B
B
False

I think I'm missing something stupid. Where am I wrong?
--
http://mail.python.org/mailman/listinfo/python-list

  


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


Advice for a replacement for plone.

2008-09-30 Thread Ken Seehart

I want a new python based CMS.  ... One that won't keep me up all night 

I've been fooling around with zope and plone, and I like plone for some 
things, such as a repository for online project documentation.  However 
for general-purpose web development it is too monolithic.  Is there 
anything out there with the following properties?


0. Web page being developed is a typical small business home page (i.e. 
not a newspaper or a blog).
1. Page owner can edit pages on line with no expertise (plone does great 
here).
2. Main page does not look plone-like.  For an example of a main page 
that does not look plone-like, see http://www.arbitrary.com/  Note the 
lack of CMS style navigation widgets.
3. Item 2 should be reachable with nearly no effort (plone fails utterly 
here).
4. Target viewer (not the owner), should /not/ see something that looks 
at all like a CMS system, but rather see exactly what the page owner 
wants the page to look like.
5. Page owner should be able to view and navigate the tree of contents, 
and select pages to edit with a wysiwyg editor (plone does great here)...

6. ... But the target viewer should not see this tree.  See items 2 and 4.
7. Potential to add python scripted pages of various kinds.

philosophy There are a couple different design approaches to making a 
development environment.  You can let the developer start with nothing, 
and provide the developer with tools to create something (e.g. zope, 
most plain text editors), or you can start with a finished result and 
let the developer rip out and discard what is not desired (e.g. plone).  
I often prefer to start with nothing.  It's a natural starting point.  
Note that systems that are based on starting with nothing can provide 
the benefits of starting with something by providing templates and such. 
/philosophy


I would love to see a system that would give me an editable Hello World 
page in under 5 minutes.  Hello World would be a page consisting of 
nothing but the words hello world (no tools, no navigation bar, and 
certainly no CMS navigation stuff) in a url such as 
www.myhelloworldwebsite.com/hello and a different url to edit the page, 
such as www.myhelloworldwebsite.com/hello/edit or 
www.mywebsite.com/edit/hello


If you are a plone fanatic and still think I should use plone, fine, but 
please let me know where I can find a Hello World kind of example that 
demonstrates items 2, 4, and 6.


In addition, I would like the ability to design the layout of the page 
independent of the content.  Plone has some nice features that would be 
very helpful, but again, getting to hello world seems difficult.


Just plain Zope does a pretty good job at some of this, but I haven't 
found a good online wysiwyg editor for the page owner to modify content.


Thanks for any ideas.

Ken

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


Re: Newbie question...

2008-09-30 Thread Ken Seehart

Ken D'Ambrosio wrote:
First, apologies for such a newbie question; if there's a better forum 
(I've poked around, some) feel free to point it out to me.  Anyway, a 
mere 25-odd years after first hearing about OOP, I've finally decided 
to go to it, by way of Python.  But this puzzles me:


import commands
free = commands.getoutput(free)
for line in free:
   print line,

Gives:
  t o t a l   u s e d  f r e e
 s h a r e d b u f f e r s   c a c h e d
M e m : 5 1 5 9 9 2   4 6 0 4 5 2 
5 5 5 4 0

   0 7 7 5 1 6 9 1 8 8 4
- / +   b u f f e r s / c a c h e :   2 9 1 0 5 2   2 
2 4 9 4 0


Why are there spaces between everything?  And how do I keep it from 
happening?  *confused*


Thanks much,

-Ken
** Posted from http://www.teranews.com **
--
http://mail.python.org/mailman/listinfo/python-list

The variable 'free' is a string containing all of the output, not a file 
object or a sequence of strings.  Therefore, when you iterate free you 
iterate a sequence of characters.  This is different than the case of 
iterating an open file, which would give you a sequence of lines as you 
expect.


So ...

  print line,

... prints each character followed by a space and no newline.

You can do this instead:

import commands
free = commands.getoutput(free)
print free


- Ken (that's my name too)

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


Re: Python String Immutability Broken!

2008-08-25 Thread Ken Seehart
You can also use ctypes to globally change the value of integers less 
than 101.   Personally, I don't particularly like the number 14.  I 
changed it to 9 and I am much happier now.


I love ctypes.  So cool.  It's not supposed to be safe.

   Life is either a daring adventure or nothing. Security does not
   exist in nature, nor do the children of men as a whole experience
   it. Avoiding danger is no safer in the long run than exposure.
   *Helen Keller http://www.quotationspage.com/quotes/Helen_Keller/*
   /US blind  deaf educator (1880 - 1968)/

Of course I would not hire anyone who believes this quote, other than 
Helen Keller, if she were still with us.


It is quite possible to write a small program that works using abused 
strings.  But my life better not depend on it.  Among other things, if 
you use the abused string as a key anywhere, you will not get correct 
results.  Trying to change the length of the string will cause 
disasters.  Lengthening a string will corrupt memory, and shortening the 
string will not shorten it but rather embed '\0' in it.


Ken

Hendrik van Rooyen wrote:
 
Patrick Maupin pmauail.com wrote:


  

Very entertaining.




Thanks. Nice to see that there is still some sense of humour
left somewhere - its all been so serious here lately - people
seem to forget that hacking is fun!

  

But let me get this straight:   Are you just complaining that if you
pass a string to an arbitrary C function using ctypes, that that
arbitrary function can modify the string?




Actually, I am not complaining - I am asking for advice on the side
effects of what I am doing, which is replacing a bunch of bits
in what is essentially an output bit field with the corresponding
input bits at the same addresses read back from a simulated i/o
bus structure.  And I would also like to know if there is a better
way of doing this.

The C code actually works, doing what was intended - the \xff that
one sees appearing back comes from the pullup resistors on the
eBox's i/o.  I can show that it is working by adding some resistance
and capacitance (by holding the connector against my tongue) in which
case I get a munged version of the fox back.  (- evidently my tongue
is not such a perfect communications medium as I would like to believe.)

Passing the fox is actually deceptive and misleading, as in real
use there would be no such correlation sideways across bits, as
they are just representations of output lines.
(Think coils in PLC jargon)

  

Because if you are, then I think you share a great deal of
responsibility for the death of that string -- sending the poor thing
to its grave through some unknown C function.



This string is NOT dead - it is alive, and not even stunned -
it just looks as if it is sleeping because of the \xff - which
comes from the fact that there is no real hardware out there yet.

The C functions are very simple ones actually - they just do
what are essentially Linux I/O system calls - setting direction
bits for a port (in or out) and then reading or writing the data.

- Hendrik



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

  


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


Re: Python String Immutability Broken!

2008-08-25 Thread Ken Seehart

Hendrik van Rooyen wrote:
 
...


Actually, I am not complaining - I am asking for advice on the side
effects of what I am doing, which is replacing a bunch of bits
in what is essentially an output bit field with the corresponding
input bits at the same addresses read back from a simulated i/o
bus structure.  And I would also like to know if there is a better
way of doing this.
  

Yes, there is a better way.  Use a character array instead of a string.

http://python.net/crew/theller/ctypes/tutorial.html#arrays

...

- Ken

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


SocketServer and long-polling connections

2008-08-25 Thread Ken Seehart

I apologize if this message is a repeat.  It looks like didn't get received.

I'm using SocketServer to implement a local server that serves comet 
long-polling connections. 

How do I increase the maximum number of open connections?  Currently it 
is limited to about 8 I think.  More than that and it seems to block on 
opening more connections until one of the other connections releases.


Is it possible that the limit may actually be imposed by the client in 
some bizarre way?  In this case all of the clients are local, and are 
multiple instances of the same mozilla activex DLL.



- Ken

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


SocketServer max connections

2008-08-25 Thread Ken Seehart
I'm using SocketServer to implement a local server that serves comet 
long-polling connections. 

How do I increase the maximum number of open connections?  Currently it 
is limited to about 8 I think.  More than that and it seems to block on 
opening more connections until one of the other connections releases.


- Ken

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

Re: automatically import modules upon interpreter invocation

2008-06-25 Thread Ken Seehart

Daniel Fetchinson wrote:

Hi folks, this seems like a very basic thing but I couldn't find a solution.
I always do the following after starting the python interpreter (on linux):

import rlcompleter
import readline
readline.parse_and_bind(tab: complete)

Is there a way of making python execute the above whenever it starts
up so that I don't have to type it all the time?

Cheers,
Daniel
  
environment variable PYTHONSTARTUP can be set to a python file that runs 
when you start the python interpreter


- Ken

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


opinion - file.readlines blemish

2007-11-03 Thread Ken Seehart


I was just reading about the new file.newlines method that was added per 
PEP 278.  I suspect Guido must have been looking in some other direction 
when this got added, because it seems unlikely to me that he would have 
let this get by...  Okay, maybe I'm being a little harsh :-)   Sorry, 
I'm picky about this kind of thing.  The again, maybe I'm just missing 
some key point.



 file.newlines

   *newlines*

   If Python was built with the *---with-universal-newlines* option to
   *configure* (the default) this read-only attribute exists, and for
   files opened in universal newline read mode it keeps track of the
   types of newlines encountered while reading the file. The values it
   can take are |'\r'|, |'\n'|, |'\r\n'|, *None*
   http://effbot.org/pyref/None.htm (unknown, no newlines read yet)
   or a tuple containing all the newline types seen, to indicate that
   multiple newline conventions were encountered. For files not opened
   in universal newline read mode the value of this attribute will be
   *None* http://effbot.org/pyref/None.htm.


It seems immediately obvious to me that the return value should always 
be a tuple, consisting of zero or more strings.  If built with universal 
newlines, it should return ('\n',) if a newline was found.


Perhaps there was some blurry contemplation that the None return value 
could identify that *universal-newlines* is enabled, but this is blurry 
because None could also just mean that no newlines have been read.  
Besides, it's not a good idea to stuff extra semantics like that.  
Better would be a separate way to identify *universal-newlines *mode.


Ken Seehart


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

UnicodeDecodeError

2007-07-21 Thread Ken Seehart
I get this whenever I encounter a non-ascii character in a non-unicode 
string:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xd1 in position 23: 
ordinal not in range(128)

The string in question is ... ESPA\xd1OL ...

I completely understand why I get the error, and my solution will be to 
simply convert to unicode (since the code that uses the string is 
unicode ready).

I am wondering if anyone knows where I can find a mapping from this 
particular extended ascii code (where \xd1 is Ñ), to the corresponding 
unicode characters.

Ken


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


Re: UnicodeDecodeError

2007-07-21 Thread Ken Seehart
Um, never mind.  The recent unicode conversation gave me my answer  :-)
unicode(s, 'Windows-1252')

Ken Seehart wrote:
 I get this whenever I encounter a non-ascii character in a non-unicode 
 string:

 UnicodeDecodeError: 'ascii' codec can't decode byte 0xd1 in position 23: 
 ordinal not in range(128)

 The string in question is ... ESPA\xd1OL ...

 I completely understand why I get the error, and my solution will be to 
 simply convert to unicode (since the code that uses the string is 
 unicode ready).

 I am wondering if anyone knows where I can find a mapping from this 
 particular extended ascii code (where \xd1 is Ñ), to the corresponding 
 unicode characters.

 Ken


   

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


C wrappers and the proxy dilemma

2007-06-22 Thread Ken Seehart
Anyone who has wrapped C or C++ libraries has encountered the proxy 
dilemma. 

A long time ago, I naively thought that I could start by deriving my 
high level python class from the c-python type, but this leads to many 
difficult problems because several of the underlying methods return the 
low level objects.  After reading up on the subject, I learned that the 
correct solution is to use composition rather than inheritance.  It 
makes sense, but it is nevertheless rather annoying.


An excellent example of this is the wxPython library, which uses 
composition (proxies) and even solves the OOR (original object return) 
problem that is associated with this kind of proxy oriented solution.


Conclusion: There is no really good solution to this kind of thing that 
doesn't get messy.  Wrapping C or C++ in python becomes difficult 
whenever you have methods that return (or take as arguments) instances 
of the objects that you are trying to wrap in high level python.  Any 
solution seems to add more overhead than is desirable (in terms of both 
programmer time and run time).


This problem comes up over and over again, so perhaps it is even worth a 
PEP if a change to the python language would facilitate a more 
convenient solution to this nagging problem.


First, mentally set aside the idea of using composition.  I understand 
the set of problems that it solves, but it also creates a new set of 
problems (such as function call overhead on every method call).  I also 
understand why composition is better than inheritance.  But in order to 
contemplate this proposal, it is necessary to temporarily set aside the 
composition idea.  This proposal involves taking another look at 
something slightly more akin to the inheritance approach that we all 
gave up on before.


Okay, so here is a somewhat radical proposal:

Add a writable  __overload__ attribute to low level python type.  This 
would be assigned a python 'type' instance (or None to have no effect).  
The effect is to place __overload__ at the /beginning /of the __mro__ of 
the low level type, making it possible to directly alter the behavior of 
the base python type without using inheritance.  This means that 
instances of the base type acquire the attributes of our high level 
python class.  It is important that the __overload__ type is checked 
/before /the actual type.


Yeah, it sounds a bit scary at first.  A bit too loose maybe.  So the 
type definition should have to explicitly enable this feature to prevent 
people from doing bad things.  But dwelling too much on the scariness 
seems somewhat non-pythonic.  It is more important that we can do really 
good things than that we prevent ourselves from going out of our way to 
do bad things (as long as it is not too /easy /to do bad things).


So here are some of the benefits:

1. Eliminates the duality between proxy objects and low level objects.

2. Increased speed on methods whose syntaxes are not being altered 
(because no wrapper is needed).


3. Much less work than writing wrappers for every method (even if such a 
task is partially automated).


Usage example:

from foolib import c_foo

class Foo(c_foo):
   Overload of c_foo

   def __new__(typ, *args, **kwargs):
   return c_foo(*args, **kwargs)

   def __init__(self, parrot_state):
   c_foo.__init__(self)
   self.parrot_state = parrot_state

   def myfunc(self):
   return 5 * self.juju(self.parrot_state) # juju method is defined 
in foolib


# This makes all c_foo instances into Foo instances:  Poof!
c_foo.c_footype.__overload__ = Foo



x = Foo('not dead yet') # actually creates a c_foo instance that looks 
like a Foo instance


# c_foo.Add is defined in foolib and takes c_foo instances
x.Add(Foo('strange'))
x.Add(Foo('missing'))

# c_foo.GetChildren is defined in foolib and returns c_foo instances
for y in x.GetChildren():
   print y.myfunc() # Wow, I am magically accessing Foo.myfunc


Yeah I know, that's an infinitely recursive inheritance; but that's 
okay, we just implement the __overload__ feature such that the MRO 
thingy will do the right thing for us.  The semantics will be that Foo 
is derived from c_foo, and all instances of c_foo behave as if the are 
instances of Foo.


I used the term radical earlier, to describe this idea.  What I meant 
was that it seems to violate some basic Object Oriented principles.  
However, when evaluating solutions I try not to apply any principles too 
dogmatically.  Theory is only relevant when it has /practical 
/consequences.  So I recommend not quoting theory without also defending 
the relevance of the theory to the case in point.  IMO, anything that 
eliminates thousands of lines of boilerplate code is worth bending 
theory a little.


Is it possible to get the same syntactic/semantic results without 
changing python?


- Ken

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

audio video streaming communications

2007-01-07 Thread Ken Seehart
Hello,

I am looking for a good audio/video conferencing library.  Ideally it 
should work with wxPython (or have some means of making it work there).

So far my main difficulty in my attempt at searching for such a package 
is that there is so much stuff out there on downloading music and videos.

I am not interested in download torrents, etc.  I'm just looking video 
conferencing, and live video broadcasts, etc.

Any recommendations?

Thanks,
- Ken


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


Re: Registration Code

2006-04-03 Thread Ken Seehart
Math wrote:
 Hello,
 
 I wonder if I can ask this particular question here...
 I'm writing this piece of Python Software and I'm almost done...:-)
 But now I want the end-user to register this software with a 
 registration code or perhaps something like an evaluation demo version 
 which expires after some period of time...
 Is this the right place to ask or does anybody know where to look for 
 more on the subject?
 
 Thanks
 

One thing I tried some time ago:

I encypted the bytecode of a few important functions with a key based on 
information required from the user.  Without the key, these functions 
can't be decrypted.  This is somewhat more secure than just testing the 
key with an if statement since the latter could easily be bypassed by 
a hacker.

To encrypt the function, I saved the bytecode of the target function to 
a file.  Then, in the version to be shipped, I substituted the encrypted 
version of bytecode for the original bytecode.  The rest of the program 
(including the part that asks for the authentication information) works 
fine.  If the authentication passes, the encrypted function(s) are 
decrypted.  Normally the encrypted functions are never executed of 
course (if authentication fails, the user is notified).  If a hacker 
bypasses the authentication test, the program will crash when the 
program attempts to executed the encypted bytecode.

How to determine the key depends on your application.  You may choose to 
include hardware serial numbers, for example.

- Ken

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


Re: Creating Pie Chart from Python

2005-09-15 Thread Ken Seehart
Thierry Lam wrote:
 Let's say I have the following data:
 
 500 objects:
 -100 are red
 -300 are blue
 -the rest are green
 
 Is there some python package which can represen the above information
 in a pie chart?
 
 Thanks
 Thierry
 

What is the user interface context?

Is it a web page?  Do you want to create image files with pie chart?  If 
  yes to either of these, try gdchart.

http://www.icewalkers.com/Linux/Software/52020/GDChart.html
http://athani.pair.com/msteed/software/gdchart/download.html




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


Re: Need help with C extension module

2005-09-13 Thread Ken Seehart
chris wrote:
 This is my first attempt at undertaking a C extension module.  I want
 to wrap an existing C library so I can call the functions from Python.
 There are only two functions I'm interested in calling.  I did mess
 with Pyrex a bit and Swig, to no avail, so I turned to doing it by
 hand.  Using the example in Programming Python, I did get the easier of
 the two functions working--only takes a string parameter.  I'm stuck
 now on the other function and not sure how to wrap it, because it
 involves some structs.  Here's a simplified version of the C:
 
 struct In
 {
   int x;
   char* s;
   ... (only primitive data types)
 };
 
 struct Out
 {
   int y;
   char* s;
   ... (only primitive data types)
 };
 
 Out* func(In* x, Out* y);
 
 So the function takes pointers to the two structs, and fills out the
 output struct and also returns the pointer to it.  I would envision the
 Python looking like
 
 in = In()
 in.y = 1
 in.s = abc
 ...
 
 out = func(in)
 
 maybe?  Just no idea how to deal with the structs in the C extension
 module code.
 
 Any tips appreciated.
 
 Thanks,
 Chris
 

Since others have responded about Pyrex, I'll just add my two cents,
which may or not apply to you.

The idea of filling in a struct, passing it to a function, then
returning a struct, is a typical c-ism.  If the only purpose of the
structs is to pass data to the function and receive the result, read on.
  On the other hand, if the structures represent things in the
object-oriented sense, just ignore what I am saying.

I would wrap the function such that the python code simply takes a set
of parameters and returns a tuple.  This is much more pythonic, and much
easier to code.

y,s,... = func(x,s,...)

My untested pseudocode would look something like this:

static PyObject *map_sweep(map__object *self, PyObject *args)
{
   In input;
   Out output;

   if (PyArg_ParseTuple(args, is..., (In.x), (In.s), ...))
   {
 return NULL;
   }

   func(In, Out);

   return Py_BuildValue(is..., Out.y, Out.s, ...);
}



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


Python for ARM7?

2005-09-13 Thread Ken Seehart
Hello.  Where might I find python binaries for ARM7 (Linux 2.4)?

I don't have an ARM7 compiler, and I probably don't have enough disk
space (about 3MB of flash available) for the complete build anyway.

My plan is to just copy the files I need.  This approach seems to work
on Windows XP, where I have a working python executable with the
libraries I need (python.exe, SimpleHTTPServer.py and dependencies), all
in about 1 MB.  The application is basically a web server.

If I absolutely have to build my own python, I would probably use a
cygwin (or maybe linux) cross-compiler for ARM7, but that seems like a
daunting task.  I'd much rather find someone who has already done it who
has the binaries :)

I'm pretty sure the only files I actually need are the python executable
and the _socket shared library.

Thanks,
- Ken


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


Re: Python for ARM7?

2005-09-13 Thread Ken Seehart
Sybren Stuvel wrote:
 Ken Seehart enlightened us with:
 
Hello.  Where might I find python binaries for ARM7 (Linux 2.4)?
 
 
 Check http://www.vanille.de/projects/python.spy
 
 
If I absolutely have to build my own python, I would probably use a
cygwin (or maybe linux) cross-compiler for ARM7, but that seems like a
daunting task.
 
 
 It is. From the above URL:
 
 : The build process of Python compiles a core part of it (the parser
 : generator pgen) and tries to execute that later in the build
 : process. This - of course - doesn't work for cross compiling.
 
 Sybren

Wow thanks!

Two more questions:

1. How do I know whether to use sharprom or modern?

2. What do I do with ipk files?  I surfed around and found that in one 
example, the command is ipkg install foo.ipk, but ipkg doesn't seem to 
exist on my hardware.

Thanks again!
- Ken

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


Re: Python for ARM7?

2005-09-13 Thread Ken Seehart
Sybren Stuvel wrote:
 Ken Seehart enlightened us with:
 
1. How do I know whether to use sharprom or modern?
  
 If it works, use it.

That makes sense :)

2. What do I do with ipk files?  I surfed around and found that in
one example, the command is ipkg install foo.ipk, but ipkg doesn't
seem to exist on my hardware.
 
 ipkg doesn't have anything to do with your hardware. It's just a shell
 script. Anyway, an ipkg file is nothing more than a tarball with a
 certain content.

 Sybren

That was a dumb way for me to put it :)  What I mean is that the linux 
installation on my hardware is so stripped down I don't have various 
files such as ipkg.  It's helpful to know that ipkg is a shell script 
instead of a binary, but I am also missing tar, ar, and gcc (which I 
assume I need libraries from).

I could try to unpack them on another (non-ARM7) linux box and then move 
the files over to the ARM7 device.  Better yet, can I unpack them on 
windows XP somehow?

If for some reason that is not possible, i suppose my next step is to 
find a compatible ARM7 linux installation to unpack on another machine 
to steal files from.  Unfortunately I don't have access to another ARM7 
computer, and I don't have room for a full linux installation on the 
ARM7 device that I am working with (it has a total of 4MB).  It there an 
easy to obtain the files I need without attempting to reintall linux?

The files I know about are:
   libgcc1 (= 3.4.3)  (exact file name unknown)
   libc6 (= 2.3.2+cvs20040726)  (exact file name unknown)
   ipkg (the shell script)
   tar
   ar
   (any other commands that ipkg runs)

- Ken


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


Re: which is more 'pythonic' / 'better' ?

2005-09-13 Thread Ken Seehart
Will McGugan wrote:
 gabor wrote:
 
 hi,

 there are 2 versions of a simple code.
 which is preferred?


 ===
 if len(line) = (n+1):
 text = line[n]
 else:
 text = 'nothing'
 ===


 ===
 try:
 text = line[n]
 except IndexError:
 text = 'nothing'
 ===


 which is the one you would use?
 
 
 I would actualy use the following for this particular case..
 
 text = line[n:n+1] or 'nothing'
 
 But in general I think it is best to use exceptions like that only where 
  you expect the code to _not_ throw the exception the majority of times. 
 Otherwise the simple condition is better. Although I expect there is not 
 much difference either way..
 
 
 Will McGugan
 -- 
 http://www.kelpiesoft.com

Hey are you a perl programmer?  That looks perlish to me.  A python 
programmer would never use or that way (even though it works).  :)

It's okay, I used to be a perl programmer too.  It's nothing to be 
ashamed of. :)

- Ken

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