Re: Generator vs functools.partial?

2012-06-21 Thread J. Cliff Dyer
On Thu, 2012-06-21 at 21:25 +1000, John O'Hagan wrote:
 Sometimes a function gets called repeatedly with the same expensive argument:
 
 def some_func(arg, i):
 (do_something with arg and i)
 
 same_old_arg = big_calculation()
 for i in lots_of_items:
 some_func(same_old_arg, i)
 

Another possibility is to refactor this into a callable class.

class DoesSomethingWithExpensiveData(object):
def __init__(self, arg):
self.arg = arg

def __call__(self, operand):
return sum([self.arg, operand])

func = DoesSomethingWithExpensiveData(big_calculation()):
for thing in a_bunch_of_things:
print func(thing)

Or you can write a function factory:

def get_func(arg):
def inner(operand):
return sum([arg, operand])
return inner

func = get_func(big_calculation())
for thing in a_bunch_of_things:
print func(thing)


 A simple case like that looks OK, but it can get messy when groups of 
 arguments
 are passed between functions, especially if the arguments are used by 
 functions
 called within the functions they are passed to (by other functions!).
 
 Maybe that's a code smell, but it can be cleaned up with:
 
 import functools
 some_func = functools.partial(some_func, big_calculation())
 for i in lots_of_items:
 some_func(i)
 
 But what about a generator?
 
 def some_func():
 arg = big_calculation()
 while 1:
 i = yield
 (do_something with arg and i)
 
 some_gen = some_func()
 some_gen.send(None)
 for i in lots_of_items:
 some_gen.send(i)
 
 I like this because it encapsulates the calculation of the arguments
 inside the function that is using them without repeating it, and there are no
 restrictions on argument order like partial. But sending None is annoying. :)
 
 Old news? Thoughts, criticisms, theories?
 
 --
 
 John  


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


Re: Academic citation of Python

2012-06-16 Thread J. Cliff Dyer
That's a rather vague question.  What do you want to cite about python?
If you're just mentioning python, that shouldn't warrant a citation,
though a parenthetical note linking to python.org might be useful.

The standard documentation should be acceptable, or possibly a link to
the source code at a given revision.

Cheers,
Cliff

On Sat, 2012-06-16 at 13:24 +1000, Mark Livingstone wrote:
 Hello!
 
 I wish to properly cite Python in an academic paper I am writing.
 
 Is there a preferred document etc to cite?
 
 Thanks in advance,
 
 MArkL


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


Re: Interprocess comunication

2012-06-07 Thread J. Cliff Dyer
On Thu, 2012-06-07 at 16:04 +, Julio Sergio wrote:

 Up to this point it worked as expected. However, when I tryied with the 
 methods 
 that write and read several lines, apparently the process got stalled:
 
 - fi.writelines([uno\n,dos\n,tres\n])
 - fi.flush()
 - s = fo.readlines()
 .
 .
 .

readlines() reads all the lines from the filehandle, but the filehandle
hasn't signalled that it is done writing lines, so fo is waiting until
fi is complete.  You either need to keep reading one line at a time, and
manually release control when there's nothing more to read, or you need
to do an fi.close() before you try to use fo.readlines().



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


Re: Interprocess comunication

2012-06-07 Thread J. Cliff Dyer
It is for reading all the lines from a complete file.  If the file is
still being written to, it doesn't have an end yet.  File objects do
many things besides RPC.  Also, there are instances where all you want
to do is block until the file is done, and then get all the content.
readlines will do that, too.



On Thu, 2012-06-07 at 16:39 +, Julio Sergio wrote:
 MRAB python at mrabarnett.plus.com writes:
 
  
  I believe it's waiting for the end of the input, i.e. for the pipe to
  close.
  
  Have you tried calling fo.readline() 3 times instead?
  
 
 yeah! It worked!...
 A question remains: what is then the purpose of fo.readlines(...)?
 
 Thanks,
 --Sergio
 
 
 
 


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


Re: I look for a package to make some simple console form

2012-04-02 Thread J. Cliff Dyer
You might look into formencode.  It basically takes the philosophy that
a form is nothing more and nothing less than an interface between user
input and python data.  It doesn't make assumptions about how you
present the form to the user.  It just handles validation and conversion
of that data into useful python objects, and from python objects to
displayable values.

http://www.formencode.org/en/latest/Validator.html

Might be what you're looking for.

Cheers,
Cliff


On Mon, 2012-04-02 at 14:55 +0200, Stéphane Klein wrote:
 Hi,
 
 I look for a package to make some console form.
 
 It's a standard stuff, I think there are a package to do that.
 
 Example :
 
 What is your name ?
 Select your lang [EN, FR, DE…] ?
 Do you want … [Y, N] ?
 
 Type of field :
 
 * textline
 * select choice
 * boolean question
 
 Thank for your help,
 Stéphane
 -- 
 Stéphane Klein steph...@harobed.org
 blog: http://stephane-klein.info
 Twitter: http://twitter.com/klein_stephane
 pro: http://www.is-webdesign.com
 


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


Re: help needed to understand an error message.

2012-03-30 Thread J. Cliff Dyer
So the problem is that python doesn't know what you're trying to do.  It
doesn't know that you meant to say print.  When the parser is looking
at the word Print, it assumes you are referencing an object named Print,
which is completely legal.  It's only once you've created the next
token, a string literal, that the parser discovers the error: you can't
have a string literal following a variable.  

*You* think your error is that you misspelled print.  The parser
thinks your error is trying to put a string literal next to a variable.

Cheers,
Cliff


On Mon, 2012-03-26 at 18:22 +0530, Aloke Ghosh wrote:
 Hi,
 I am learning Python and do not have programming experience.
 I was following
 an exercise from http://learnpythonthehardway.org/book/ex2.html
 and made a mistake in entry :
 
 
 PrintI like typing this. 
 
 
 and got the following error message:
 
 
 In [2]: PrintI like typing this.
 
File ipython console, line 1
  PrintI like typing this.
 ^
 SyntaxError: invalid syntax
 
 
 I feel the error is in Capital P in print .
 However the error indicated with ^ 
 hints at quote at the end of the line.
 
 
 Can any one please help me understand this.
 
 
 -- 
 A.K.Ghosh


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


Re: Is there any difference between print 3 and print '3' in Python ?

2012-03-26 Thread J. Cliff Dyer
As others have pointed out, the output is the same, because the result
of converting an integer to a string is the string of that integer.
However, other numeric literals might not do what you want, due to the
fact that they are converted to an internal numeric representation, then
converted back to a string in a canonical format.

 print 3, '3'
3 3
 print 3.00, '3.00'
3.0 3.00
 print 0x3, '0x3'
3 0x3
 print 03, '03'
3 03
 print 3e0, '3e0'
3.0 3e0

You might think that the take away message is to use the string
representation, since it prints what you tell it to print.  However, if
you use a number, you can specify the output formatting with more
fine-grained control, and even exert that control on calculated number:

 print '%0.2f' % (3,)
3.00
 print '%0.2f' % (2 + 1)
3.00

This is better because you can't perform math on a string:

 print '2' + '1'
21
 print '2.00' + '1.00'
2.001.00
print '2 + 1'
2 + 1

So in general, you should use numbers, and then format them using
standard string formatting operations when you want to print them.
There's more information on how to do formatting here:
http://docs.python.org/library/stdtypes.html#string-formatting


Cheers,
Cliff


On Mon, 2012-03-26 at 04:45 -0700, redstone-c...@163.com wrote:
 I know the print statement produces the same result when both of these
 two instructions are executed ,I just want to know Is there any
 difference between print 3 and print '3' in Python ?


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


Re: Python classes: Simplify?

2012-03-22 Thread J. Cliff Dyer
The issue of explicitly naming a self parameter has been discussed in
depth on a number of occasions.  I recommend a google search for python
implicit self for some of the reasons why it exists.  Here's what Guido
has to say about it:

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

Cheers,
Cliff



On Thu, 2012-03-22 at 13:15 +, Andrea Crotti wrote:
 On 03/22/2012 10:51 AM, Steven Lehar wrote: 
  It seems to me that the Python class system is needlessly confusing.
  Am I missing something? 
  
  
  For example in the class Complex given in the documentation
  
  
  class Complex:
  def __init__(self, realpart, imagpart):
  self.r = realpart
  self.i = imagpart
  
  
  x = Complex(3.0, -4.5)
  
  
  I initially found it profoundly confusing that __init__( ) calls for
  3 arguments, but you call Complex( ) with 2. Furthermore, why not
  call the initialization function after the class name as is done in
  other languages? Isn't that the simplest conceptually? Demonstrating
  with the above example:
  
  
  class Complex:
  def Complex(realpart, imagpart):
  Complex.r = realpart
  Complex.i = imagpart
  
  
  x = Complex(3.0, -4.5)
  
  
  Is there a good reason why classes cannot be defined that way?
  (Besides the problem of backward-compatibility)
  
  
 
 Some time ago I saw some nasty hack that allowed you to drop the self
 in the method declaration,
 using some crazy metaclass trick, but that's really not a good idea ;)
 
 I agree that is counter-intuitive but just set your editor to do that
 for you and you will be fine..
 
 in my emacs
 - s TAB - self
 - . TAB - self.
 - m TAB - def ${1:method}(self$2):
  $0
 
 so I never actually write the self..


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


Re: List comprehension/genexp inconsistency.

2012-03-21 Thread J. Cliff Dyer
Thanks, Ian.

That does seem to explain it.  The inner loop doesn't have access to the
class's name space, and of course you can't fix it by referencing Foo.y
explicitly, because the class isn't fully defined yet.

Ultimately, we realized that the dict should be created in the __init__
method, so that it gets the appropriate values of the foo and bar
attributes if the class is subclassed, which obviates the problem, but
it's a fascinating peek into python internals.

It looks like this is explained in the section of the pep entitled
Early Binding versus Late Binding
http://www.python.org/dev/peps/pep-0289/#early-binding-versus-late-binding


Cheers,
Cliff



On Tue, 2012-03-20 at 16:50 -0600, Ian Kelly wrote:
 On Tue, Mar 20, 2012 at 3:16 PM, Dennis Lee Bieber
 wlfr...@ix.netcom.com wrote:
  On Tue, 20 Mar 2012 16:23:22 -0400, J. Cliff Dyer
  j...@sdf.lonestar.org declaimed the following in
  gmane.comp.python.general:
 
 
  When trying to create a class with a dual-loop generator expression in a
  class definition, there is a strange scoping issue where the inner
  variable is not found, (but the outer loop variable is found), while a
  list comprehension has no problem finding both variables.
 
 Read http://www.python.org/dev/peps/pep-0289/ -- in particular, look
  for the word leak
 
 No, this has nothing to do with the loop variable leaking.  It appears
 to have to do with the fact that the variables and the generator
 expression are inside a class block.  I think that it's related to the
 reason that this doesn't work:
 
 class Foo(object):
 x = 42
 def foo():
 print(x)
 foo()
 
 In this case, x is not a local variable of foo, nor is it a global.
 In order for foo to access x, it would have to be a closure -- but
 Python can't make it a closure in this case, because the variable it
 accesses is (or rather, will become) a class attribute, not a local
 variable of a function that can be stored in a cell.  Instead, the
 compiler just makes it a global reference in the hope that such a
 global will actually be defined when the code is run.
 
 For that reason, what surprises me about Cliff's example is that a
 generator expression works at all in that context.  It seems to work
 as long as it contains only one loop, but not if it contains two.  To
 find out why, I tried disassembling one:
 
  class Foo(object):
 ... x = 42
 ... y = 12
 ... g = (a+b for a in range(x) for b in range(y))
 ...
  dis.dis(Foo.g.gi_code)
   4   0 LOAD_FAST0 (.0)
 3 FOR_ITER34 (to 40)
   6 STORE_FAST   1 (a)
   9 LOAD_GLOBAL  0 (range)
  12 LOAD_GLOBAL  1 (y)
  15 CALL_FUNCTION1
  18 GET_ITER
19 FOR_ITER15 (to 37)
  22 STORE_FAST   2 (b)
  25 LOAD_FAST1 (a)
  28 LOAD_FAST2 (b)
  31 BINARY_ADD
  32 YIELD_VALUE
  33 POP_TOP
  34 JUMP_ABSOLUTE   19
37 JUMP_ABSOLUTE3
40 LOAD_CONST   0 (None)
  43 RETURN_VALUE
 
 So that explains it.  Notice that x is never actually accessed in
 that disassembly; only y is.  It turns out that the first iterator
 [range(x)] is actually created before the generator ever starts
 executing, and is stored as an anonymous local variable on the
 generator's stack frame -- so it's created in the class scope, not in
 the generator scope.  The second iterator, however, is recreated on
 every iteration of the first iterator, so it can't be pre-built in
 that manner.  It does get created in the generator scope, and when
 that happens it blows up because it can't find the variable, just like
 the function example above.
 
 Cheers,
 Ian


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


Re: Best way to disconnect from ldap?

2012-03-21 Thread J. Cliff Dyer
Write a context manager.  

Then you just do

with MyLDAPWrapper() as ldap
   ldap.this()
   ldap.that()

and when you leave the scope of the with statement, your ldap __exit__
method will get called regardless of how you left.

Cheers,
Cliff


On Wed, 2012-03-21 at 19:30 +, John Gordon wrote:
 I'm writing an application that interacts with ldap, and I'm looking
 for advice on how to handle the connection.  Specifically, how to
 close the ldap connection when the application is done.
 
 I wrote a class to wrap an LDAP connection, similar to this:LDAP
 
 import ldap
 import ConfigParser
 
 class MyLDAPWrapper(object):
 
 def __init__(self):
 
 config = ConfigParser.SafeConfigParser()
 config.read('sample.conf')
 
 uri = config.get('LDAP', 'uri')
 user = config.get('LDAP', 'user')
 password = config.get('LDAP', 'password')
 
 self.ldapClient = ldap.initialize(uri)
 self.ldapClient.simple_bind_s(user, password)
 
 My question is this: what is the best way to ensure the ldap connection
 gets closed when it should?  I could write an explicit close() method,
 but that seems a bit messy; there would end up being lots of calls to
 close() scattered around in my code (primarily inside exception handlers.)
 
 Or I could write a __del__ method:
 
 def __del__(self):
 self.ldapClient.unbind_s()
 
 This seems like a much cleaner solution, as I don't ever have to worry
 about closing the connection; it gets done automatically.
 
 I haven't ever used __del__ before.  Are there any 'gotchas' I need to
 worry about?
 
 Thanks!
 
 -- 
 John Gordon   A is for Amy, who fell down the stairs
 gor...@panix.com  B is for Basil, assaulted by bears
 -- Edward Gorey, The Gashlycrumb Tinies
 


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


List comprehension/genexp inconsistency.

2012-03-20 Thread J. Cliff Dyer
One of my coworkers just stumbled across an interesting issue.  I'm
hoping someone here can explain why it's happening.

When trying to create a class with a dual-loop generator expression in a
class definition, there is a strange scoping issue where the inner
variable is not found, (but the outer loop variable is found), while a
list comprehension has no problem finding both variables.

Demonstration:

 class Spam:
... foo, bar = 4, 4
... baz = dict(((x, y), x+y) for x in range(foo) for y in
range(bar))
... 
Traceback (most recent call last):
  File stdin, line 1, in module
  File stdin, line 3, in Spam
  File stdin, line 3, in genexpr
NameError: global name 'bar' is not defined
 class Eggs(object):
... foo, bar = 4, 4
... baz = dict([((x, y), x+y) for x in range(foo) for y in
range(bar)])
... 
 

This was discovered in python 2.6.  In python 3.2, both versions fail
with the same NameError.  

Obviously, this is easy enough to work around.  I'm curious though:
What's going on under the hood to cause the nested generator expression
to fail while the list comprehension succeeds?

Cheers,
Cliff


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


Re: super() woes (n00b)

2010-06-17 Thread J. Cliff Dyer
On Thu, 2010-06-17 at 16:36 +, Deadly Dirk wrote:
 I cannot get right the super() function:
 Python 3.1.1+ (r311:74480, Nov  2 2009, 14:49:22) 
 [GCC 4.4.1] on linux2
 Type copyright, credits or license() for more information.
  No Subprocess 
  class P:
 def __init__(__class__,self):
 print(I am a member of class P)
 
 
  class C(P):
 def __init__(self):
 super().__init__(self)
 print(I am a member of class C)
 
 
 
 class P:
 def __init__(self):
 print(I am a member of class P)
 
 class C(P):
 def __init__(self):
 super().__init__(self)
 print(I am a member of class C)
 
 x=C()
 
 That is more or less the text from the Quick Python Book. What am I 
 doing wrong?
 

super gives you an instantiated version of the super class, which means
that you don't have to explicitly send self to any methods you call on
it.  

So use `super().__init__()` instead.
 -- 
 The missionaries go forth to Christianize the savages - 
 as if the savages weren't dangerous enough already.


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


Re: optional optional args vs optional positional options

2010-06-02 Thread J. Cliff Dyer
+1  

Options are options, arguments are arguments.  An optional argument is
not an option.  It is an argument that can be left out.



On Wed, 2010-06-02 at 12:42 +0200, Antoine Pitrou wrote:
 On Wed, 2 Jun 2010 01:49:18 -0700 (PDT)
 Michele Simionato michele.simion...@gmail.com wrote:
  
  Notice that optparse is basically useless in the use case Tim is
  considering (positional arguments) since it only manages options.
 
 By the way, could you stop naming these optional arguments, since
 positional arguments can be optional as well? It is confusing :)
 
 Thanks
 
 Antoine.
 
 


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


Re: how to preserve hex value

2010-05-19 Thread J. Cliff Dyer
On Wed, 2010-05-19 at 11:38 -0700, Back9 wrote:
 Hi,
 
 When converting a hex value, I'd like to preserve the decimal
 position.
 For example, 0x0A is converted to 0A not just A in string.
 
 How do I do this?
 
 TIA

I'm not sure I understand what your use case is, but generally speaking,
it is better to treat hex values as integers (which they are) than as
string (which they are not).  0x0a is an integer value of ten.  There is
no change of decimal position gained by prepending a zero digit.  It
means exactly the same thing.  If you know how large a chunk is, you can
multiply (or bit-shift) and add to get the behavior you're looking for.

def concat(byte1, byte2):
return (byte1  8) + byte2

 hex(concat(0x43, 0x0a))
0x430a

One common use case is when using hex notation to represent sequences of
bytes.  In this case, you may want to work with a byte string instead.
To concatenate your numbers this way, convert each number to a byte
using chr(x) (Python 2.x)

To print it as hex, do something like this:  

def bytestring_to_hex(s):
return '0x + ''.join('%02x' % ord(x) for x in s) 

However, if you need an arbitrary number of zeros preserved, you're out
of luck.  They are semantically meaningless in python.  (Is semantically
meaningless redundant?)

Cheers,
Cliff



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


Re: Regular expression

2010-05-18 Thread J. Cliff Dyer
Don't use regular expressions for that.

s = '0x340x5A0x9B0xBA'
return '0x' + ''.join(s.split('0x'))

On Tue, 2010-05-18 at 06:48 -0700, Back9 wrote:
 Hi,
 
 I have a string like this:
 0x340x5A0x9B0xBA
 I want to extract 0x from the string but the first one.
 
 How I can use re for this case?
 
 The string size will vary.
 
 TIA
 


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


Re: unittest not being run

2010-05-10 Thread J. Cliff Dyer
My guess is you mixed tabs and spaces.  One tab is always treated by the
python interpreter as being equal to eight spaces, which is two
indentation levels in your code.

Though if it were exactly as you show it, you'd be getting a syntax
error, because even there, it looks like the indentation of your `def
test_T1(self):` line is off by one column, relative to pass, and by
three columns relative to the other methods.

Cheers,
Cliff
 

On Mon, 2010-05-10 at 13:38 +0100, John Maclean wrote:
 hi,
 
 can some one explain why the __first__ test is not being run?
 
 #!/usr/bin/env python
 import unittest # {{{
 class T1TestCase(unittest.TestCase):
 
  def setUp(self):
  pass  # can we use global variables here?
 
  def tearDown(self):
  pass  # garbage collection
 
   def test_T1(self):
   '''this test aint loading'''
   self.assertEquals(1, 0)
 
  def test_T2(self):  ## test method names begin 'test*'
  self.assertEquals((1 + 2), 3)
  self.assertEquals(0 + 1, 1)
 
  def test_T3(self):
  self.assertEquals((0 * 10), 0)
  self.assertEquals((5 * 8), 40)
 
 # the output is better. prints each test and ok or fail
 suite = unittest.TestLoader().loadTestsFromTestCase(T1TestCase)
 unittest.TextTestRunner(verbosity=2).run(suite) # }}}
 
 
 ''' halp!
 
 the first test ain't loading...
 
 python blaht.py
 test_T2 (__main__.T1TestCase) ... ok
 test_T3 (__main__.T1TestCase) ... ok
 
 --
 Ran 2 tests in 0.000s
 
 OK
 
 '''



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


Re: Frustration debugging serial code

2010-05-07 Thread J. Cliff Dyer
On Fri, 2010-05-07 at 15:36 -0400, William R. Wing wrote:

 
 Maybe I should have been more explicit.  The first line in the Python
 file is:
 
 
 #!/usr/bin/env Python (alternatively #!/usr/bin/Python - same results
 either way).
 

python should be lowercased when referring to the name of the
executable.  Unix filesystems are, of course, case sensitive.




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


Re: Frustration debugging serial code

2010-05-07 Thread J. Cliff Dyer
On Fri, 2010-05-07 at 15:36 -0400, William R. Wing wrote:

 
 Maybe I should have been more explicit.  The first line in the Python
 file is:
 
 
 #!/usr/bin/env Python (alternatively #!/usr/bin/Python - same results
 either way).
 

python should be lowercased when referring to the name of the
executable.  Unix filesystems are, of course, case sensitive.





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


Re: Python dot-equals (syntax proposal)

2010-04-30 Thread J. Cliff Dyer
That's kind of a nifty idea.  However, python is currently under a
syntax moratorium.  No syntax changes will be accepted for at least 24
months starting from the release date of Python 3.1.  See more details
here: http://www.python.org/dev/peps/pep-3003/

Cheers,
Cliff


On Fri, 2010-04-30 at 09:04 -0700, Jabapyth wrote:
 At least a few times a day I wish python had the following shortcut
 syntax:
 
 vbl.=func(args)
 
 this would be equivalent to
 
 vbl = vbl.func(args)
 
 example:
 
 foo = Hello world
 foo.=split( )
 print foo
 # ['Hello', 'world']
 
 and I guess you could generalize this to
 
 vbl.=[some text]
 #
 vbl = vbl.[some text]
 
 e.g.
 
 temp.=children[0]
 # temp = temp.children[0]
 
 thoughts?


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


Re: how to select column

2010-04-26 Thread J. Cliff Dyer
It depends on what you mean by a column.  I assume your data is more
complex than what you've shown us.  

If your data is really single words separated by spaces, you can do:

for line in open('file'):
columns = line.split()
return columns[0], columns[3]

If your columns can have spaces within them, or are separated in other
ways, you'll need something else.  

Cheers,
Cliff


On Mon, 2010-04-26 at 14:50 +, mannu jha wrote:
 Dear all,
 
 I am new in python, can anyone help me that how can I select two
 column out of 6 column from a file.
 For example if I have file like:
 
 a1 a2 a3 a4 a5 a6
 b1 b2 b3 b4 b5 b6
 c1 c2 c3 c4 c5 c6
 d1 d2 d3 d4 d5 d6 
 
 and I want output like:
 
 a1 a4
 b1 b4
 c1 c4
 d1 d4
 
 then how to do
 
 Thanks 


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


Re: Difficulty w/json keys

2010-04-23 Thread J. Cliff Dyer

You need to know what your input data actually looks like, and the best
thing for that is a little bit of formatting.  I bet you can figure out
the problem yourself, once you see the structure of your data more
clearly.  I've reformatted the JSON for you to help out.


On Fri, 2010-04-23 at 07:20 -0700, Red wrote:
 -- Two Sample Twitter lines -
{
  text:tech managers what size for your teams? better to have 10-20
ppl per manager or 2-5 and have the managers be more hands
on?,
  in_reply_to_user_id:null,
  coordinates:null,
  geo:null,
  created_at:Thu Apr 22 17:35:42 + 2010,
  contributors:null,
  source:a href=\http://twitterfeed.com\; rel=\nofollow
\twitterfeed/a,
  in_reply_to_status_id:null,
  place:null,
  truncated:false,
  in_reply_to_screen_name:null,
  user: {
favourites_count:0,
profile_text_color:00,
time_zone:Eastern Time (US  Canada),
created_at:Tue Oct 27 19:50:51 + 2009,
statuses_count: 286,
notifications:null,
profile_link_color:ff,
description:I write code and talk to people.,
lang:en,
profile_background_image_url:http://s.twimg.com/a/
1271891196/images/themes/theme1/bg.png,
profile_image_url:http://s.twimg.com/a/1271891196/images/
default_profile_0_normal.png,
location:Near the water.,
contributors_enabled:false,
following:null,
geo_enabled:false,
profile_sidebar_fill_color:e0ff92,
profile_background_tile:false,
screen_name:sstatik,
profile_sidebar_border_color:87bc44,
followers_count: 40,
protected:false,
verified:false,
url:http://elliotmurphy.com/;,
name:statik,
friends_count:18,
id:85646316,
utc_offset:-18000,
profile_background_color:9ae4e8
  },
  id: 12651537502,
  favorited:false
}
{
  delete: {
status:{
  id:12650137902,
  user_id:128090723
}
  }
}


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


Re: question about list extension

2010-04-16 Thread J. Cliff Dyer
On Sat, 2010-04-17 at 00:37 +1000, Lie Ryan wrote:
 On 04/16/10 23:41, J wrote:

  So, what I'm curious about, is there a list comprehension or other
  means to reduce that to a single line?
 
 from itertools import chain
 def printout(*info):
 print '\n'.join(map(str, chain(*info)))
 
 or using generator comprehension
 
 from itertools import chain
 def printout(*info):
 print '\n'.join(str(x) for x in chain(*info))


It's even easier if you don't need to modify lista.  

print lista + listb


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


Re: Globally override built-in print function?

2010-04-16 Thread J. Cliff Dyer
On Fri, 2010-04-16 at 09:50 -0700, Dave W. wrote:
  old_print = __builtins__.print
  __builtins__.print = printhook
  yield
  __builtins__.print = old_print
 
  I'm pretty sure this is semantically equivalent to my original
  code, but I gave it a try anyway.
 
  Not at all. Declaring global print then assigning to print
  simply changes what the module's print variable refers to. Other
  modules are unaffected.  Global variables aren't truly global;
  they are actually local to the module.  You need to replace it in
  the __builtins__ because that's where everyone else gets it.
 
   FWIW, it doesn't work, either. :-}
 
  Right. Lie answered why. I didn't pay attention and thought you
  were already using Python 3.
 
 Thanks, Robert and Lie for the considered and informative responses.
 Getting feedback like this from people who really understand
 Python's internals is invaluable.  Sounds like redirecting
 stdout/stderr is the way to go.  (Especially given that they're not
 the 'real' stdout/stderr---that was news to me!)
 
 [xref Suppress output to stdout/stderr in InteractiveInterpreter]

It's good to remember that names in python are just names.  The objects
that have the names sys.stdout and sys.stderr are the real deal, but
when you assign a file object to them, you are not actually
redirecting anything.  You are assigning a name (sys.stdout) to a
different file object.  The old object still points to STDOUT, but
sys.stdout no longer refers to that object as long as your assignment
remains in scope.

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


Re: Urllib2 urlopen and read - difference

2010-04-15 Thread J. Cliff Dyer
On Thu, 2010-04-15 at 11:25 -0700, koranthala wrote:
 Hi,
Suppose I am doing the following:
 req = urllib2.urlopen('http://www.python.org')
 data = req.read()
 
When is the actual data received? is it done by the first line? or
 is it done only when req.read() is used?
   My understanding is that when urlopen is done itself, we would have
 received all the data, and req.read() just reads it from the file
 descriptor.
   But, when I read the source code of pylot, it mentioned the
 following:
 resp = opener.open(request)  # this sends the HTTP request
 and returns as soon as it is done connecting and sending
 connect_end_time = self.default_timer()
 content = resp.read()
 req_end_time = self.default_timer()
 
 Here, it seems to suggest that the data is received only after you do
 resp.read(), which made me all confused.
 
 If someone could help me out, it would be much helpful.

My understanding (please correct me if I'm wrong), is that when you call
open, you send a request to the server, and get a response object back.
The server immediately begins sending data (you can't control when they
send it, once you've requested it).  When you call read() on your
response object, it reads all the data it has already received, and if
that amount of data isn't sufficient to handle your read call, it blocks
until it has enough.

So your opener returns as soon as the request is sent, and read() blocks
if it doesn't have enough data to handle your request.

Cheers,
Cliff

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


Re: Urllib2 urlopen and read - difference

2010-04-15 Thread J. Cliff Dyer
On Thu, 2010-04-15 at 11:25 -0700, koranthala wrote:
 Hi,
Suppose I am doing the following:
 req = urllib2.urlopen('http://www.python.org')
 data = req.read()
 
When is the actual data received? is it done by the first line? or
 is it done only when req.read() is used?
   My understanding is that when urlopen is done itself, we would have
 received all the data, and req.read() just reads it from the file
 descriptor.
   But, when I read the source code of pylot, it mentioned the
 following:
 resp = opener.open(request)  # this sends the HTTP request
 and returns as soon as it is done connecting and sending
 connect_end_time = self.default_timer()
 content = resp.read()
 req_end_time = self.default_timer()
 
 Here, it seems to suggest that the data is received only after you do
 resp.read(), which made me all confused.
 
 If someone could help me out, it would be much helpful.

My understanding (please correct me if I'm wrong), is that when you call
open, you send a request to the server, and get a response object back.
The server immediately begins sending data (you can't control when they
send it, once you've requested it).  When you call read() on your
response object, it reads all the data it has already received, and if
that amount of data isn't sufficient to handle your read call, it blocks
until it has enough.

So your opener returns as soon as the request is sent, and read() blocks
if it doesn't have enough data to handle your request.

Cheers,
Cliff


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


Re: Unit testing errors (testing the platform module)

2010-04-14 Thread J. Cliff Dyer
On Wed, 2010-04-14 at 15:51 +0100, john maclean wrote:
 self.assertEqual(platform.__builtins__.__class__, dict,
 platform.__class__ supposed to be dict)
 self.assertEqual(platform.__name__, 'platform' ) 

The preferred spelling for:

platform.__builtins__.__class__ 

would be

type(platform.__builtins__)

It's shorter and easier to read, but essentially says the same thing.

You can also use it on integer literals, which you can't do with your
syntax:

 type(1)
type 'int'
 1.__class__
...
SyntaxError: invalid syntax

Admittedly, this is a trivial benefit.  If you're using a literal, you
already know what type you're dealing with.

Cheers,
Cliff

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


Re: Unit testing errors (testing the platform module)

2010-04-13 Thread J. Cliff Dyer
The problem is that the class of platform.__builtins__ is a dict, not a
string containing the text type 'dict'.  

Try replacing line 16 with this:

self.assertEqual(type(platform.__builtins__), dict)

Cheers,
Cliff


On Tue, 2010-04-13 at 15:01 +0100, John Maclean wrote:
 I normally use  languages unit testing framework to get a better
 understanding of how a language works. Right now I want to grok the
 platform module;
 
 
  1 #!/usr/bin/env python
   2 '''a pythonic factor'''
   3 import unittest
   4 import platform
   5
   6 class TestPyfactorTestCase(unittest.TestCase):
   7 def setUp(self):
   8 '''setting up stuff'''
  13
  14 def testplatformbuiltins(self): 15
 '''platform.__builtins__.blah '''
  16 self.assertEquals(platform.__builtins__.__class__, type 'd
 ict')
  17
  18
  19 def tearDown(self):
  20 print 'cleaning stuff up'
  21
  22 if __name__ == __main__:
  23 unittest.main()
 
 
 Is there an error in my syntax? Why is my test failing? Line 16.
 
 
 python stfu/testing/test_pyfactor.py
 Fcleaning stuff up
 
 ==
 FAIL: platform.__builtins__.blah
 --
 Traceback (most recent call last):
   File stfu/testing/test_pyfactor.py, line 16, in testplatformbuiltins
 self.assertEquals(platform.__builtins__.__class__, type 'dict')
 AssertionError: type 'dict' != type 'dict'
 
 --
 Ran 1 test in 0.000s
 
 FAILED (failures=1)
 
 -- 
 John Maclean MSc. (DIC) Bsc. (Hons),Core Linux Systems Engineering,07739
 171 531


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


Re: New to Python

2010-02-10 Thread J. Cliff Dyer
On Wed, 2010-02-10 at 13:18 -0800, Stephen Hansen wrote:

 
 The original code:
 
 
 s = f.readline()
 if 'mystring' in s: print 'foundit'
 if 'mystring' not in s: print 'not found'
 if 'mystring' in s:
   print 'processing'
 
 
 ... will only work on Python 2.x, as print is being used as a
 statement. If you change print to a function, that code will work in
 either Python 2.x or Python 3.x.
 
 
 However, in both CPython and IronPython, the above is provably correct
 code. It works fine: with those five lines alone, you are guaranteed
 to get 'foundit' followed by 'processing' if mystring is in s. With
 those five lines alone, you must get that result. If you aren't, then
 there's something else going on before or after-- and its not about
 IronPython vs CPython. To help diagnose what's wrong, copy and paste
 -real- results from a command prompt or interpreter when you run it,
 providing complete code. Something else is going wrong, you're looking
 in the wrong place to find the solution.

One other assumption that you are making is that f is a file-like object
that supports a reasonalbe readline function.

Quin: can you Ctrl-C, Ctrl-V the following code into a python file, by
itself, and run it to reproduce your problem?

from StringIO import StringIO
f = StringIO('This string contains mystring')
s = f.readline()
if 'mystring' in s: print 'foundit'
if 'mystring' not in s: print 'not found'
if 'mystring' in s:
   print 'processing'

# Should print:
# foundit
# processing
f = StringIO('This string does not contain MyStRiNg')
s = f.readline()
if 'mystring' in s: print 'foundit'
if 'mystring' not in s: print 'not found'
if 'mystring' in s:
   print 'processing'
# Should print:
# not found

And please, humor me and copy and paste the exact results as returned by
your IronPython interpreter. 



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


Re: Another Screwy Problem

2010-01-09 Thread J. Cliff Dyer
On Sat, 2010-01-09 at 07:59 -0500, Victor Subervi wrote:
 On Fri, Jan 8, 2010 at 4:44 PM, J. Clifford Dyer
 j...@sdf.lonestar.org wrote:
 Victor Subervi wrote:
  Hi;
  I have this line of code:
   sql = 'select Name, Price from %sPackages where ID=%s;' %
 (store, pid)
  which prints to this:
   select Name, Price from productsPackages where ID=1;
  which when I enter it into the MySQL interpreter gives me
 this:
  mysql select Name, Price from productsPackages where ID=1;
  +--++
  | Name | Price  |
  +--++
  | pkg  | 123.45 |
  +--++
  1 row in set (0.00 sec)
 
  exactly what I expect. However, in my script for some reason
 it returns
  this:
  ((1,),)
 
 
 
 First, got your other email. I thought I had executed the statement.
 Oops. Works fine now. Sorry.
  
 First, never use string formatting to pass parameters to your
 database.  Read the MySQLdb documentation (or sqlite, or
 psycopg2) documentation for reasons why, and how to do it
 right.
 
 The only thing I found, which collaborates with something someone else
 taught me on this list about entering binary data, is that one must
 pass the parameters in the execute statement. Is that what you mean?
 If so, I find that for all purposes thus far other than binary data,
 the way I've been doing it seems to work just fine. I would prefer to
 keep doing it that way, because I find putting a print statement
 between the sql= line and the execute statement gives me a good
 opportunity to review the sql statement and catch errors. Is this not
 good practice?
 
 
 Thanks.
 beno

This is a horrendous practice.  You leave yourself vulnerable not only
to attacks, but to simple absent-mindedness as well.  Using parameters
in your execute statement will handle all necessary quoting for you,
which eliminates the possibility of a bad query sneaking in.  For more
information, as I mentioned, look up SQL injection.  Also, read this:
http://xkcd.com/327/

Cheers,
Cliff



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


Re: Help with cumulative sum

2009-09-08 Thread J. Cliff Dyer
If I gave you a list of numbers, could you come up with a summifier
function that returns another list of numbers that are a cumulative sum?
You've got the information in place to create a file

def summifier(nums):
Returns a list of numbers that are the running 
sum totals of nums

# ???

list_of_numbers = [1, 24, 34, 28, 4, 1]
cumulative_sum = summifier(list_of_numbers)
assert(cumulative_sum == [1, 25, 59, 87, 91, 92])

If you can come up with the summifier function, you're all set.  I gotta
say, though, this smells like homework.

Cheers,
Cliff


On Tue, 2009-09-08 at 12:29 -0700, Maggie wrote:
 Building on the code that I posted in one of the previous posts.. I
 need to find a cumulative sum of the file of the times in the test
 file:
 
 here is the code i have:
 
 #!/usr/bin/python
 
 import os.path
 
 #name of output file
 filename = OUTPUT.txt
 
 #open the file
 test = open (test.txt, rU)
 
 #read in all the data into a list
 readData = test.readlines()
 
 count = 0
 
 FILE = open(filename, w)
 
 for item in readData:
 
count = count + 1
tmp_string = str(count) + '' + item
print  FILE, tmp_string,
 
 else:
print 'The loop is finito'
 
 -
 
 my test file is this
 
 23
 241
 34234
 83
 123
 
 and I need to find a CUMULATIVE sum (or the running sum)...what would
 be the best way to go about that given the code i already have?
 
 thank you all!

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


Re: Numeric literals in other than base 10 - was Annoying octal notation

2009-08-23 Thread J. Cliff Dyer
I had an objection to using spaces in numeric literals last time around
and it still stands, and it still stands in the new one.

What happens if you use a literal like 0x10f 304?  Is 304 treated as
decimal or hexadecimal?  It's not clear how you would begin to combine
it   The way string concatenation works, it takes two independent string
literals, and combines them.  If you specify r'\n' 'abc\n', the first
half is treated independently as a raw string, and the second half is
treated as a normal string.  The result is '\\nabc\n'.

With numeric literals, this behavior doesn't even make sense.  How do
you concatenate hex 10f with decimal 304?  I suppose you could multiply
0x10f by 1000, and add them, but this probably wouldn't fit any
practical usecase.  

Alternatively, you could raise an exception, and require the user to use
numeric literals of the same type, like 0x10f 0x304, but then you lose
any readability benefit you might have gained by dropping the _ to begin
with.  

If, on the other hand, you want to combine the tokens before processing
their independent meanings, which makes the most intuitive sense, well,
in that case we're no longer talking about an operation analogous to
string contcatenation.  We're talking about integers no longer being
simple tokens that can be assigned a value.  I'm not familiar with the
code that makes all this happen in C Python (or any other implementation
for that matter), but it seems like it extends the complexity of the
parser unnecessarily. 

I'm concerned that the benefit in readability will be outweighed by the
burden it places on the parser, and the cognitive burden on the
programmer of knowing what to expect when using non-decimal numeric
literals.  For that reason, I'm a -1 on using a space in numeric
literals, but +1 on using some other separator, and an _, in spite of
its slight awkwardness in typing, seems like a good idea.

If someone with a solid understanding of the python parser could chime
in that this wouldn't cause as much friction as I think, and explain a
clean, elegant implementation for this, many of my concerns would be
alleviated, and I would change my -1 to a -0.

Cheers,
Cliff


On Mon, 2009-08-24 at 00:01 +1000, Ben Finney wrote:
 garabik-news-2005...@kassiopeia.juls.savba.sk writes:

  Why not just use the space? 123 000 looks better than 123_000, and is
  not syntactically ambiguous (at least in python). And as it already
  works for string literals, it could be applied to numbers, too…
 
 +1 to all this. I think this discussion was had many months ago, but
 can't recall how it ended back then.
 
 -- 
  \  “Only the educated are free.” —Epictetus, _Discourses_ |
   `\   |
 _o__)  |
 Ben Finney

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


Re: How to create functors?

2009-08-19 Thread J. Cliff Dyer
On Wed, 2009-08-19 at 15:56 +0200, Bruno Desthuilliers wrote:
 Terry Reedy a écrit :
  Robert Dailey wrote:
  
  I'm using Python 2.6. And using the legacy syntax in the lambda does
  not work either. I want to avoid using a def if possible. Thanks.
  
  In Python, writing
  
  name = lambda arg: expr
  
  instead of
  
  def name(arg): return expr
  
  is all negative and no positive and should be avoided.
 
 Except that def is a statement, and as such can't be used as a named 
 params when calling a function expecting a callback, ie:
 
 vroom = some_dead('parrot', name=lambda arg: exp)
 
 (notice the 'name = lambda arg: exp' ?-)
 

Which offers no added functionality over:

  def name_func(arg):
  return exp

  vroom = some_dead('parrot', name=name_func)

except for confusion in debugging. :)  (See other excessively long
threads on lambda for further discussion of the debugging headaches
caused by lambdas.


 Ok, nitpicking. Me ---[]
 
 g

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


Re: variable scoping question.

2009-08-10 Thread J. Cliff Dyer
On Mon, 2009-08-10 at 08:46 -0700, Cornelius Keller wrote:
 On 10 Aug., 17:12, Diez B. Roggisch de...@nospam.web.de wrote:
  Cornelius Keller wrote:
 [snip]
 
  http://effbot.org/zone/default-values.htm
 
  Diez
 
 Ok thank you.
 I' understand now why.
 I still think this is very confusing, because default values don't
 behave like most people would expect without reading the docs.
 
 - Cornelius

You are correct.  This is confusing at first blush.  The important thing
to remember is: *don't do that.*  Learn the pythonic workaround of using
None in your parameters whenever you want a default empty list, and
don't let it bother you too much.  Overall, python is a remarkably well
designed language.  This is one of the relatively rare warts that crept
in because it enables a broader cleanliness of design.

Cheers,
Cliff


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


Re: dictionary help

2009-08-10 Thread J. Cliff Dyer
On Mon, 2009-08-10 at 22:11 -0400, Krishna Pacifici wrote:
 Hi,
 kind of a newbie here, but I have two questions that are probably
 pretty simple.
 
 1.  I need to get rid of duplicate values that are associated with
 different keys in a dictionary.  For example I have the following
 code.
 s={}
 s[0]=[10,2,3]
 s[10]=[22,23,24]
 s[20]=[45,5]
 s[30]=[2,4]
 s[40]=[6,7,8]
 
 Now I want to be able to loop through the primary keys and get rid of
 duplicates (both in keys and values) so that I would have either a new
 dictionary or the same dictionary but with the following values:
 
 s[0]=[3]
 s[10]=[22,23,24]
 s[20]=[45,5]
 s[30]=[2,4]
 s[40]=[6,7,8]
 
 It doesn't matter which value gets removed as long as there is only
 one remaining, so in this example it doesn't matter that 2 got removed
 from s[0] or from s[30] as long as there is only one 2 in the
 dictionary.
 
So if the number is a key, you want to keep the key, and delete all
matching values, and if the number is not a key, you want to keep one
(any one) instance of that number?  I'm not sure that what you are
looking for is best represented by a dict.  You might want to consider
creating your own class and overriding __getitem__.


 2.  I need to be able to loop over the values in the dictionary when
 there are multiple values assigned to each key like above and assign
 new values to those values.  Taking the above example I would want to
 assign a new value so that when you called s[0] it would equal [3,4]
 say if 4 was the new value.  I think this should be as simple as
 adding a value, but I kept on having difficulty.
 
Here you might want to either use the append() method on the lists of
each entry.

 Any suggestions would be greatly appreciated.
 
I'm not sure what you are doing maps cleanly to currently existing
datastructures, which means that there might not be a quick shortcut for
you.  Hammer out the specs of what you want your class to be able to do,
and what the API will be for performing each of those functions.  Then
you should be able to begin implementing it, or at least come up with
some more specific questions.



 Thank you very much,
 Krishna

Cheers,
Cliff


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


Re: A Bug By Any Other Name ...

2009-08-03 Thread J. Cliff Dyer
On Sun, 2009-08-02 at 14:14 +, Albert van der Horst wrote:
 This is actually quite thoroughly untrue.  In python, *indentation*
 is
 significant.  Whitespace (internal to a line) is not.  You can even
 call
 methods like this if you want:
 
 You totally don't get it. You describe how python is now.
 I propose a change to be made to python. Small wonder that that is
 different from what it is now.
 
 
  s = 'abc'
  s. upper()
 ABC
 
 You prove nothing by giving examples.
 You can disprove by giving one counter example,
 here it goes.
 
 Whitespace (internal to a line) is significant.
 In Python you cannot change
   xleftgoing = 12300
 to
 x left going = 123 000 000
 
 (You can in Algol68)

I had a feeling that counterexample would be coming sooner or later.
However, it doesn't really address the change you're looking for.
Internal whitespace *is* irrelevant, except insofar as it can be used to
delimit different tokens in parsing.  If tokens are separate, they are
separate, and no more or less whitespace is going to make any
difference.  

Again, I'm describing how python is now.  Which is not to say it
couldn't be changed, I just want to make sure you understand how deep
into the heart of python you are trying to cut.  You make it sound like
a small change, but it is not.  You are proposing changing the parsing
rules, which completely changes the scope of what is possible and what
isn't with python syntax.  All to solve a problem that, so far, hasn't
been proven to exist in anything other than a speculative way.

You're trying to turn an ocean liner around because you left your
sunscreen on the dock.

Cheers,
Cliff

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


Re: Help understanding the decisions *behind* python?

2009-07-20 Thread J. Cliff Dyer
On Mon, 2009-07-20 at 12:26 -0700, Phillip B Oldham wrote:
 On Jul 20, 6:08 pm, Duncan Booth duncan.bo...@invalid.invalid wrote:
  The main reason why you need both lists and tuples is that because a tuple
  of immutable objects is itself immutable you can use it as a dictionary
  key.
 
 Really? That sounds interesting, although I can't think of any real-
 world cases where you'd use something like that.

Well, if you wanted to index a dictionary by coordinates, you might do
something like this:


fleet = {}
fleet[9,4] = 'destroyer'
fleet[8,4] = 'destroyer'
fleet[3,5] = 'aircraftcarrier'
fleet[4,5] = 'aircraftcarrier'
fleet[5,5] = 'aircraftcarrier'
fleet[6,5] = 'aircraftcarrier'
fleet[8,0] = 'battleship'
fleet[8,1] = 'battleship'
fleet[8,2] = 'battleship'


def checkattack(x, y, fleet):
if x,y in fleet:
return You hit my %s' % fleet[x,y]

Maybe not the best implementation of Battleship, but you get the idea.



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


Re: A Bug By Any Other Name ...

2009-07-17 Thread J. Cliff Dyer
On Fri, 2009-07-17 at 20:53 +, Albert van der Horst wrote:
 Because unlike in algol 68 in python whitespace is relevant,
 we could get by with requiring whitespace:
 x= -q   # okay
 ab and -ac and -b  -d# okay
 8 ** -2 # okay

This is actually quite thoroughly untrue.  In python, *indentation* is
significant.  Whitespace (internal to a line) is not.  You can even call
methods like this if you want:

 s = 'abc'
 s. upper()
ABC

Obviously, that's A Bad Idea(tm), but python's parser won't stop you.
The ++ operator gotcha is so minor that I can't remember anyone actually
asking about it on the list (who was actually facing it as a
problem--this thread was started by idle speculation).  Can we not
change the language syntax to address non-issues?

Practicality beats purity, a.k.a. don't you have something better to do?


Cheers,
Cliff



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


Re: mail

2009-07-15 Thread J. Cliff Dyer
On Thu, 2009-07-16 at 00:16 +0530, amr...@iisermohali.ac.in wrote:
 Dear all,
 
 Sorry that I am disturbing you all again and again but this is the way I
 am trying to solve my problem:---
 
  import re
  exp = re.compile(CA)
  infile = open(file1.txt)
  for line in infile:
 ... values = re.split(\s+, line)
 ... if exp.search(line):
 ...print (%s %s CA = %s %(values[2], values[3], values[6]))
 ...
  with this it is giving the output like:
 
 8 ALA CA = 54.67
 15 ALA CA = 52.18
 21 ALA CA = 54.33
 23 ALA CA = 55.84
 33 ALA CA = 55.58
 38 ALA CA = 54.33
 
 which is all right but i want CB and C value also in each row and it
 should take value from 5th column infront of them, file is something
 lookin like:-
 
  47 8   ALA   H H  7.85 0.02 1
  48 8   ALA   HAH  2.98 0.02 1
  49 8   ALA   HBH  1.05 0.02 1
  50 8   ALA   C C179.39  0.3 1
  51 8   ALA   CAC 54.67  0.3 1
  52 8   ALA   CBC 18.85  0.3 1
  53 8   ALA   N N123.95  0.3 1
 10715   ALA   H H  8.05 0.02 1
 10815   ALA   HAH  4.52 0.02 1
 10915   ALA   HBH  1.29 0.02 1
 11015   ALA   C C177.18  0.3 1
 11115   ALA   CAC 52.18  0.3 1
 11215   ALA   CBC 20.64  0.3 1
 11315   ALA   N N119.31  0.3 1
 15421   ALA   H H  7.66 0.02 1
 15521   ALA   HAH  4.05 0.02 1
 15621   ALA   HBH  1.39 0.02 1
 15721   ALA   C C179.35  0.3 1
 15821   ALA   CAC 54.33  0.3 1
 15921   ALA   CBC 17.87  0.3 1
 16021   ALA   N N123.58  0.3 1
 16923   ALA   H H  8.78 0.02 1
 17023   ALA   HAH  4.14 0.02 1
 17123   ALA   HBH  1.62 0.02 1
 17223   ALA   C C179.93  0.3 1
 17323   ALA   CAC 55.84  0.3 1
 17423   ALA   CBC 17.55  0.3 1
 17523   ALA   N N120.16  0.3 1
 23233   ALA   H H  7.57 0.02 1
 23333   ALA   HAH  3.89 0.02 1
 23433   ALA   HBH  1.78 0.02 1
 23533   ALA   C C179.24  0.3 1
 23633   ALA   CAC 55.58  0.3 1
 23733   ALA   CBC 19.75  0.3 1
 23833   ALA   N N121.52  0.3 1
 26938   ALA   H H  8.29 0.02 1
 27038   ALA   HAH  4.04 0.02 1
 27138   ALA   HBH  1.35 0.02 1
 27238   ALA   C C178.95  0.3 1
 27338   ALA   CAC 54.33  0.3 1
 27438   ALA   CBC 18.30  0.3 1
 27538   ALA   N N120.62  0.3 1
 
 I just want that it will give output something like:-
 
 8  ALA  C = 179.39  CA = 54.67  CB = 18.85
 15 ALA  C = 177.18  CA = 52.18  CB = 20.64
 21 ALA  C = 179.35  CA = 54.33  CB = 17.87.
 
 so first it will write the position of the amino acid(given by second
 column)then amino acid here it is ALA and then the corresponding value of
 C, CA and CB from 5th colum for each position of ALA.
 

Your program is structured wrong for doing what you want.  Right now you
are essentially saying, for each line in the input file, print
something, but each line of the input file only has part of the
information you want.  Instead, you should create a data structure that
gathers the pieces you want, and only print once you have all of those
pieces in place.

Step one, create your data structure, so the data is grouped the way you
want it, not by line:

 thingies = {} # create a dictionary
 for line in infile:
 data = line.split()

 if data[1] not in thingies:
 # group data by data[1]
 thingies[data[1]] = {}
 
 thingies[data[1]][data[3]] = data[5]

Step two, extract the data from the list:

 for key, data in thingies.items(): 
 print key,
 for entry in data
 print '%s = %s' % (entry, data[entry]),

This should do what you want, minus some formatting issues.

Cheers,
Cliff


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


Re: Clarity vs. code reuse/generality

2009-07-10 Thread J. Cliff Dyer
On Fri, 2009-07-10 at 02:57 +, Steven D'Aprano wrote:
 On Fri, 10 Jul 2009 03:28:04 +0100, Nobody wrote:
 
  On Thu, 09 Jul 2009 04:57:15 -0300, Gabriel Genellina wrote:
  
  Nobody says you shouldn't check your data. Only that assert is not
  the right way to do that.
  
  assert is not the right way to check your *inputs*. It's a perfectly
  reasonable way to check data which should be valid, as well as a way
  to document what variables are supposed to contain.
 
 Where are those variables coming from?
 
 The distinction really boils down to this:
 
 * asserts should never fail. If there is any chance that an assertion 
 might fail outside of test suites, then don't use assert.
 

I'm no expert, but the more I read this thread, and the more I think on
it, the more I believe that asserts don't really need to exist outside
of test suites.  The function that assertions provide is handled in a
far more robust and maintainable way by unit tests and doctests.
Anything else should be handled by more descriptive exceptions.  

The use of assertions in regular code may just be a historical baby step
on the way to real code testing.

To play devils advocate for a moment, one possible use case for assert
statements is if you need to test something that you can't easily get
under a proper unittest or doctest.  Maybe you need to know the value of
a variable halfway through a method.  A judicious assertion can help
supplement your test suite  A better solution would be to refactor so
you can get the needed value under test, but if time constraints won't
allow it, then make your assertion and move on, but only to help you
debug the code, not to protect your code at runtime.  Even then, why not
use a proper Exception (unless speed is a major issue)?

Even if it is only used internally in a module, I would still prefer a
ValueError to an AssertionError.  It tells you what's happening more
clearly.  And it protects you if another caller (even one internal to
the class) calls it with a different set of assumptions.

At minimum, I think there's a heavy burden on an author to justify the
use of AssertionErrors rather than other kinds of Exceptions. 

Cheers,
Cliff

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


Re: Clarity vs. code reuse/generality

2009-07-10 Thread J. Cliff Dyer
On Fri, 2009-07-10 at 11:57 -0500, Robert Kern wrote:
 On 2009-07-10 11:50, J. Cliff Dyer wrote:
  On Fri, 2009-07-10 at 02:57 +, Steven D'Aprano wrote:
  On Fri, 10 Jul 2009 03:28:04 +0100, Nobody wrote:
 
  On Thu, 09 Jul 2009 04:57:15 -0300, Gabriel Genellina wrote:
 
  Nobody says you shouldn't check your data. Only that assert is not
  the right way to do that.
  assert is not the right way to check your *inputs*. It's a perfectly
  reasonable way to check data which should be valid, as well as a way
  to document what variables are supposed to contain.
  Where are those variables coming from?
 
  The distinction really boils down to this:
 
  * asserts should never fail. If there is any chance that an assertion
  might fail outside of test suites, then don't use assert.
 
 
  I'm no expert, but the more I read this thread, and the more I think on
  it, the more I believe that asserts don't really need to exist outside
  of test suites.
 
 Actually, there is a good argument that one shouldn't use an assert statement 
 in 
 test suites: code can have bugs that only show up under -O so you want to be 
 able to run your test suite under -O.
 

That's an interesting point.  Presumably TestCase.assert_() doesn't
suffer from this defect?  Otherwise the entire unittest suite is
essentially broken by your argument.  I suppose I should have said one
should only raise AssertionErrors in test suites.  Practically speaking,
that's what a test suite is:  The place where you assert what the code
does.



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


Re: gett error message: TypeError: 'int' object is not callable

2009-07-10 Thread J. Cliff Dyer
On Thu, 2009-07-09 at 13:53 +, Friðrik Már Jónsson wrote:
 Look at:
 
len = len(text)
 
 You're overriding `len` (a built-in method), with an integer  
 (`len(text)`).  You then call:
 
for i in range(len(fields)):
 
 But `len` is no longer a callable, but merely an integer.
 
 Regards,
 Friðrik Már
 
 P.S. While this is a fairly obvious problem it's usually a good idea  
 to post working code and a traceback when requesting help.

While we're on the subject of good question posting form:  The body of
your post should contain all relevant information.  Please don't make
readers look back at the subject heading for the statement of the
problem.  Duplicating the statement of the problem in the subject and
the body is ok, but it really ought to be in the body.

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


Re: Nested Classes and Instances

2009-07-10 Thread J. Cliff Dyer
On Fri, 2009-07-10 at 19:00 +0200, Manuel Graune wrote:
 Hello,
 
 as an example of what I would like to achieve, think of a street
 where each house has a door and a sign with a unique (per house)
 number on it. I tried to model this like this:
 
 class House(object):
 class Door(object):
 def __init__(self,color):
  self.color=color
 class Sign(object):
 def __init__(self,text):
  self.text=text
 def __init__(self, doorcolor,housenumber):
 self.housenumber=housenumber
 self.door=House.Door(doorcolor)
 self.sign=House.Sign(housenumber)
 
 house1=House(red,1)
 house2=House(blue,2)
 

Don't do it like that.  Keep your classes independent.  Many houses can
have doors, and there's no reason to redefine the concept of Doors for
each one.  Same goes for Signs.  

class House(object):
def __init__(self, doorcolor, housenumber):
self.door = Door(doorcolor)
self.sign = Sign(housenumber)
self.number = housenumber

class Door(object):
def __init__(self, color):
self.color = color

class Sign(object):
def __init__(self, inscription):
self.inscription = str(housenumber)



(Something like that)

Cheers,
Cliff




 Well, so far, so good. Now, what I'd like to achive is that the text of
 the sign changes whenever the variable housenumber of the
 parent-instance changes or  that
 house1.sign.text is a reference/pointer to house1.housenumber
 
 Thanks in advance,
 
 Manuel
 
 
 
 -- 
 A hundred men did the rational thing. The sum of those rational choices was
 called panic. Neal Stephenson -- System of the world
 http://www.graune.org/GnuPG_pubkey.asc
 Key fingerprint = 1E44 9CBD DEE4 9E07 5E0A  5828 5476 7E92 2DB4 3C99

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


Re: count

2009-07-09 Thread J. Cliff Dyer
Bearophile wins!  (This only times the loop itself.  It doesn't check
for __len__)

summer:5
0:00:00.51
bearophile:5
0:00:00.09
summer:50
0:00:00.30
bearophile:50
0:00:00.13
summer:500
0:00:00.77
bearophile:500
0:00:00.53
summer:5000
0:00:00.000575
bearophile:5000
0:00:00.000473
summer:5
0:00:00.005583
bearophile:5
0:00:00.004625
summer:50
0:00:00.055834
bearophile:50
0:00:00.046137
summer:500
0:00:00.426734
bearophile:500
0:00:00.349573
summer:5000
0:00:04.180920
bearophile:5000
0:00:03.652311
summer:5
0:00:42.647885
bearophile: 5
0:00:35.190550

On Thu, 2009-07-09 at 04:04 -0700, Bearophile wrote:
 Paul Rubin:
  print x, sum(1 for _ in g)
 
 Don't use that, use my function :-) If g has a __len__ you are wasting
 time. And sum(1 ...) is (on my PC) slower.
 
 
 J. Clifford Dyer:
  if __name__ == '__main__':
  test_func(summer, 1000)
  test_func(tupler, 1000)
  test_func(summer, 1)
  test_func(tupler, 1)
 
 Have you forgotten my function?
 
 Bye,
 bearophile

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


Re: tough-to-explain Python

2009-07-09 Thread J. Cliff Dyer
On Thu, 2009-07-09 at 18:10 +, Steven D'Aprano wrote:
 If programming is symbol manipulation, then you should remember that
 the 
 user interface is also symbol manipulation, and it is a MUCH harder 
 problem than databases, sorting, searching, and all the other
 problems 
 you learn about in academia. The user interface has to communicate
 over a 
 rich but noisy channel using multiple under-specified protocols to a 
 couple of pounds of meat which processes information using buggy 
 heuristics evolved over millions of years to find the ripe fruit,
 avoid 
 being eaten, and have sex. If you think getting XML was hard, that's 
 *nothing* compared to user interfaces.

+1 QOTW!  QOTM, even!




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


Re: Idioms and Anti-Idioms Question

2009-07-02 Thread J. Cliff Dyer
On Wed, 2009-07-01 at 17:19 +1200, Lawrence D'Oliveiro wrote:
 In message mailman.2018.1245772229.8015.python-l...@python.org, J. Cliff 
 Dyer wrote:
 
  If the lines got separated, a leading + could disappear into its line
  without any errors showing up.  A trailing + would raise a syntax error.
 
 Unless, of course, it was moved onto the previous line as part of whatever 
 caused the separation of the lines. How would you guard against that?
 

Can you give an example of what you mean?

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


Re: It's ...

2009-07-01 Thread J. Cliff Dyer
On Tue, 2009-06-30 at 13:24 -0700, Beni Cherniavsky wrote:
 On Jun 24, 11:40 pm, J. Cliff Dyer j...@sdf.lonestar.org wrote:
  Also note that you can iterate over a file several times:
 
  f = open('foo.txt')
  for line in f:
  print line[0]  # prints the first character of every line
  for line in f:
  print line[1]  #prints the second character of every line
 
 No, you can't.  The second loop prints nothing!
 A file by default advances forward.  Once you reach the end, you stay
 there.

You are, of course, absolutely right.  Sorry for the misinformation.

:(

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


Re: Get name of class without instance

2009-06-24 Thread J. Cliff Dyer
On Wed, 2009-06-24 at 09:17 -0700, Bryan wrote:
 Given a class:
 
 class Foo(object):
 pass
 
 How can I get the name Foo without having an instance of the class?
 
 str(Foo) gives me more than just the name Foo.   __main__.Account
 Foo.__class__.__name__ gives me type
 
 I don't want to do:
 Foo().__class__.__name__ if possible.  I would rather avoid the
 constructor.  I just want to get a string Foo
 

I'll give you a hint:

 Foo().__class__ is Foo
True


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


Re: It's ...

2009-06-24 Thread J. Cliff Dyer
On Wed, 2009-06-24 at 20:53 +0100, Angus Rodgers wrote:
 ... my first Python program!  So please be gentle (no fifty ton
 weights on the head!), but tell me if it's properly Pythonic,
 or if it's a dead parrot (and if the latter, how to revive it).
 

Yay.  Welcome to Python.


 I'm working from Beazley's /Python: Essential Reference/ (2nd
 ed. 2001), so my first newbie question is how best to find out
 what's changed from version 2.1 to version 2.5. (I've recently
 installed 2.5.4 on my creaky old Win98SE system.) I expect to 
 be buying the 4th edition when it comes out, which will be soon,
 but before then, is there a quick online way to find this out?
 

Check here: http://docs.python.org/whatsnew/index.html

It's not designed to be newbie friendly, but it's in there.

 Having only got up to page 84 - where we can actually start to
 read stuff from the hard disk - I'm emboldened to try to learn
 to do something useful, such as removing all those annoying hard
 tab characters from my many old text files (before I cottoned on
 to using soft tabs in my text editor).
 
 This sort of thing seems to work, in the interpreter (for an 
 ASCII text file, named 'h071.txt', in the current directory):
 
 stop = 3   # Tab stops every 3 characters
 from types import StringType   # Is this awkwardness necessary?

Not anymore.  You can just use str for this.

 detab = lambda s : StringType.expandtabs(s, stop)  # Or use def

First, use def.  lambda is a rarity for use when you'd rather not assign
your function to a variable.  

Second, expandtabs is a method on string objects.  s is a string object,
so you can just use s.expandtabs(stop)

Third, I'd recommend passing your tabstops into detab with a default
argument, rather than defining it irrevocably in a global variable
(which is brittle and ugly)

def detab(s, stop=3):
#do stuff

Then you can do

three_space_version = detab(s)
eight_space_version = detab(s, 8)

 f = open('h071.txt')   # Do some stuff to f, perhaps, and then:
 f.seek(0)

f is not opened for writing, so if you do stuff to the contents of f,
you'll have to put the new version in a different variable, so f.seek(0)
doesn't help.  If you don't do stuff to it, then you're at the beginning
of the file anyway, so either way, you shouldn't need to f.seek(0).

 print ''.join(map(detab, f.xreadlines()))

Sometime in the history of python, files became iterable, which means
you can do the following:

for line in f:
print detab(line)

Much prettier than running through join/map shenanigans.  This is also
the place to modify the output before passing it to detab:

for line in f:
# do stuff to line
print detab(line)

Also note that you can iterate over a file several times:

f = open('foo.txt')
for line in f:
print line[0]  # prints the first character of every line
for line in f:
print line[1]  #prints the second character of every line
 f.close()
 


 Obviously, to turn this into a generally useful program, I need
 to learn to write to a new file, and how to parcel up the Python
 code, and write a script to apply the detab function to all the
 files found by searching a Windows directory, and replace the old
 files with the new ones; but, for the guts of the program, is this
 a reasonable way to write the code to strip tabs from a text file?
 
 For writing the output file, this seems to work in the interpreter:
 
 g = open('temp.txt', 'w')
 g.writelines(map(detab, f.xreadlines()))
 g.close()
 

Doesn't help, as map returns a list.  You can use itertools.imap, or you
can use a for loop, as above.

 In practice, does this avoid creating the whole string in memory
 at one time, as is done by using ''.join()? (I'll have to read up
 on opaque sequence objects, which have only been mentioned once
 or twice in passing - another instance perhaps being an xrange()?)
 Not that that matters much in practice (in this simple case), but
 it seems elegant to avoid creating the whole output file at once.

The terms to look for, rather than opaque sequence objects are
iterators and generators.

 
 OK, I'm just getting my feet wet, and I'll try not to ask too many
 silly questions!
 
 First impressions are: (1) Python seems both elegant and practical;
 and (2) Beazley seems a pleasantly unfussy introduction for someone 
 with at least a little programming experience in other languages.
 

Glad you're enjoying Beazley.  I would look for something more
up-to-date.  Python's come a long way since 2.1.  I'd hate for you to
miss out on all the iterators, booleans, codecs, subprocess, yield,
unified int/longs, decorators, decimals, sets, context managers and
new-style classes that have come since then.


 -- 
 Angus Rodgers

Cheers,
Cliff


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


Re: Procedures

2009-06-23 Thread J. Cliff Dyer
Please keep the discussion on-list. (Reply-all, rather than just
replying to me.) 

On Mon, 2009-06-22 at 15:36 -0700, Greg Reyna wrote:
 It's not the error that concerned me.  The fact that there is an 
 error of this type makes clear that there's something wrong with the 
 way the scripts are structured.  I was trying to communicate that I 
 recognized this fact.  Clearly, I was not successful.  Thought I'd 
 try to save bandwidth, too.
 ...
 I had tried running this previously with only one Class header: 
 LineReader, the others were just defs.  I changed the short defs 
 into sub-classes out of desperation, since I don't understand why the 
 main script is not recognizing functions that are in the same file.
 
 First is the shell input/output, then ParseWork.py, the entire text 
 file that contains the scripts.
 
 Thanks for your interest, Cliff,
 Greg
 ---
   import ParseWork
   avar = ParseWork.LineReader()
   xstring = 'scene 1, pnl 1, 3+8, pnl 2, 1+12, pnl 3, 12, pnl 4, 2+4,'
   avar.parseLine(xstring)
 Traceback (most recent call last):
File stdin, line 1, in module
File ParseWork.py, line 13, in parseLine
  xreturn = advanceSearch(xstring)  #shorten the part of string to 
 be searched
 NameError: global name 'advanceSearch' is not defined
 
 ---
 class LineReader:   
  def parseLine(self, xstring):
  global sc_info,scnum,pnlnum,prev_pos,cur_pos
  sc_info = { 'sc':{0:0}}  #dict to store scene num; pnl num(s), ftge
  prev_pos = 0
  cur_pos = xstring.find(',') #defaults to length of string
  while xstring.find(',',(prev_pos+1))  !=  -1: 
  temp = xstring[prev_pos:cur_pos]   #pull out the part btwn commas
  section = temp.strip()   
  if section[0:1] == 's':
  scnum = int(section[5:])  #get the number(s) off the 
 end of scene block
  sc_info['sc',scnum] = scnum  #store scnum-which is 
 both key and value
  xreturn = advanceSearch(xstring)  #shorten the part 
 of string to be searched
  continue
  if section[0:1] == 'p':
  pnlnum = int(section[3:])
  sc_info['sc',scnum,pnlnum] = pnlnum  #store pnlnum  
 temp value for pnlnum
  xreturn = advanceSearch(xstring)  #the return value 
 is to move flow back here
  continue
  if section[0:1] != 's' or 'p':
  xnum = section[0:]   #section must contain the footage
  if section.find('+'):   #the + exists
  ftge = parseFtge(section) 
  sc_info['sc',scnum,pnlnum] = ftge  #store ftge in pnlnum
  xreturn = advanceSearch(xstring)
  continue
  else:
  ftge = (section/16.0)  #section is frames-convert 
 to decimal
  sc_info['sc',scnum,pnlnum] = ftge  #store ftge in pnlnum
  xreturn = advanceSearch(xstring)
  continue
  else:
  print sc_info
 
 class ContRead(LineReader):   
  def advanceSearch(xstring):
  prev_pos = (cur_pos +1) 
  cur_pos = xstring.find(',',prev_pos)  #find the next comma  
  return
 
 class Footage(LineReader):   
  def parseFtge(section):
  xplus = section.find('+')  #find position of '+'
  xfeet = int(section[0:xplus])
  xframes = int(section[(xplus + 1):-1])  
  xframes_d = (xframes/16.0)
  return (xfeet + xframes_d)
 
 


From what I can see you have no need for classes in your code.  This is
probably what is causing you trouble at the moment, so I would say just
remove them.  

def parse_line(line):
splits a line on commas
return line.split(',')

def parse_footer(line):
Strips off leading four characters (maybe 'Ftr:'), and then
parses the rest as above
return parse_line([4:])

That should solve your problem.  Below I illustrate some of how classes
work, in case you are dead set on using them.  Learning Python should
address all of this as well.  I haven't read it, so I can't be more
specific.

To call a method from another method within a class, you need to prefix
it with self.  That looks like this:

class Spam(object):  ## {1}
def parse_line(self, line):  ## {2}
return line.split(',')

def parse_footer(self, line):
return self.parse_line(line[4:])  ## {3}

# {1} always subclass 'object' to use newstyle classes
# {2} Note that method declarations have an extra first argument, 
# usually called self, which refers to your instance of the class.
# {3} self finds parse_line within the current class
# or superclasses of the current class.  Note that self has 
# moved from inside the parentheses to before the method name.


To call a method from another class you need to instantiate that class
first.

class Spam(object):

Re: Idioms and Anti-Idioms Question

2009-06-23 Thread J. Cliff Dyer
On Mon, 2009-06-22 at 22:52 +, Peter Billam wrote:

 I wonder on what grounds PEP8
 says The preferred place to break around a binary operator is
 *after* the operator ?
 Perhaps it's just the continutation marker rationale?
 
 Regards,  Peter
 
 -- 
 Peter Billam   www.pjb.com.auwww.pjb.com.au/comp/contact.html


If the lines got separated, a leading + could disappear into its line
without any errors showing up.  A trailing + would raise a syntax error.

 spam = 6
 spam +
  File stdin, line 1
spam +
 ^
SyntaxError: invalid syntax
 + spam
6
 



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


Re: Perl's @foo[3,7,1,-1] ?

2009-06-22 Thread J. Cliff Dyer
On Mon, 2009-06-22 at 14:57 +0200, Jean-Michel Pichavant wrote:
 J. Cliff Dyer wrote:
  On Wed, 2009-06-17 at 14:13 +0200, Jean-Michel Pichavant wrote:

  On Wed, Jun 17, 2009 at 04:14, Steven D'Aprano wrote:
  
  What's np.arange?
  
  
  import numpy as np
 
  --
  Pierre delroth Bourdon delr...@gmail.com
  Étudiant à l'EPITA / Student at EPITA


  Perfect example of why renaming namespaces should be done only when 
  absolutely required, that is, almost never.
 
  Jean-Michel
  
 
  I disagree.  Renaming namespaces should always be done if it will help
  stop people from doing a 'from package import *'.  However, example code
  should always include relevant imports.
 
  Cheers,
  Cliff
 
 

 The import * should not used if possible, I totally agree on that point, 
 but there's no need to rename namespaces for that.
 
 br
 
 Jean-Michel
 

Technically, no.  But we're dealing with people, who are notoriously
*un*technical in their behavior.  A person is much more likely to
develop bad habits if the alternative means more work for them.  The
reason people do `from foo import *` is that they don't want to type
more than they have to.  If they can write a one or two letter
namespace, they're likely to be happy with that trade-off.  If the
alternative is to write out long module names every time you use a
variable, they'll tend to develop bad habits.  To paraphrase Peter
Maurin, coding guidelines should have the aim of helping to bring about
a world in which it is easy to be good.

I don't really see much problem with renaming namespaces: For people
reading the code, everything is explicit, as you can just look at the
top of the module to find out what module a namespace variable
represent; the local namespace doesn't get polluted with God knows what
from God knows where; and code remains succinct.

I've found in my own code that using, for example, the name `sqlalchemy`
in my code means that I have to go through painful contortions to get
your code down to the PEP-8 recommended 80 characters per line.  The
resulting mess of multi-line statements is significantly less readable
than the same code using the abbreviation `sa`.

Do you have an argument for avoiding renaming namespaces?  So far the
only example you provided is a code fragment that doesn't run.  I don't
disagree with you on that example; referring to numpy as np without
telling anyone what np refers to is a bad idea, but no functioning piece
of code could reasonably do that.

Cheers,
Cliff

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


Re: How to output a complex List object to a file.

2009-06-22 Thread J. Cliff Dyer
Have you looked at the JSON module?

On Mon, 2009-06-22 at 21:17 +0800, Jim Qiu wrote:
 Hi all,
 
 I have a object list list this:
 
 from bots.botsconfig import *
 from D96Arecords import recorddefs
 from edifactsyntax3 import syntax
 
 structure=[
 {ID:'UNH',MIN:1,MAX:1,LEVEL:[
 {ID:'BGM',MIN:1,MAX:1},
 {ID:'DTM',MIN:1,MAX:5},
 {ID:'NAD',MIN:1,MAX:5,LEVEL:[
 {ID:'CTA',MIN:0,MAX:5,LEVEL:[
 {ID:'COM',MIN:0,MAX:5},
 ]},
 ]},
 {ID:'RFF',MIN:0,MAX:5,LEVEL:[
 {ID:'DTM',MIN:0,MAX:5},
 ]},
 {ID:'CUX',MIN:0,MAX:5,LEVEL:[
 {ID:'DTM',MIN:0,MAX:5},
 ]},
 {ID:'LOC',MIN:1,MAX:20,LEVEL:[
 {ID:'DTM',MIN:0,MAX:5},
 {ID:'LIN',MIN:0,MAX:20,LEVEL:[
 {ID:'PIA',MIN:0,MAX:5},
 {ID:'IMD',MIN:0,MAX:5},
 {ID:'RFF',MIN:0,MAX:5},
 {ID:'ALI',MIN:0,MAX:5},
 {ID:'MOA',MIN:0,MAX:5},
 {ID:'PRI',MIN:0,MAX:5},
 {ID:'QTY',MIN:0,MAX:999,LEVEL:[
 {ID:'NAD',MIN:0,MAX:1},
 ]},
 ]},
 ]},
 {ID:'UNT',MIN:1,MAX:1},
 ]
 }
 ]
 
 I need to output this structure object into a file, how to do that ?
 
 Jim
 

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


Re: Procedures

2009-06-22 Thread J. Cliff Dyer
On Mon, 2009-06-22 at 12:13 -0700, Greg Reyna wrote:
 Learning Python (on a Mac), with the massive help of Mark Lutz's 
 excellent book, Learning Python.
 
 What I want to do is this:
 I've got a Class Object that begins with a def.  It's designed to be 
 fed a string that looks like this:
 
 scene 1, pnl 1, 3+8, pnl 2, 1+12, pnl 3, 12, pnl 4, 2+4,
 
 I'm parsing the string by finding the commas, and pulling out the 
 data between them.
 No problem so far (I think...)  The trouble is, there is a place 
 where code is repeated:
 
 1. Resetting the start  end position and finding the next comma in the 
 string.
 

Have you looked at the split() method on string objects.  It works kind
of like this:

 s = scene 1, pnl 1, 3+8, pnl 2, 1+12, pnl 3, 12, pnl 4, 2+4,
 s.split(,)
['scene 1', ' pnl 1', ' 3+8', ' pnl 2', ' 1+12', ' pnl 3', ' 12', ' pnl
4', ' 2+4', '']
 elements = s.split(,)
 elements
['scene 1', ' pnl 1', ' 3+8', ' pnl 2', ' 1+12', ' pnl 3', ' 12', ' pnl
4', ' 2+4', '']
 elements[2]
' 3+8'



 In my previous experience (with a non-OOP language), I could create a 
 'procedure', which was a separate function.  With a call like: 
 var=CallProcedure(arg1,arg2) the flow control would go to the 
 procedure, run, then Return back to the main function.
 

Python doesn't have procedures quite like this.  It has functions (the
things starting with def), which generally speaking take arguments and
return values.  For the most part, you do not want your functions to
operate on variables that aren't either defined in the function or
passed in as arguments.  That leads to difficult-to-debug code.  


 In Python, when I create a second def in the same file as the first 
 it receives a undefined error.  I can't figure out how to deal with 
 this.  How do I set it up to have my function #1 call my function #2, 
 and return?

Your problem description is confusing.  First of all, no class starts
with 'def'.  They all start with 'class'.  Perhaps you are talking about
a module (a python file)?  

Also, telling us that you get an undefined error is not particularly
helpful.  Every Exception that python raises is accompanied by an
extensive stack trace which will help you (or us) debug the problem.  If
you don't show us this information, we can't tell you what's going
wrong.  It will tell you (in ways that are crystal clear once you have a
bit of practice reading them) exactly what went wrong.

Can you show your code, as well as the complete error message you are
receiving?

My suggestions here, are essentially a paraphrasing of Eric Raymond's
essay, How to Ask Smart Questions.  It is freely available on the web,
and easily found via google.  I recommend reading that, in order to get
the most mileage out this news group.

Cheers,
Cliff


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


Re: Perl's @foo[3,7,1,-1] ?

2009-06-17 Thread J. Cliff Dyer
On Wed, 2009-06-17 at 14:13 +0200, Jean-Michel Pichavant wrote:
 On Wed, Jun 17, 2009 at 04:14, Steven D'Aprano wrote:
  What's np.arange?
  
 
  import numpy as np
 
  --
  Pierre delroth Bourdon delr...@gmail.com
  Étudiant à l'EPITA / Student at EPITA

 
 Perfect example of why renaming namespaces should be done only when 
 absolutely required, that is, almost never.
 
 Jean-Michel

I disagree.  Renaming namespaces should always be done if it will help
stop people from doing a 'from package import *'.  However, example code
should always include relevant imports.

Cheers,
Cliff


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


Re: Perl's @foo[3,7,1,-1] ?

2009-06-15 Thread J. Cliff Dyer
On Sun, 2009-06-14 at 23:01 +1000, Steven D'Aprano wrote:
 Write a helper function:
 
 def getitems(L, *indexes):
 if len(indexes) == 1:
 indexes = indexes[0]
 return [L[i] for i in indexes]
 

Whoops!  Your example is broken:

 cars = ['Ford', 'Toyota', 'Edsel']
 getitems(cars, 1)
Traceback (most recent call last):
  File stdin, line 1, in module
  File stdin, line 4, in getitems
TypeError: 'int' object is not iterable
 

I think you meant to apply that [0] to the created list instead.
Something like:

def getitems(L, *indexes):
new_list = [L[i] for i in indexes]
if len(indexes) == 1:
new_list = new_list[0]
return new_list

But I'm not sure that would be the best idea anyway.  Just let getitems
always return a list.  That way the caller doesn't have to test the
length to figure out what to do with it.  If you know you want a single
item, you can use regular old .__getitem__ (or .get) methods, or direct
indexing.  

Then getitems can just be:

def getitems(L, *indexes):
return [L[i] for i in indexes]

 
 But I think this is an obvious enough extension to the __getitem__ protocol
 that I for one would vote +1 on it being added to Python sequence objects
 (lists, tuples, strings).
 

I'd be +0.  It won't change my life, but it seems like a decent idea.

 
 -- 
 Steven
 

Cheers,
Cliff

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


Re: reseting an iterator

2009-05-22 Thread J. Cliff Dyer
On Wed, 2009-05-20 at 11:35 -0700, Jan wrote:
 Wouldn't it be easy for Python to implement generating functions so
 that the iterators they return are equipped with a __reset__() method?
 
 Here is the context of this question.
 
 Python documentation defines  a iterator as an object ITERATOR
 having methods __next__() and __iter__() such that the call
 ITERATOR.__iter__() returns the object itself, and once a call
 ITERATOR. __next__() raises StopIteration every such subsequent call
 does the same.

You don't need a reset method.  There is no hard and fast rule that
__iter__ must return the object itself.  It just needs to return an
iterator.  For example:

 l = [1,2,3]
 l.__iter__()
listiterator object at 0x7fd0da315850
 l is l.__iter__()
False

Just create a class with an __iter__ method that returns a reset
iterator object.


class X(object):
def __init__(self, max=3):
self.counter = 0
self.max = max
def __iter__(self):
return self
def next(self):
if self.counter  self.max:
self.counter += 1
return self.counter
else:
raise StopIteration

class Y(object):
def __iter__(self):
return X()

In this setup, X has the problem you are trying to avoid, but Y behaves
as a resettable iterable.

 x = X()
 for c in x:
... print c
... 
1
2
3
 for c in x:
... print c
... 
 y = Y()
 for c in y:
... print c
... 
1
2
3
 for c in y:
... if c  3:
... print c
... 
1
2
 for c in y:
... print c
... 
1
2
3

Cheers,
Cliff


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


Re: While Statement

2009-05-22 Thread J. Cliff Dyer
On Fri, 2009-05-22 at 09:59 -0400, Dave Angel wrote:
 
 Tim Wintle wrote:
  On Fri, 2009-05-22 at 13:19 +0200, Andre Engels wrote:

  number/total = 998/999 = 0
  number/total*100 = 0*100 = 0
  float(number/total*100) = float(0) = 0.0
 
  Change float(number/total*100) to float(number)/total*100 and it
  should work:
  
 
  I'd use:
 
   (number * 100.)/total
 
  - works because
   int * float = float 
 
  It's a minor thing, but it's much faster to cast implicitly as you miss
  the python function call overhead - it's no extra work to write, and for
  numerical things it can really speed things up.
 

  a = timeit.Timer(float(200)/5*100)
  b = timeit.Timer((200*100.)/5)
  a.timeit(1000)
  
  12.282480955123901

  b.timeit(1000)
  
  3.6434230804443359
 
  Tim W
 
 
 

 It's the old-timer in me, but I'd avoid the float entirely.  You start 
 with ints, and you want to end with ints.  So simply do the multiply 
 first, then the divide.
 
  number * 100/total
 
 will get the same answer.
 

Also, to get the same behavior (faster integer division) in python 3 or
when using `from __future__ import division`, use the // division
operator instead of /.

In fact, a little experimentation shows that you get the speedup even
when using python 2.6 with old-style division.  Probably because python
doesn't have to check the types of its arguments before deciding to do
integer division.

 a = timeit.Timer((200 * 100.)/5)
 b = timeit.Timer((200 * 100)/5)
 c = timeit.Timer((200 * 100)//5)
 d = timeit.Timer((200 * 100.0)//5)
 for each in a, b, c, d:
... each.timeit()
... 
2.3681092262268066
2.417525053024292
0.81031703948974609
0.81548619270324707


Cheers,
Cliff




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


Re: Question about locals()

2009-05-22 Thread J. Cliff Dyer
Top-posting corrected.

On Fri, 2009-05-22 at 10:00 -0500, Gökhan SEVER wrote:

 On Fri, May 22, 2009 at 9:43 AM, David Robinow drobi...@gmail.com
 wrote:
 On Fri, May 22, 2009 at 10:27 AM, Gökhan SEVER
 gokhanse...@gmail.com wrote:
 ...
  serialc = np.loadtxt(sys.argv[1], skiprows=skiprows).T
  for i in range(20):
  locals()['serialc_bin' + str(i+1)] = serialc[i+4]
 
  I don't know easier way than using locals() to construct
 variable-like
  identities in my program.
 
 
 I don't either.  I also don't know why you feel you need to
 construct
 variable-like identities.
 Why is:
  serialc_bin1
 better than
  serialc_bin[0]
 or, for that matter,
  serialc[4]
 
 ???
 
 --
 http://mail.python.org/mailman/listinfo/python-list
 

 Because in this case serialc is an numpy array. Since loadtxt returns
 a numpy-array. Furthermore 
 
 locals()['serialc_bin' + str(i+1)]  creates a dictionary key (that's
 what I use the term variable-like) serialc_bin1, serialc_bin2, ...
 not serialc_bin[0] with indexes. 
 
 
 Gökhan
 
 

I'm not sure why dictionary keys are better than indexes in your
example, especially as they're being created by coercing integers to
strings.  However, that's your business.  But why not just create your
own dictionary.  Then you can pass the results around easily as a group
or one at a time, and you don't have to mess in locals() (which isn't
supposed to be messed in).  It also gives you more flexibility as to how
you name your entries:

d = {}

You can use the same indexes you're using now:

d['serialc_bin' + str(i+1)] = serialc[i+4]

or you can keep the integerness of i by using tuples as keys (thus
preserving sort order across your keys):

d[('serialc_bin', i+1)] = serialc[i+4]

or you can use the integer as a key by itself (yes!  It looks like
you're accessing a list, but it's actually a dict with integer keys):

d[i+1] = serialc[i+4]

Or you can use a dict with a more descriptive name and the last above
solution gains the readability of your initial solution:

serialc_bin = {}
serialc_bin[i+1] = serialc[i+4]

Finally, if there's more you want to do with these numbers, you can
create a class to hold them.  Then as you need to, you can add methods
to it to implement the behavior you're looking for.

class MyCollection(object):
Untested code

def __init__(self):
self._store = {}

def __getitem__(self, item):
return self._store[item]

def __setitem__(self, item, value):
self._store[item] = value

def __delitem__(self, item):
del(self._store[item])

def sum(self):
total = 0
for value in self._store.values():
total += value
return value

serialc_bin = MyCollection()
for i, value in enumerate(serialc):
serialc_bin[i+1] = value
print serialc_bin.sum()

Any one of these seems like a better idea to me than trying to pollute
your local namespace with an unknown number of variables.  You have a
collection of objects.  It makes sense to store them in one of python's
collection types, or create an object of your own to store them.

Cheers,
Cliff


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


Re: reseting an iterator

2009-05-22 Thread J. Cliff Dyer
On Fri, 2009-05-22 at 10:54 -0700, Jan wrote: 
 On May 22, 9:46 am, J. Cliff Dyer j...@sdf.lonestar.org wrote:
 
  You don't need a reset method.  There is no hard and fast rule that
  __iter__ must return the object itself.  It just needs to return an
  iterator.  
 
 I disagree.
 If ITRATOR is a true iterator, ITRATOR.__iter__() must return
 ITERATOR.
 If ITERABLE is an iterable (but not necessarily an iterator)
 ITRABLE.__iter__() must return an iterator.
 

You are correct:  It is an iterable, not an iterator.  However, that's
not a disagreement with me.  It may not be an iterator (and I probably
should have said so) but it works, and it solves the OP's problem.
  For example:
 
   l = [1,2,3]
   l.__iter__()
 
  listiterator object at 0x7fd0da315850 l is l.__iter__()
 
  False
 
 [1,2,3] is an iterable but not an iterator, so this False result is
 expected.
 Compare this with the following.
 
  ii = iter([1,2,3])  # ii is an iterator.
  next(ii)
 1
  jj = ii.__iter__() # call __iter__ method on an iterator
  ii is jj
 True
  next(jj)
 2
 
  Just create a class with an __iter__ method that returns a reset
  iterator object.
 
  class X(object):
  def __init__(self, max=3):
  self.counter = 0
  self.max = max
  def __iter__(self):
  return self
  def next(self):
  if self.counter  self.max:
  self.counter += 1
  return self.counter
  else:
  raise StopIteration
 
  class Y(object):
  def __iter__(self):
  return X()
 
  In this setup, X has the problem you are trying to avoid, but Y behaves
  as a resettable iterable.
 
  y = Y()
 
 This does not work.
 
 With this, y is not an interator, and not even an iterable.
 
  for c in y:
 
 This produces an error because by definition of for-loops
 it is executed the same way as:
 
 temp_iterator = iter(y) # temp_iterator is y
 while True:
 try:
 print(next(temp_iterator)) # temp_iterator does not support
 __next__()
 except StopIteration:
 break
 

Did you try running my code?  I did.  It works on my computer.  What
error message did you get?

Cheers,
Cliff


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


Re: Sorting a dictionary

2009-05-15 Thread J. Cliff Dyer
On Fri, 2009-05-15 at 09:57 -0700, Tobiah wrote:
 On Tue, 12 May 2009 14:17:54 +0200, Jaime Fernandez del Rio wrote:
 
  This one I think I know... Try with:
  
  for k in sorted(word_count) :
  print k,=,word_count[k]
  
  You need to do the sorting before iterating over the keys...
 
 Isn't that what's happening here?  I read this as the
 'sorted' function iterating over the keys in word_count,
 then passing that sorted list to the 'for' loop, for a
 second iteration over the sorted pairs.
 
 No?
 
 Tobiah

The code and the description you are responding to are by the same
poster.  The OP posted something quite different.

@Ronn:

The for statement, which is the line ending with : has all the control
over what order elements get processed in.  Compare the following cases:

 for x in ['hat', 'socks', 'shirt', 'pants']:
... print sorted(x)
aht
ckoss
hirst
anpst

In this case, each element of the list gets sorted.  That is, the
letters of each string are put in alphabetical order.  But they are
processed in the order they appeared in the list.  If you want to
process the elements of the list in alphabetical order, you have to sort
the list itself:

 for x in sorted(['hat', 'socks', 'shirt', 'pants']):
... print x
hat
pants
shirt
socks

Cheers,
Cliff



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


Re: How to see the code definiton in the shell ?

2009-05-13 Thread J. Cliff Dyer
On Wed, 2009-05-13 at 09:40 -0700, Mohan Parthasarathy wrote:
 Hi,
 
 I am new to Python. I tried searching this but could not find an
 answer. In the interactive shell, I write a new function and I want to
 be able to see all the code that I wrote at a later time. Just typing
 the function name only shows 
 
  allmethods
 function allmethods at 0x822b0
 
 How do I see the actual code ?
 
 thanks
 mohan
 

The function definition itself has been compiled down to bytecode.  You
can see the bytecode as follows:

 def one():
... return 1
...
 print one.func_code.co_code  # not very readable
'd\x01\x00S'
 import dis # python disassembler
 dis.dis(one.func_code.co_code)
  0 LOAD_CONST  1 (1)
  3 RETURN_VALUE   

or just run dis.dis on the function itself

 def two():
... import math
... return (math.e ** (0+1J*math.pi)) + 3
... 
 two()
(2+1.2246063538223773e-16j)
 two.func_code.co_code
'd\x01\x00d\x00\x00k\x00\x00}\x00\x00|\x00\x00i\x01\x00d\x02\x00d\x03
\x00|\x00\x00i\x02\x00\x14\x17\x13d\x04\x00\x17S'
 dis.dis(two)
  2   0 LOAD_CONST   1 (-1)
  3 LOAD_CONST   0 (None)
  6 IMPORT_NAME  0 (math)
  9 STORE_FAST   0 (math)

  3  12 LOAD_FAST0 (math)
 15 LOAD_ATTR1 (e)
 18 LOAD_CONST   2 (0)
 21 LOAD_CONST   3 (1j)
 24 LOAD_FAST0 (math)
 27 LOAD_ATTR2 (pi)
 30 BINARY_MULTIPLY 
 31 BINARY_ADD  
 32 BINARY_POWER
 33 LOAD_CONST   4 (3)
 36 BINARY_ADD  
 37 RETURN_VALUE



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


Re: unicode bit me

2009-05-08 Thread J. Cliff Dyer
On Fri, 2009-05-08 at 07:53 -0700, anuraguni...@yahoo.com wrote:
 #how can I print a list of object which may return unicode
 representation?
 # -*- coding: utf-8 -*-
 
 class A(object):
 
 def __unicode__(self):
 return u©au
 
 __str__ = __repr__ = __unicode__
 

Your __str__ and __repr__ methods don't return strings.  You should
encode your unicode to the encoding you want before you try to print it.

class A(object):
def __unicode__(self):
return u©au

def get_utf8_repr(self):
return self.__unicode__().encode('utf-8')

def get_koi8_repr(self):
return self.__unicode__().encode('koi-8')

__str__ = __repr__ = self.get_utf8_repr

 a = A()
 
 try:
 print a # doesn't work?
 except UnicodeEncodeError,e:
 print e
 try:
 print unicode(a) # works, ok fine, great
 except UnicodeEncodeError,e:
 print e
 try:
 print unicode([a]) # what doesn't work?
 except UnicodeEncodeError,e:
 print e
 
 Now how can I print a list of object which may return unicode
 representation?
 loop/map is not an option as it goes much deepr in my real code
 any can anyoen explain what is happening here under the hood?
 
 --
 http://mail.python.org/mailman/listinfo/python-list
 

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


Re: php to python code converter

2009-05-08 Thread J. Cliff Dyer
On Fri, 2009-05-08 at 17:19 +0200, Pascal Chambon wrote:
 PS : Am I the only one having most of answers rejected by the
 antispam 
 system of python-list ? That's humiliating :p
 

I've had several messages not make it through.

:(


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


Re: for with decimal values?

2009-05-05 Thread J. Cliff Dyer
On Sun, 2009-05-03 at 19:48 -0700, alex23 wrote:
 On May 4, 11:41 am, Esmail ebo...@hotmail.com wrote:
  All this discussion makes me wonder if it would be a good idea
  for Python to have this feature (batteries included and all) - it
  would have its uses, no?
 
 Well, sometimes more discussion == less consensus :)
 
 But it's really easy to roll your own:
 
 from decimal import Decimal
 
 def args2dec(fn):
 '''*args to Decimal decorator'''
 float2dec = lambda f: Decimal(str(f))
 def _args2dec(*args):
 args = map(float2dec, args)
 return fn(*args)
 return _args2dec
 
 @args2dec
 def drange(start, stop, step):
 while start  stop:
 yield start
 start += step

I'd prefer not to force my users into using the Decimal type.  Maybe
float fits their needs better.  So here's a generalized xrange
replacement, which fixes an error in the drange implementation above
(try it with a negative step).  It's also highly *not* optimized.
Calculating i * step every time slows it down.  Speeding it back up is
an exercise for the reader.

def general_xrange(start, stop, step=1):
target = stop * step
if start * step  target:
raise ValueError
i = start
while i * step  target:
yield i
i += step

Cheers,
Cliff


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


Re: Re: list comprehension question

2009-05-05 Thread J. Cliff Dyer
On Fri, 2009-05-01 at 13:00 -0400, John Posner wrote:
 Shane Geiger wrote:
 if type(el) == list or type(el) is tuple:
 A tiny improvement:
 
 if type(el) in (list, tuple):
 

Another alternative, which might be useful in some cases:

  if hasattr(el, '__iter__'):

This covers all iterables, not just lists and tuples.  

So:

 flatten([1,2, xrange(3,15), 15, 16])
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]

The downside, of course, is that some iterables might be infinite (such
as count), which would cause hidden breakage.  But if you have one of
those in your data structure, why are you trying to flatten it anyway?


Cheers,
Cliff


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


Re: list comprehension question

2009-05-05 Thread J. Cliff Dyer
On Tue, 2009-05-05 at 12:15 -0400, J Kenneth King wrote:
 Emile van Sebille em...@fenx.com writes:
 
  On 5/1/2009 7:31 AM J Kenneth King said...
  Chris Rebert c...@rebertia.com writes:
  b = []
  for pair in a:
  for item in pair:
  b.append(item)
 
  This is much more clear than a nested comprehension.
 
  I love comprehensions, but abusing them can lead to really dense and
  difficult to read code.
 
  I disagree on dense and difficult, although I'll leave open the
  question of abuse.
 
 Dense and difficult may be subjective to people like you or I, but you
 left out the to read part of that sentence. I was referring to the
 negative effect nested or complex list comprehensions can have on
 readability.
 
  b = [ item for pair in a for item in pair ]
 
 It's a clever statement, but use it once and its gone. Why not use the
 expanded form in a function definition and get an even shorter, but
 clearer statement?
 

It's also not obvious what it means.  I would have thought the proper
incantation would be:

[item for item in pair for pair in a]

but that returns [5,5,5,6,6,6].  I still haven't figured out why.  The
way you have to bounce your eyes back and forth in the comprehension
makes it hard to read the logic.  With the loop, on the other hand it is
blatantly obvious which way the nesting occurs.




  b = flatten(a)
 
 Boom, done.
 
  This is exactly the code above expressed in comprehension form.
 
  It's worth knowing that a list comprehension is structured identically
  to the equivalent for loop.  So it really is neither more dense nor
  more difficult to read.  Further, you can tell immediately from the
  start of the list comprehension what you've got -- in this case a list
  of item(s).

Empirically, I'd have to disagree with you.  The structure is changed by
the fact that item gets put at the beginning of the comprehension.
With the loop, each variable gets introduced in a for statement, and
then used.  With the list comprehension, item gets used at the
beginning, and then assigned to in the for expression at the end.
Meanwhile, pair gets introduced as the loop variable in the first for
expression, and iterated over in the second for expression.  So do you
read it left to right or right to left?  Neither.  You have to hold the
whole expression in your mind at once.  Good for short and simple
comprehensions, bad as they get more complicated.

 
 
  Here with some slight changes...
 
  a = [(1, 2), (3, 4, 7), (5, 6)]
  [ item for j in a if len(j)==2 for item in j if item % 2 ]
  [1, 5]
 

This, to me, looks like line noise, approaching perl in its
unreadability.


  ...opposed to...
 
  for j in a:
  ... if len(j)==2:
  ... for item in j:
  ... if item % 2:
  ... b.append(item)
  ...
  b
  [1, 5]
 
 

Much nicer.  Thank you.

 Thanks for the lesson in list comprehensions, but I'm well aware of
 how they work.
 
 List comprehensions can make a reader of your code apprehensive
 because it can read like a run-on sentence and thus be difficult to
 parse. The Python documentation discourages their use and I believe
 for good reason. It's much easier to explain complex processes with a
 function rather than a single nested statement.
 
 
  YMMV,
 
  Emile
 
 It will apparently vary greatly. Depending on how much coffee I've
 had.
 
 ;)
 
 J
 --
 http://mail.python.org/mailman/listinfo/python-list
 

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


Re: dict is really slow for big truck

2009-04-29 Thread J. Cliff Dyer
On Wed, 2009-04-29 at 10:05 -0700, Scott David Daniels wrote:
 Bruno Desthuilliers wrote:
  d = {}
  for line in open(thefile):
 arr = line.strip().split()
 d[arr[0]] = arr
 
 Sorry, not picking on Bruno in particular, but I keep seeing
 this formulation around various places.
 When does line.strip().split() ever differ from line.split()?

Good question.  I can't count the number of times I've used
line.strip().split() in my own code, just because I didn't 1) read the
documentation closely enough, or 2) try the alternative, but lo and
behold, they are the same, at least in the cases I was trying to account
for.

' a b c '.split() == ' a b c '.strip().split() == 'a b c'.split()

Thanks for pointing this out.

Cheers,
Cliff


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


Re: Restart generator when it is exhausted.

2009-04-28 Thread J. Cliff Dyer

On Tue, 2009-04-28 at 10:41 +, Duncan Booth wrote: 
 Lacrima lacrima.ma...@gmail.com wrote:
 
  If it is not possible what are common techniques to use iterator or
  generator objects that allow restarting when it is needed?
 
 The usual thing if you want to use the generator's output more than once  
 would be to convert the generator to a list, then you can iterate over it 
 as often as you want.
 
  a = ['a', 'b', 'c']
  g = (i for i in a)
  restartable = list(g)
 
 If you want the output of the generator to potentially change each time you 
 iterate then you need to create a new generator.
 

More verbosely, but without putting your generator in , you can use the
iterator protocol to create a reusable iterable:

An iterable is a class with an __iter__ method that returns an iterator.

So for example:

class Iterator(object):
def __init__(self, filename):
self.f = open(filename)

def __iter__(self):
return self

def next(self):
line = self.f.readline()
if not line:
raise StopIteration
return line.strip()[:8]

is an iterator (which is also an iterable), which will grab each line of
a file, returning the first eight non-whitespace characters until the
file is used up.  Then the iterator is exhausted, and will continue to
raise StopIteration each time it is called.

class Iterable(object):
def __init__(self, filename):
self.filename = filename

def __iter__(self):
return Iterator(self.filename)

This is a reusable iterable which returns a new instance of the Iterator
class above each time it is exhausted.

So given a file hello.txt:

  Hello world
Hola mundo
  Guten tag, weld.

The classes can be used as followed:
 iterator = Iterator('hello.txt')
 for i in xrange(3):
 print *** %d *** % i
 for j in iterator:
 print j
*** 0 ***
Hello wo
Hola mun
Guten ta
*** 1 ***
*** 2 ***
 iterable = Iterable('hello.txt')
 for i in xrange(3):
 print *** %d *** % i
 for j in iterable:
 print j
*** 0 ***
Hello wo
Hola mun
Guten ta
*** 1 ***
Hello wo
Hola mun
Guten ta
*** 2 ***
Hello wo
Hola mun
Guten ta

When Iterator hits a StopIteration, it passes out of the inner loop, and
when it comes back in, the inner loop calls iterator.__iter__(), and
gets the same exhausted iterator (which immediately breaks the inner
loop by raising StopIteration).  In Iterable, when the loop calls
iterable.__iter__(), it gets a fresh iterator, so it can loop over the
file again.

The important thing is that when you call x.__iter__() (which you do
when entering a loop), you get a fresh iterator that won't just call
StopIteration right away.

Cheers,
Cliff


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


Re: Lambda alternative?

2009-04-17 Thread J. Cliff Dyer
On Thu, 2009-04-16 at 13:33 +0200, Hrvoje Niksic wrote:
 mousemeat mousem...@gmail.com writes:
 
  Correct me if i am wrong, but i can pickle an object that contains a
  bound method (it's own bound method).
 
 No, you can't:
 
  import cPickle as p
  p.dumps([])
 '(l.'
  p.dumps([].append)
 Traceback (most recent call last):
   File stdin, line 1, in module
 TypeError: expected string or Unicode object, NoneType found

Yes he can.  mousemeat stated that he could pickle an object that
*contains* a bound method, not that he could pickle the method itself.

That said, you can make an instance method out of a lambda, just as well
as any named function, and you can pickle that object, too:

Python 2.5.2 (r252:60911, Oct  5 2008, 19:29:17) 
[GCC 4.3.2] on linux2
Type help, copyright, credits or license for more information.
 import cPickle as p
 class Foo(object):
... a = lambda self, x: x+1
 foo.a(1)
2
 type(foo.a)
type 'instancemethod'
 p.dumps(foo)
'ccopy_reg\n_reconstructor\np1\n(c__main__\nFoo\np2\nc__builtin__
\nobject\np3\nNtRp4\n.'

Cheers,
Cliff


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


Re: [OT] large db question about no joins

2009-04-17 Thread J. Cliff Dyer
On Thu, 2009-04-16 at 14:11 -0700, John Fabiani wrote:
 Daniel Fetchinson wrote:
 
  Hi folks, I've come across many times the claim that 'joins are bad'
  for large databases because they don't scale
 
 IMO that's bull...

OK.  That makes four legs so far

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

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


Re: extract Infobox contents

2009-04-08 Thread J. Cliff Dyer
On Wed, 2009-04-08 at 01:57 +0100, Rhodri James wrote:
 On Tue, 07 Apr 2009 12:46:18 +0100, J. Clifford Dyer  
 j...@sdf.lonestar.org wrote:
 
  On Mon, 2009-04-06 at 23:41 +0100, Rhodri James wrote:
  On Mon, 06 Apr 2009 23:12:14 +0100, Anish Chapagain
  anishchapag...@gmail.com wrote:
 
   Hi,
   I was trying to extract wikipedia Infobox contents which is in format
   like given below, from the opened URL page in Python.
  
   {{ Infobox Software
   | name   = Bash
 [snip]
   | latest release date= {{release date|mf=yes|2009|02|20}}
   | programming language   = [[C (programming language)|C]]
   | operating system   = [[Cross-platform]]
   | platform   = [[GNU]]
   | language   = English, multilingual ([[gettext]])
   | status = Active
 [snip some more]
   }} //upto this line
  
   I need to extract all data between {{ Infobox ...to }}
 
 [snip still more]
 
  You end up with 'infoboxes' containing a list of all the infoboxes
  on the page, each held as a list of the lines of their content.
  For safety's sake you really should be using regular expressions
  rather than 'startswith', but I leave that as an exercise for the
  reader :-)
 
 
  I agree that startswith isn't the right option, but for matching two
  constant characters, I don't think re is necessary.  I'd just do:
 
  if '}}' in line:
  pass
 
  Then, as the saying goes, you only have one problem.
 
 That would be the problem of matching lines like:
 
   | latest release date= {{release date|mf=yes|2009|02|20}}
 
 would it? :-)
 

That's the one.

 A quick bit of timing suggests that:
 
if line.lstrip().startswith(}}):
  pass
 
 is what we actually want.
 

Indeed.  Thanks.

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


Re: Eval Problem

2009-04-07 Thread J. Cliff Dyer
OK.

You still haven't shown the code where tableTop gets defined, so your
code is unrunnable.  However, I think your problem is that wherever
tableTop lives, it isn't part of your globals or locals in eval.  See
the documentation on evals here:

http://www.python.org/doc/1.4/lib/node26.html

Something like the following might work:

print eval(line, {'tableTop': tableTop})

Cheers,
Cliff

On Tue, 2009-04-07 at 08:38 -0400, Victor Subervi wrote:
 
 I have excluded the code where I call the separate text files for
 printing normal text. They work. It's my code that I cannot get to
 work. For example, if I take out the eval part of the above, it will
 nicely print the commands, such as this:
 
 tableTop(123,456)
 
 which is supposed to call said fn. If I place that line in the file
 calling the text files and the bits file it will execute just fine,
 but that inevitably makes my job harder. Ideas?
 TIA,
 Victor

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


Re: Introducing Python to others

2009-03-26 Thread J. Cliff Dyer

On Thu, 2009-03-26 at 09:35 +, Paddy O'Loughlin wrote:
 Hi,
 As our resident python advocate, I've been asked by my team leader to
 give a bit of a presentation as an introduction to python to the rest
 of our department.
 It'll be less than an hour, with time for taking questions at the end.
 
 There's not going to be a whole lot of structure to it. First, I'm
 going to open up a python terminal and show them how the interpreter
 works and a few basic syntax things and then a file .py files (got to
 show them that python's indenting structure is not something to be
 afraid of :P). I think I'll mostly show things in the order that they
 appear in the python tutorial (http://docs.python.org/tutorial/).
 
 My question to you, dear python-list, is what suggestions do you have
 for aspects of python that I should show them to make them maybe think
 that python is better than what they are using at the moment.
 All of the audience will be experienced (4+ years) programmers, almost
 all of them are PHP developers (2 others, plus myself, work in C, know
 C#, perl, java, etc.).
 Because of this, I was thinking of making sure I included exceptions
 and handling, the richness of the python library and a pointing out
 how many modules there were out there to do almost anything one could
 think of.
 Anything else you think could make PHP developers starting think that
 python is a better choice?
 If I were to do a (very) short demonstration one web framework for the
 PHP devs, what should I use? CherryPy (seems to be the easiest),
 Django (seems to be the biggest/most used), or something else?
 
 Any other suggestions for a possible wow reaction from an audience like 
 that?
 
1) For PHP developers, I'm a big fan of clean namespaces.  Show no code
with `from foo import *`.  Instead show them (using how by using `import
foo` and `from foo import x, y, z`, your namespace only has those things
you explicitly imported, plus the contents of `dir(__builtins__)`.  

2) Aliasing imports is also cool.  Show people how easy it is to switch
from 

 import MySQLdb as db
to
 import psycopg2 as db

and have all your dbapi2 code still work.  Or from

 from StringIO import StringIO
to
 from cStringIO import StringIO

3) Functions as first-class variables (which could connect to a
discussion of decorators or of dictionaries as dispatch tables).

4) List comprehensions

5) Generators using yield

6) Also very handy is the interactive interpreter and the ability to
type `dir(foo)` and `help(foo)` to see what facilities are available to
you.


 Thanks,
 Paddy
 

Cheers,
Cliff

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


Re: Mangle function name with decorator?

2009-03-25 Thread J. Cliff Dyer
On Wed, 2009-03-18 at 08:18 -0700, Adam wrote:
 On Mar 18, 10:33 am, J. Cliff Dyer j...@sdf.lonestar.org wrote:
  You might be interested in redefining __getattribute__(self, attr) on
  your class.  This could operate in conjunction with the hash tables
  (dictionaries) mentioned by andrew cooke.  i.e. (untested code):
 
  class C(object):
  def __init__(self):
  self._get_table = {}
  self._post_table = {}
 
  def __getattribute__(self, x):
  if self.method=='GET':
  return object.__getattribute__(self, _get_table)[x]
  elif self.method=='POST':
  return object.__getattribute__(self, _post_table)[x]
  else:
  raise AttributeError
  @GET
  def foo(x):
  return Got, x
  @POST
  def foo(x)
  return Posted to, x
 
  This is definitely not functional code, but might get you in the right
  direction on __getattribute__.  __getattr__ might also work for you.  I
  haven't worked too much with these corners of python.
 
  Cheers,
  Cliff
 
 Hey, Cliff.  Thanks for sharing this idea.  Unfortunately, providing a
 way to actually call the method with the mangled name is relatively
 easy, and there are options there.  The real issue, to me, seems to be
 finding a way to prevent Python from eating all but the last version
 of a function definition in a class.  While decorators are a elegant
 and unintrusive approach, I don't believe that there is any way for a
 decorator to collection information in a data structure and then
 provide that data back to the class instance or the class's metaclass.
 

If your __getattribute__ dispatches method calls from a dictionary, it
shouldn't matter if the original name of the method has been blown away.
The actual method object lives on, and is accessed normally from the
user's standpoint.

I'm just thinking out loud now on ideas I haven't tested myself, but
what if your decorator were itself a method of the same class, so you
had something like this:

class BarBar(object):
def decorate_get(self, func):
pass
def decorate_post(self, func):
pass

@self.decorate_get
def foo(self):
pass
@self.decorate_post
def foo(self):
pass

Then the decorator has access to self, and can pass around whatever
information it needs.  Maybe the decoration methods should live on a
base class somewhere.

class Decoratable(object):
def get(self, func):
pass
def post(self, func):
pass

class BarBar(Decoratable):
@self.get
def foo(self):
pass
@self.post
def foo(self)
pass

 I'm beginning to think that I am trying to get the square peg of
 Python to fit into the round hole of a .NET idiom.  

Well, square is just one abstraction layer away from round anyway,
right?

 I am trying to
 borrow what I think is a really elegant and useful idiom from ASP.NET
 MVC.  Specifically, in an ASP.NET MVC Controller class, I can have two
 methods with the same name that are called for different HTTP Verbs by
 applying an Attribute:
 
 public ActionResult Payment() {
 ViewData[Title] = Payment Information;
 ViewData[submit_text] = Next ;
 
 return View();
 }
 
 [AcceptVerbs(HttpVerbs.Post)]
 public ActionResult Payment(FormCollection form) {
 
 return RedirectToAction(Legal);
 }
 
 Right?  The first Payment method is called when the Payment page is
 rendered.  The second is called when the form that it contains is
 submitted.  I find it to be readable, elegant and it does not intrude
 into the actual logic of the method itself.  The alternatives that I
 can readily identify are less-than-optimal.  For instance, if could
 just have an if statement inside the body of the method that branches
 on the HTTP verb:
 
 def Payment(self):
 if self.request.verb == 'GET':
 # Do stuff for GET
 elif self.request.verb == 'POST':
 # So stuff for POST
 
 Yes, it would work, but it is ugly and requires me to mix in the same
 function the behaviors for two very separate things.
 
 Or, I could do something like this:
 
 def GET_Payment(self):
 # Do stuff for GET
 
 def POST_Payment(self):
 # Do stuff for POST
 
 This is trivially-easy to implement (in this case, a metaclass can
 very easily manipulate the namespace), but it makes the source code
 less appealing and just feels crufty and hacky.  It also makes it
 difficult to deal elegantly with having one method respond to more
 than verb like I could if I could write:
 
 @GET
 @POST
 def foo(self):
 # Do stuff for page foo, if it is GET or POST; PUT and DELETE not
 allowed!
 
 
 --
 http://mail.python.org/mailman/listinfo/python-list
 

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


Re: Another of those is issues.

2009-03-24 Thread J. Cliff Dyer
On Fri, 2009-03-20 at 11:20 -0700, Emanuele D'Arrigo wrote:
 Hi everybody,
 
 I was unit testing some code today and I eventually stumbled on one of
 those is issues quickly solved replacing the is with ==. Still,
 I don't quite see the sense of why these two cases are different:
 
  def aFunction():
 ... pass
 ...
  f = aFunction
  f is aFunction
 True   --- Ok, this seems reasonable. Nevertheless, I suspect I
 shouldn't quite rely on it.
 
  class MyClass(object):
 ... def myMethod(self):
 ... pass
 ...
  c = MyClass()
  m = c.myMethod
  m is c.myMethod
 False  --- What? Why is that?
 
 In my mind I was expecting that when the method is assigned to m all
 that it happens is that its address is assigned to the name m so
 that effectively the same address is now pointed to by two names, like
 in the function case. I googled around for some hint but I wouldn't
 exactly say I'm clear on the issue just yet...
 
 Can anybody shed some light? Or point to a resource to look at? Or
 what's the bit of python's source code that is responsible for dealing
 with those assignments?
 
 Manu
 

So here's a f'rinstance counterexample for you:

class TempAttributeClass(object):
  def __init__(self):
self.temp = True

  def foo(self, x):
return len(x) + 1

  def __getattribute__(self, attr):
attribute = object.__getattribute__(self,attr)
if hasattr(attribute, '__call__'):
  if object.__getattribute__(self, 'temp'):
self.temp = False
return len
  else:
return attribute
else:
  return attribute

The first time a method is accessed from an instance of this class, it
will return len instead.

 print TempAttributeClass.foo
unbound method TempAttributeClass.foo
 c = TempAttributeClass()
 l = [1,2,3]
 x = c.foo
 x(l) 
3
 c.foo
4
 x == c.foo
False
 print x
built-in function len
 print y
bound method TempAttributeClass.foo of __main__.TempAttributeClass
object at 0x7f672b35e290

c.foo is a bound attribute, but what has it been bound to?  Well, I
guess it technically, it's bound to the instance c, but what has it been
bound from?  That depends first on what it encounters when traversing
its base classes, and second on how it's accessing its attributes.  As
the example above shows, python is too dynamic to make any guarantees
about any of that.

Another way you could mess with that is by changing the __class__
attribute on c.

class A(object):
x = 4
def __init__(self):
self.y = 5

class B(object):
x = u'cow'
def __init__(self):
self.y = u'goat'

 c = A()
 c.x
4
 c.y
5
 c.__class__ = B
 # Note that neither c nor x were changed in the last step
... c.x # Class attribute found on B now
u'cow'
 c.y # Instance attribute: already initialized from A.__init__
5
 c.__init__() # Reinitialize c, now using B.__init__
 c.y # Re-initialized instance attribute
u'goat'


Cheers,
Cliff



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


Re: Mangle function name with decorator?

2009-03-18 Thread J. Cliff Dyer
You might be interested in redefining __getattribute__(self, attr) on
your class.  This could operate in conjunction with the hash tables
(dictionaries) mentioned by andrew cooke.  i.e. (untested code):

class C(object):
def __init__(self):
self._get_table = {}
self._post_table = {}

def __getattribute__(self, x):
if self.method=='GET':
return object.__getattribute__(self, _get_table)[x]
elif self.method=='POST':
return object.__getattribute__(self, _post_table)[x]
else:
raise AttributeError
@GET
def foo(x):
return Got, x
@POST
def foo(x)
return Posted to, x

This is definitely not functional code, but might get you in the right
direction on __getattribute__.  __getattr__ might also work for you.  I
haven't worked too much with these corners of python.

Cheers,
Cliff

On Tue, 2009-03-17 at 13:54 -0400, andrew cooke wrote:
 ah, ok.  then yes, you can do that with decorators.  you'd need hash
 tables or something similar in a metaclass.  then the decorator would take
 the given function, stick it in the appropriate hash table, and return a
 function that does the dispatch (ie at run time does the lookup from the
 hash table) (you'd only want to set the function once once, so it would
 need to check the function wasn't already defined).  also, the decorator
 would have one name and get/post etc would be an argument.
 
 this is all documented in the docs and peps, although it's very spread
 around.  you might look at the code for the property decorator - it's not
 doing what you want, but it's an interesting non-trivial example that's
 public.
 
 http://docs.python.org/library/functions.html#property
 
 andrew
 
 
 Adam wrote:
  Thanks, Andrew.  I'm trying to accomplish something with a
  metaprogramming flavor, where, for the convenience of the programmer
  and the clarity of code, I'd like to have a decorator or some other
  mechanism do twiddling behind the scenes to make a class do something
  it wouldn't normally do.
 
  Here's a less-obfuscated exmaple:  I want to build a framework for
  creating MVC web applications in Python (I know, I know, there's
  already 2^1bazillion of them) where a controller class can have
  methods that respond to the same action but for different HTTP verbs:
 
  class foo_controller(Controller):
  @GET
  def new(self):
  # Display the form to create a new foo
 
  @POST
  def new(self):
  # Receive a form post with new foo data in it
 
  The Controller class will do all of the work behind the scenes to
  makes sure that the correct method is called at run-time, but for the
  sake of the programmer, I'd like to supply a clear, friendly syntax
  like this.  Without a little metaprogramming magic, last-in-wins, and
  only the second version of foo will end up in the class, so I'd like
  to mangle the names to something that will result in a unique
  attribute name and that the Controller class can work with behind the
  scenes to do the right thing at run time.
 
  So, Python experts, am I completely barking up the wrong tree here?
  --
  http://mail.python.org/mailman/listinfo/python-list
 
 
 
 
 --
 http://mail.python.org/mailman/listinfo/python-list
 

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


Re: Battleship style game

2009-02-25 Thread J. Cliff Dyer
On Wed, 2009-02-25 at 15:54 -0500, Shawn Milochik wrote:
 On Wed, Feb 25, 2009 at 3:15 PM, Diez B. Roggisch de...@nospam.web.de wrote:
 
  Not really. The point about properties is that you *can* make attribute
  access trigger getter or setter code.
 
  But not that you do unless there is an actual reason for that. The way you
  do it now is simply introducing clutter, without benefit. Your class would
  be half the current size - without loss of functionality.
 
 
 
  Diez
  --
  http://mail.python.org/mailman/listinfo/python-list
 
 
 It is true that it would be fewer lines of code with the same
 functionality, but it's better practice to have that framework in
 place so that any changes made in the future wouldn't break any of the
 code accessing my class. Obviously this is a fairly simple game that
 has a fixed set of rules, but I'm trying to cultivate good habits, and
 I don't think that doing it this way is anti-Pythonic.
 
 Unless, of course, anything I said is wrong, which is always possible.
 If I'm missing a bigger-picture idea, I'd like to know about it.
 

The piece you're missing is exactly why properties are so cool.

They take what looks like attribute access from the client side, and
pass it through a method.  So while you can add any sort of changes you
want to make can be made without breaking any client code.  To them, it
still looks like attribute access.

class Foo(object):
a = 4

class Bar(object):
def __init__(self):
self._a = 4

def _get_a(self):
return self._a

def _set_a(self, value):
if not value % 2:
self._a = value
a = property(_get_a, _set_a)

 foo = Foo()
 foo.a
4
 foo.a = 5
 foo.a
5
 bar = Bar()
 bar.a
4
 bar.a = 5
 bar.a
4
 bar.a = 6
 bar.a
6

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

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


Re: Is there something easier than ORM?

2009-02-17 Thread J. Cliff Dyer

On Tue, 2009-02-17 at 06:15 -0800, 一首诗 wrote:
 Thanks for your reply.
 
 With sqlalchemy, an mapped must living in a session, you have no way
 to disconnect it with its session.
 
 For example :
 
 #-
 user = session.query(User).first()
 session.expunge(user)
 print user.name   #Error here
 #-
 
 I just want to get an read-only copy of user disconnected with session
 to avoid  unexpected database operation.
 But after expunge, properties of user is not accessible anymore.

For that, the Right Thing to Do(tm) is to log in to your session with
read-only permissions.



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


Re: RedHat 4

2009-02-16 Thread J. Cliff Dyer
It *works* in RHEL 4, but it doesn't come as a package.  RHEL4 ships
with Python 2.3, and RHEL 5 only ships with Python 2.4, even though 2.5
had been out for god knows how long when it came out.

Though I haven't tested it, I wouldn't recommend replacing the system's
python binary with 2.5 if you do install it yourself.  Instead, install
it to python2.5, and set the shebang line (#!) on your scripts to point
to /usr/local/bin/python2.5  (or wherever you install it).  

Cheers,
Cliff

On Mon, 2009-02-16 at 07:28 -0800, Germán Gutiérrez wrote:
 Hi,
 
 I need to confirm and certificate python 2.5 works in a RedHat
 Enterprise 4 and I don't know where I find that information. Please,
 if anybody knows some URL in RedHat or Python with this information
 email me.
 
 Thanks
  
 Germán Gutiérrez Gayoso
 Temuco
 
 
 
 --
 http://mail.python.org/mailman/listinfo/python-list

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


Re: Pythonic way to determine if one char of many in a string

2009-02-16 Thread J. Cliff Dyer

On Mon, 2009-02-16 at 00:28 -0500, Nicolas Dandrimont wrote:
 * pyt...@bdurham.com pyt...@bdurham.com [2009-02-16 00:17:37 -0500]:
 
  I need to test strings to determine if one of a list of chars is
  in the string. A simple example would be to test strings to
  determine if they have a vowel (aeiouAEIOU) present.
  I was hopeful that there was a built-in method that operated
  similar to startswith where I could pass a tuple of chars to be
  tested, but I could not find such a method.
  Which of the following techniques is most Pythonic or are there
  better ways to perform this type of match?
  # long and hard coded but short circuits as soon as match found
  if 'a' in word or 'e' in word or 'i' in word or 'u' in word or
  ... :
  -OR-
  # flexible, but no short circuit on first match
  if [ char for char in word if char in 'aeiouAEIOU' ]:
  -OR-
  # flexible, but no short circuit on first match
  if set( word ).intersection( 'aeiouAEIOU' ):
 
 I would go for something like:
 
 for char in word:
 if char in 'aeiouAEIUO':
 char_found = True
 break
 else:
 char_found = False
 
 (No, I did not forget to indent the else statement, see
 http://docs.python.org/reference/compound_stmts.html#for)
 
 It is clear (imo), and it is seems to be the intended idiom for a search
 loop, that short-circuits as soon as a match is found.
 

If performance becomes an issue, you can tune this very easily, so it
doesn't have to scan through the string 'aeiouAEIOU' every time, by
making a set out of that:

vowels = set('aeiouAEIOU')
for char in word 
if char in vowels:
return True
return False

Searching in a set runs in constant time.  


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

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


Re: wxPython and Croatian characters

2009-02-16 Thread J. Cliff Dyer
On Mon, 2009-02-16 at 20:06 +0100, Diez B. Roggisch wrote:
 vedrandeko...@gmail.com schrieb:
  Hello,
  
  I have problem with configuring my wxPython script to work with
  Croatian characters like:  đ,š,ž,č,ć.
  Here is my simple script without wxPython (this script works):
  
# -*- coding: utf-8 -*-
s = hello normal string đšžćč
print s
  
  ..here is my snippet with wxPython:
  
  text = wx.StaticText(self, -1,Matični broj,(0,100)) # in this
  example,we have character č
  
  ...when I run this text, it looks something like:  Mati,some weird
  characters ,and ni
 
 Unless you are using python 3.0 (which I doubt, afaik no wx available), 
 your above coding declaration is useless for the shown piece of code, as 
 it only applies to unicode literals, which are written with a preceding u.
 

No.  The coding declaration does nothing to unicode literals.  It only
affects how the python's source code parser reads the the source code.
Without it, your source code will be parsed (or is it lexed?) by python
as an ascii document.  So if your document is UTF-8, it will choke as
soon as it reaches a non ascii character.  If it's encoded in UTF-16,
however, it will choke right away, as it will immediately come across a
\x00 byte, which is treated as the ascii NULL character, which is not
legal in python source code.

But print will still try to encode all unicode objects to the encoding
of your terminal, and any file writing operations will try to decode as
ASCII, unless explicitly told otherwise.  Same as without the -*- coding
-*- declaration. 

For the OP:  

When dealing with potentially non-ascii text, always use unicode objects
instead of strings, and explicitly encode them to the encoding you want
on output, unless your printing facility (here wx.StaticText) handles
that for you.  I also don't know how wxpython works in this regard.  

So:

s = uMatični broj # instead of Matični broj
text = wx.StaticText(self, -1, s,(0,100))
# or if that doesn't work try this:
#text = wx.StaticText(self, -1, s.encode('utf-8'), (0,100))

Cheers,
Cliff


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


Re: English-like Python

2009-02-03 Thread J. Cliff Dyer

On Thu, 2009-01-22 at 09:07 -0700, Joe Strout wrote:
   Beep
 
  Doesn't get much more readable and syntax-free than that.
  
  readable doesn't mean smallest amount of syntax possible sometimes
 syntax 
  increases the readability of a text as you would see if we for
 example 
  dropped all punctuation but you probably already knew that but
 perhaps 
  you didnt draw the connection with programming language wink
 
 Cute.  But I stand by my contention that Beep is about the most 
 readable way imaginable to express the make a beep sound now please 
 command, and any additional punctuation (parentheses, semicolons,
 etc.) 
 only get in the way.
 

But what if your language allows functions to be used as first class
objects?  (Mine does :))  

x = Beep

Does that assign the name x to the Beep object or does it assign the
result of a Beep call to x?

There's no virtue in making ridiculously simple things even simpler if
it makes the interesting things impossible.

def tone_sequence(sound):
sequence = DialTone.followed_by(sound)
sequence()

for x in Beep, Buzz, Warble:
tone_sequence(x)

Cheers,
Cliff


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


Re: English-like Python

2009-02-03 Thread J. Cliff Dyer

On Tue, 2009-02-03 at 08:33 -0700, Joe Strout wrote:
 J. Cliff Dyer wrote:
 
  But what if your language allows functions to be used as first class
  objects?  (Mine does :))  
  
  x = Beep
  
  Does that assign the name x to the Beep object or does it assign the
  result of a Beep call to x?
 
 It assigns the result.  To assign the name to the Beep object requires a 
 bit of additional syntax; in RB, this would be
 
   x = AddressOf Beep
 
  There's no virtue in making ridiculously simple things even simpler if
  it makes the interesting things impossible.
 
 Of course.  The goal is (or should be) to make the common things 
 ridiculously simple, and make the uncommon things only somewhat less so. 
   Even in Python, it is far more common to invoke a function than to 
 need a reference to it.  So invoking a function should be the thing that 
 requires no extra syntax (even including ()).  Getting a reference to 
 it, which is less common, should be the thing that requires more syntax.
 

Except that now you have introduced an inconsistency into python.  What
does the following mean?

my_object = MyObject()
x = my_object

What if MyObject defines a method named __call__?

Now some objects are passed around using

x = my_object

while others require

x = AddressOf my_object

and the only way to tell which is which is to check the object for the
presence of a __call__ method.  

This seems like a serious inconsistency to me.

 Cheers,
 - Joe
 

Cliff

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


Re: var is None vs. var == None

2009-01-27 Thread J. Cliff Dyer

On Fri, 2009-01-23 at 19:31 -0500, Benjamin Kaplan wrote:
 
 
 On Fri, Jan 23, 2009 at 7:28 PM, Gary Herron
 gher...@islandtraining.com wrote:
 Steven D'Aprano wrote:
  On Fri, 23 Jan 2009 14:58:34 -0500, Gerald Britton wrote:
 
 
  Hi -- Some time ago I ran across a comment recommending
 using var is
  None instead of var == None (also var is not None,
 etc.)
 
 
  That entirely depends on whether you wish to test for
 something which
  *is* None or something with *equals* None. Those two things
 have
  different meanings.
 
 
 
 Actually, for None, those two things *are* the same.   If
 something
 *equals* None, it also *is* None.  This is a consequence of
 the fact
 that there is only ever one value of None anywhere in the
 system.
 
 Not if someone decided to be a PITA.
  
  class A(object) :
 ...def __eq__(self, other) :
 ...   return other is None 
 ... 
  a = A()
  a == None
 True
  a is None
 False
 
 

or slightly less PITAish:

class EqualThing(object):
def __eq__(self, other):
return True

Could be a useful sentinel value in some cases.


 
 
  I wonder, do newbies actually get the impression from
 somewhere that is
  is a synonym for ==?
 
 
 
 Yes.  Such questions pop up regularly, and are usually dealt
 with quickly.
 
 
 
 
  My own
  testing indicates that the former beats the latter by about
 30% on
  average.  Not a log for a single instruction but it can add
 up in large
  projects.
 
 
  If you have a large project where the time taken to do
 comparisons to
  None is a significant portion of the total time, I'd be very
 surprised.
 
  var is None is a micro-optimization, but that's not why we
 do it. We do
  it because usually the correct test is whether var *is* None
 and not
  merely equal to None. Any random object might happen to
 equal None
  (admittedly most objects don't), but only None is None.
 
 
 You don't have that quite right.  The only way something can
 *equal*
 None is if it *is* None.
 None is not a value an object can have, but rather it is a
 (singleton)
 object that can be referenced.  Setting something *equal* to
 None is
 accomplished by making it refer to the single None object, at
 which
 point it *is* None.
 
 Gary Herron
 
 
 
 
 --
 http://mail.python.org/mailman/listinfo/python-list
 
 --
 http://mail.python.org/mailman/listinfo/python-list

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


Re: v = json.loads({'test':'test'})

2009-01-27 Thread J. Cliff Dyer

On Sun, 2009-01-25 at 14:28 -0800, gert wrote:
 On Jan 25, 11:16 pm, Дамјан Георгиевски gdam...@gmail.com wrote:
   raise ValueError(errmsg(Expecting property name, s, end))
  http://docs.python.org/library/json.html
   What am I doing wrong ?
 
  try this
  v = json.loads('{test:test}')
 
  JSON doesn't support single quotes, only double quotes.
 
 the funny part is when you print(v) you get
 {'test': 'test'}
 
 Single quotes works in every browser that support json so i
 recommended python should support it too, besides it looks much
 cleaner
 {'test': 'test'}
 {test: test}
 
 It can not be that hard to support both notation can it ?

It's not that hard, but it does add a noticeable level of complexity to
the process.  In JSON, you simply tell the parser, if you see a
double-quote character, we're now parsing a string.  When you see
another (unescaped) double-quote character, the string is done.

The equivalent version for your extended JSON would say, if you see a
quote character of either kind, we're parsing a string.  When you see
another (unescaped) quote character of either kind, the string is done.
But that doesn't work, because it would allow {'test: test'}.  So the
parser has to remember which quote character is being used, and require
those characters to be escaped, and not the other (but then does te
\'st render as rte'st or rte\'st?) and only close the string when
the appropriate quote is found.  

Not an impossible task, but certainly more complex than the current
parsing requirements for JSON.

Cheers,
Cliff


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


Re: Newby: how to transform text into lines of text

2009-01-26 Thread J. Cliff Dyer

On Sun, 2009-01-25 at 18:23 -0800, John Machin wrote:
 On Jan 26, 1:03 pm, Gabriel Genellina gagsl-...@yahoo.com.ar
 wrote:
  En Sun, 25 Jan 2009 23:30:33 -0200, Tim Chase  
  python.l...@tim.thechases.com escribió:
 
 
 
   Unfortunately, a raw rstrip() eats other whitespace that may be  
   important.  I frequently get tab-delimited files, using the following  
   pseudo-code:
 
  def clean_line(line):
return line.rstrip('\r\n').split('\t')
 
  f = file('customer_x.txt')
  headers = clean_line(f.next())
  for line in f:
field1, field2, field3 = clean_line(line)
do_stuff()
 
   if field3 is empty in the source-file, using rstrip(None) as you suggest  
   triggers errors on the tuple assignment because it eats the tab that  
   defined it.
 
   I suppose if I were really smart, I'd dig a little deeper in the CSV  
   module to sniff out the right way to parse tab-delimited files.
 
  It's so easy that don't doing that is just inexcusable lazyness :)
  Your own example, written using the csv module:
 
  import csv
 
  f = csv.reader(open('customer_x.txt','rb'), delimiter='\t')
  headers = f.next()
  for line in f:
   field1, field2, field3 = line
   do_stuff()
 
 
 And where in all of that do you recommend that .decode(some_encoding)
 be inserted?
 

If encoding is an issue for your application, then I'd recommend you use
codecs.open('customer_x.txt', 'rb', encoding='ebcdic') instead of open()

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

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


Re: I'm a python addict !

2009-01-26 Thread J. Cliff Dyer

On Mon, 2009-01-26 at 14:43 -0800, J Kenneth King wrote:
 Linuxguy123 linuxguy...@gmail.com writes:
 
  I just started using python last week and I'm addicted.
 
  I hate Perl.  I never did learn to use it with any competence.  I has to
  be the most obfuscated, cryptic language I've ever seen.  Making it
  object oriented only makes it worse !
  .. snip ..
 
 I program full-time in Python, so I share your excitement and
 enthusiasm. But bashing Perl like that doesn't make you sound very
 smart. I'm probably one of the very few Python programmers who came from
 (and still occassionally) use Perl. 

Really?

I think many many python programmers cut their teeth on Perl.  I for
one.  I loved programming in it when I did, but I hate having to try to
understand OPP.  Now, when I deal with Perl, it's mostly legacy code,
and it's a miserable experience.

 I've written non-trivial programs in
 it and from my experience I can tell you that it's actually a great
 language. The Moose object system is well beyond Python's class
 system. But I guess you wouldn't know that.
 
 So yay for Python, but don't get in the habit of criticising that which
 you do not know.

There are legitimate reasons to criticize things even when they are
powerful.

Cheers,
Cliff


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


Re: I'm a python addict !

2009-01-26 Thread J. Cliff Dyer

On Fri, 2009-01-23 at 20:25 -0800, Paul McGuire wrote:
 Want to change the type/behavior of an object from class A to class
 B?  How about this:
 
 aobj = A()
 aobj.__class__ = B
 
 Try *that* in as simple-looking C++ or Java!

Wow.  That looks very powerful and fun.  But scary.  Any thoughts on how
you would use that in a way that wouldn't unleash sulphurous code
smells?

Cheers,
Cliff


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


Re: I'm a python addict !

2009-01-26 Thread J. Cliff Dyer

On Mon, 2009-01-26 at 09:52 -0800, Paul McGuire wrote:
 On Jan 26, 10:54 am, J. Cliff Dyer j...@sdf.lonestar.org wrote:
  On Fri, 2009-01-23 at 20:25 -0800, Paul McGuire wrote:
   Want to change the type/behavior of an object from class A to class
   B?  How about this:
 
   aobj = A()
   aobj.__class__ = B
 
   Try *that* in as simple-looking C++ or Java!
 
  Wow.  That looks very powerful and fun.  But scary.  Any thoughts on how
  you would use that in a way that wouldn't unleash sulphurous code
  smells?
 
 
 This technique is perfect for implementing the GoF State pattern.
 
 In the State pattern, you implement behavior for an object's various
 states using one of several interchangeable classes.  The classes are
 interchangeable in that they all implement a common interface.  Here
 is my favorite State pattern example, a traffic light:
 
 
 import time
 
 class TrafficLight(object):
 pass
 
 class RedLight(TrafficLight):
 cars_can_go = False
 pedestrians_can_cross = True
 color = (255,0,0)
 duration = 20
 
 class YellowLight(TrafficLight):
 cars_can_go = True
 pedestrians_can_cross = False
 color = (255,255,0)
 duration = 5
 
 class GreenLight(TrafficLight):
 cars_can_go = True
 pedestrians_can_cross = False
 color = (0,255,0)
 duration = 15
 
 # now add in next_state class vars for state transitions
 RedLight.next_state = GreenLight
 YellowLight.next_state = RedLight
 GreenLight.next_state = YellowLight
 TrafficLight.initial_state = RedLight
 
 # run a traffic light for a while...
 can_they = lambda cond : (can't,can)[cond]
 light = TrafficLight.initial_state()
 while 1:
 print light.__class__.__name__
 print waiting for, light.duration, seconds
 print Cars, can_they(light.cars_can_go), go
 print People, can_they(light.pedestrians_can_cross), cross
 print
 time.sleep(light.duration)
 
 # how you have to do it in C++ and Java
 # light = light.next_state()
 
 # using Python
 light.__class__ = light.next_state
 
 
 Gives this output:
 
 RedLight
 waiting for 20 seconds
 Cars can't go
 People can cross
 
 GreenLight
 waiting for 15 seconds
 Cars can go
 People can't cross
 
 YellowLight
 waiting for 5 seconds
 Cars can't go
 People can't cross
 
 RedLight
 waiting for 20 seconds
 Cars can't go
 People can cross
 
 ... and so on ...
 
 
 
 In Python, the base TrafficLight class isn't even necessary (don't
 need no stinking interfaces!), although it is a good place to define
 default behavior, and it helps tie together the other classes from a
 self-documentation standpoint.  But any class that has the necessary
 attributes would suffice, whether it inherits from TrafficLight or
 not.
 
 class HoldForEmergencyVehiclesLight(object):
 cars_can_go = False
 pedestrians_can_cross = False
 color = (255,0,0)
 
 
 -- Paul
 

Thanks.  That makes sense.  But your example creates a new instance of
the new class each time, rather than changing the class of a persistent
instance, as the original example, to which I was responding, did.

But perhaps something like:

class TrafficLight(object):
def change_light(self):
self.__class__ = next_state

and then you can persist information about the light (bulb_type in
set(['led', 'incandescent']), last_maintenance_date, location, etc.) on
the instance level, unaffected by the changing color.

Interesting stuff.

Cheers,
Cliff


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


Re: I'm a python addict !

2009-01-26 Thread J. Cliff Dyer

On Mon, 2009-01-26 at 12:37 -0800, Paul McGuire wrote:
 On Jan 26, 2:06 pm, J. Cliff Dyer j...@sdf.lonestar.org wrote:
 
  Thanks.  That makes sense.  But your example creates a new instance of
  the new class each time, rather than changing the class of a persistent
  instance, as the original example, to which I was responding, did.
 
 
 Look closer.  The line that creates a new instance is commented out,
 with the notation how you have to do it in C++ and Java.  The actual
 Python code is just below, and just assigns a new class to
 self.__class__, as in the original example.
 

Right.  Sorry about that.


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

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


Re: The First Law Of comp.lang.python Dynamics

2009-01-23 Thread J. Cliff Dyer
I dub it Schluehr's law.


On Thu, 2009-01-22 at 21:39 -0800, Kay Schluehr wrote:
 Whatever sufficiently sophisticated topic was the initially discussed
 it ends all up in a request for removing reference counting and the
 GIL.
 
 --
 http://mail.python.org/mailman/listinfo/python-list
 

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


Skull Socks (was Re: Convention vs. fascism)

2009-01-16 Thread J. Cliff Dyer


On Fri, 2009-01-16 at 08:57 +, Steven D'Aprano wrote:
 On Fri, 16 Jan 2009 10:03:28 +0200, Hendrik van Rooyen wrote:
 
  Oh come on you lot - you are carrying on as if Diez were wearing his
  skull socks again - do me a favour and give him a break!

 And... skull socks? Cool. Where can I get some?
 
 

http://www.letmegooglethatforyou.com/search?q=skull+socks

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


Re: Creating new instances of subclasses.

2009-01-09 Thread J. Cliff Dyer
Thanks for the solutions everyone!  I'm not sure which I'll end up
using, but I think I've got a better grasp of the problem now.  

Cool stuff.

Cheers,
Cliff

On Thu, 2009-01-08 at 06:52 -0800, Paul McGuire wrote:
 On Jan 7, 12:00 pm, Paul McGuire pt...@austin.rr.com wrote:
  On Jan 7, 10:38 am, J. Cliff Dyer j...@unc.edu wrote:
 
   I want to be able to create an object of a certain subclass, depending
   on the argument given to the class constructor.
 
   I have three fields, and one might need to be a StringField, one an
   IntegerField, and the last a ListField.  But I'd like my class to
   delegate to the proper subclass automatically, so I can just do:
 
f1 = Field('abc')
f2 = Field('123')
f3 = Field('D,E,F')
 
  O-O is not always the solution to every problem.  Since inheritance is
  getting in your way, try using a class-level factory method.  Instead
  of using the Field constructor, use a staticmethod of Field, something
  like:
 
  @staticmethod
  def make_Field(a)
  if is_list(a):
  return ListField(a)
  elif is_integer(a):
  return IntegerField(a)
  else:
  return StringField(a)
 
  and then get rid of all those __new__ methods, too.
 
  -- Paul
 
 After looking this over a bit more, I decided I didn't like make_Field
 having to know the criteria for creating the different subclasses, but
 wanted to put the smarts into the subclasses themselves.  Here is an
 excerpt that shows this working:
 
 class Field(object):
 def __init__(self, input):
 super(Field, self).__init__(input)
 self.data = input
 
 @staticmethod
 def make_Field(a):
 subs = (ListField, IntegerField, StringField)
 ret = None
 for cls in subs:
 try:
 ret = cls(a)
 except TypeError:
 continue
 else:
 break
 return ret
 
 class IntegerField(Field):
 def __new__(cls, a):
 if not is_integer(a):
 raise TypeError()
 return Field.__new__(cls, a)
 
 ...
 ListField has a similar __new__ method, and StringField just creates
 the object, with no validation.
 
 make_Field still has to know what order to list the subclasses in
 (StringField is the most permissive, and so must come last in the list
 of subclasses), but the specific type tests are moved into the
 subclasses, which is a more appropriate place I think.
 
 -- Paul
 --
 http://mail.python.org/mailman/listinfo/python-list
 

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


Re: Problem with -3 switch

2009-01-09 Thread J. Cliff Dyer

On Fri, 2009-01-09 at 13:13 -0500, Steve Holden wrote:
 Aivar Annamaa wrote:
  As was recently pointed out in a nearly identical thread, the -3
  switch only points out problems that the 2to3 converter tool can't
  automatically fix. Changing print to print() on the other hand is
  easily fixed by 2to3.
 
  Cheers,
  Chris
 
  
  I see.
  So i gotta keep my own discipline with print() then :)
  
 Only if you don't want to run your 2.x code through 2to3 before you use
 it as Python 3.x code.
 
 regards
  Steve

And mind you, if you follow that route, you are programming in a
mightily crippled language.  It's about as bad as trying to write
cross-browser CSS.  Don't put yourself through that pain if you don't
have to.





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


Creating new instances of subclasses.

2009-01-07 Thread J. Cliff Dyer
I want to be able to create an object of a certain subclass, depending
on the argument given to the class constructor.

I have three fields, and one might need to be a StringField, one an
IntegerField, and the last a ListField.  But I'd like my class to
delegate to the proper subclass automatically, so I can just do:

 f1 = Field('abc')
 f2 = Field('123')
 f3 = Field('D,E,F')
 f1.data
'abc'
 f2.data
123
 f3.data
['D','E','F']
 type(f1)
class '__main__.StringField'
 type(f2)
class '__main__.StringField'
 type(f3)
class '__main__.ListField'

I've come up with a solution, but I suspect there's something cleaner I
can do with the inheritance structure of __new__.  I don't like
explicitly leapfrogging over Field.__new__ to object.__new__.

My attempt is below:

def is_list(arg):
if ',' in arg:  return True
else:  return False

def is_integer(arg):
try:  int(arg)
except ValueError:  return False
else:  return True

class Field(object):
def __new__(cls, a):
if is_list(a):
return ListField(a)
elif is_integer(a):
return IntegerField(a)
else:
return StringField(a)

def __init__(self, input):
super(Field, self).__init__(input)
self.data = input

class IntegerField(Field):
def __new__(cls, a):
return object.__new__(cls, a)
def __init__(self, s):
super(IntegerField, self).__init__(s)
self.s = int(self.s)

class ListField(Field):
def __new__(cls, a):
return object.__new__(cls, a)
def __init__(self, s):
super(ListField, self).__init__(s)
self.s = s.split(',')

class StringField(Field):
def __new__(cls, a):
return object.__new__(cls, a)

Is there a cleaner way to do this?  The main problem is that
Field.__new__ gets in the way of properly constructing the subclasses
once I've used it to select the proper subclass in the first place.

Cheers,
Cliff

-- 
Oook,
J. Cliff Dyer
Carolina Digital Library and Archives
UNC Chapel Hill

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


Re: python is great

2009-01-06 Thread J. Cliff Dyer

On Tue, 2009-01-06 at 10:36 -0700, Joe Strout wrote:
 I've actually been rather frustrated by Python lately. 

OFF TOPIC!!!

Please try to stay within the subject presented by the subject header.
The subject in question is python is great, not python is
frustrating.

Speaking of which, python is truly great.

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


Re: subclassing 'list'

2009-01-06 Thread J. Cliff Dyer

On Tue, 2009-01-06 at 12:34 -0800, akineko wrote:
 Hello everyone,
 
 I'm creating a class which is subclassed from list (Bulit-in type).
 
 It works great.
 However, I'm having a hard time finding a way to set a new value to
 the object (within the class).
 There are methods that alter a part of the object (ex. __setitem__()).
 But I couldn't find any method that can replace the value of the
 object.
 I wanted to do something like the following:
 
 class Mylist(list):
 
 def arrange(self):
 new_value = 
 list.self.__assign__.(self, new_value)
 
 I serached the newsgroup and found that assignment operator ('=')
 cannot be overridden because it is not an operator. But it shouldn't
 stop Python to provide a way to re-assign the value internally.

Aki,

I'm not sure I understand your problem.  What I see above is an
attribute error:

AttributeError: type object 'list' has no attribute 'self'

coming from your use of list.self, but it doesn't sound like that's what
you are asking about.  Do you mean that you want to be able to specify
some sort of processing to be done when you assign to an attribute of
your object?  To do that, you need to use properties:

import math
class Circle(object):
 def __init__(self, radius=1.0):
 self._radius = radius
 self.circum = 2.0 * self._radius * math.pi

 def __get_radius(self):
 return self._radius

 def __set_radius(self,radius):
 self._radius = radius
 self.circum = 2.0 * self._radius * math.pi

 radius = property(__get_radius, __set_radius)


Then you can do:

 circ = Circle(2.0)
 print circ.radius
2.0
 print circ.circum
12.5663706144
 circ.radius = 4.5
 print circ.radius
4.5
 print circ.circum
28.2743338823

There are lots of good tutorials on this online.


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


Re: __init__.py and package help

2009-01-05 Thread J. Cliff Dyer

On Mon, 2009-01-05 at 11:49 -0800, TechieInsights wrote:
 Ok I have read all of the tutorials and documents I could find.  I am
 running Python 2.6 on windows.  The problem I am having with packages
 is that they don't show up!
 
 Simple example of what isn't working...
 Structure-
 
 pytest/ Root directory of package
 __init__.py-  code: __all__ = ['folder']
 folder/   Another folder in the package
 __init__.py- code: __all__ = ['hello']
 hello.py- code: print('Hello World')
 
 Then I append the path to sys.path, and try and import...
 
  sys.path.append(r'E:\dev\test\pytest')
  from pytest.folder import hello
 Traceback (most recent call last):
   File stdin, line 1, in module
 ImportError: No module named pytest.folder
 
 What am I doing wrong!  It's driving me crazy.
 Thanks in advance,
 Greg

You want

 sys.path.append(r'E:\dev\test')

unless your code is in E:\dev\test\pytest\pytest\folder\hello.py


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

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


Re: help I'm getting delimited

2008-12-18 Thread J. Cliff Dyer

On Wed, 2008-12-17 at 06:28 -0800, aka wrote:
 Hi John, thanks.
 You're right, I didn't past the method header because I thought it
 didn't matter when the input filename is hardcoded.
 The try/except isn't very helpful indeed so I commented it out.
 You're right I wrongly referred to the UnicodeReader
 class in my first post because that's ultimately where I want to go
 so
 I outcommented it here for you to see.
 The fact is that neither csv.reader nor the UnicodeReader will read
 the file, while writing with the UnicodeWriter
 works like a charm.
 That's why I put str() around roles to see any content.
 I simplified the csv-file by cutting off columns without result. The
 file looks now like:
 
 id;company;department
 12;Cadillac;Research
 11;Ford;Accounting
 10;Chrysler;Sales
 
 
 The dictionary on the return is because this code is part of my
 TurboGears application.
 The entire method is:
 
 
 import csv
 from utilities.urw   import UnicodeWriter, UnicodeReader
 
 
 @expose(allow_json=True)
 def import_roles(self, input=None, *args, **kwargs):
 inp = 'C:/temp/test.csv'
 roles = []
 msg = ''
 ## try:
 fp = open(inp, 'rb')
 reader = csv.reader(fp, dialect='excel', delimiter=';')
 ## reader = UnicodeReader(fp, dialect='excel', delimiter=';')
 for r in reader:
 roles.append(r[0])
 fp.close()
 ## except:
 ## msg = Something's wrong with the csv.reader
 return dict(filepath=inp,
 roles=str(roles),
 msg=msg)
 
 
 csv.reader results in: for r in reader: Error: line contains NULL
 byte
 
 
 Use of UnicodeReader results in: UnicodeDecodeError: 'utf8' codec
 can't decode byte 0xff in position 0: unexpected code byte
 

This looks like the problem might be in your choice of codec.  A UTF-8
file will never have 0xff in it, and would be unlikely to have 0x00
either.  My guess is that you will need to decode your input from
UTF-16.  (and then use the UnicodeReader).  

 
 Will post only complete code from now on thanks.
 
 --
 http://mail.python.org/mailman/listinfo/python-list
 

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


Re: Factoring Polynomials

2008-12-18 Thread J. Cliff Dyer

On Thu, 2008-12-18 at 11:52 -0800, eric wrote:
 On Dec 18, 8:37 pm, collin.da...@gmail.com wrote:
  I am trying to write a simple application to factor polynomials. I
  wrote (simple) raw_input lines to collect the a, b, and c values from
  the user, but I dont know how to implement the quadratic equation
 
  x = (-b +or- (b^2 - 4ac)^1/2) / 2a
 
  into python. Any ideas?
 
 with numpy:
 from numpy import *
 
 s=[1,-1]
 x = -b+s*sqrt( b**2-4*a*c )/(2*a)
 
 Eric

Without the Nump.

def polynomial(a, b, c):
N = ((b**2 - 4*a*c)**.5) / 2*a
return (-b + N, -b - N)



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


  1   2   3   >