Announce: Parametric Solutions (parasol)

2009-01-25 Thread Charlie Taylor
Parasol is a python framework in which mathematical models can be
investigated parametrically. Parasol enables easy optimization, sensitivity
study, and visualization. The math model can be as big or as small as
desired. Output is generated in plain text, HTML, and native Microsoft Suite
files (Excel, Word and PowerPoint).

A problem is defined by input and output parameters. Results are presented
in a manner that conveys insight into system trends and characteristics. The
process of performing an analysis results in an ample assortment of graphs,
charts, and images that display system sensitivities, variations, and
characteristics. Barriers to creating these displays have been reduced to
single commands in order to facilitate their use.

Parasol has been designed to run under Microsoft Windows.

Home Page: http://pyparasol.sourceforge.net/
--
http://mail.python.org/mailman/listinfo/python-announce-list

Support the Python Software Foundation:
http://www.python.org/psf/donations.html


Re: Does Python really follow its philosophy of Readability counts?

2009-01-25 Thread Luis Zarrabeitia
Quoting Russ P. russ.paie...@gmail.com:

 On Jan 24, 9:54 pm, Luis Zarrabeitia ky...@uh.cu wrote:
  Quoting Russ P. russ.paie...@gmail.com:
 
  It is. For starters, I'd lose the information of this attribute was
 intended to
  be internal and I'm accessing it anyway.
 
 Not really. When you get a new version of the library and try to use
 it, you will quickly get a reminder about the change (assuming your
 tests provide sufficient converage, and also assuming that the
 attribute is not made public in the new version). So you don't really
 even need to keep track of the change.

See? With every new version that doesn't change the behaviour, I have to modify
the source just to see if the tests run. That _is_ a fork. And that's assuming
the bright case where I have the source.

  No. I am not dictating _anything_. The beauty of it, you don't have to do
  _anything_ for this to happen.
 
 You are trying to dictate that the library implementer not be allowed
 to use enforced access restriction. And, in the larger sense, you are
 trying to dictate that access restrictions not be enforced in Python.

Now, please, explain to me, why are you so interested on preventing me from
using the internals on my computer? If you want controls in the code that runs
on your system, you can.

  Or contacting him about it and maybe send him a patch, sure, why not. But
 this
  has nothing to do with enforced data hiding. Having obj._public_var is just
 as
  badly designed as having private public_var.
 
 Sure, go ahead and contact him. If he agrees that a private attribute
 should be public, then the problem is solved. But if he does not
 agree, he should not be forced to bend to your desire.

Wait, if I change my project to ignore the data hiding (enforced or not), am I
forcing the author to bend for my desire? Please explain your reasoning.

Or better yet... don't. I will just give up, right now. This is no longer about
security, good practices, software engineering, bug catching or formal
proofs as you've tried to paint it before. This is about you wanting to control
how others use your code. And while it may be your legal right, that isn't the
discussion I thought I was getting into.


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


Pythonic list/tuple/dict layout?

2009-01-25 Thread Akira Kitada
Hi,

There is more than one way to write a list/tuple/dict in Python,
and actually different styles are used in standard library.
As a hobgoblin of little minds, I rather like to know which style is
considered Pythonic
in the community.

I collected common layout from existing code and pasted them below.
My vote would go to d1. How about yours?

If there is consensus on this, that might be worth being included in PEP 8.

Thanks,


d1 = {
0: ham,
1: jam,
2: spam,
3: alot,
4: knights,
5: who,
6: say,
7: ni,
8: dead,
9: parrot,
}

d2 = {
0: ham,
1: jam,
2: spam,
3: alot,
4: knights,
5: who,
6: say,
7: ni,
8: dead,
9: parrot,}

d3 = {0: ham,
  1: jam,
  2: spam,
  3: alot,
  4: knights,
  5: who,
  6: say,
  7: ni,
  8: dead,
  9: parrot,
  }

d4 = {0: ham,
  1: jam,
  2: spam,
  3: alot,
  4: knights,
  5: who,
  6: say,
  7: ni,
  8: dead,
  9: parrot,}

d5 = {0: ham,
  1: jam,
  2: spam,
  3: alot,
  4: knights,
  5: who,
  6: say,
  7: ni,
  8: dead,
  9: parrot,
}

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


Re: Pythonic list/tuple/dict layout?

2009-01-25 Thread Stephen Hansen
 and pasted them below.
 My vote would go to d1. How about yours?


Whatever reads best within the context of the specific code is Pythonic.
--
http://mail.python.org/mailman/listinfo/python-list


Plugin system, RuntimeWarning: Parent module 'ext_abc' not found while handling absolute import

2009-01-25 Thread Torsten Mohr
Hi,

i try to write a plugin system that i want to use to let users extend
a module that i write.

Within the module there is an extension loader that loads an extension
module.  This extension module should be able to import modules from
my system, which provides some extensions.

Basically, this here works but gives a warning:
RuntimeWarning: Parent module 'ext_abc' not found while handling absolute import

Below are the files, i wonder what is wrong.

It would be great if anybody could give me a hint, what provokes that
warning?


Best regards,
Torsten.



FILE psys.py:
import mymodule.ext_loader
import sys
import os.path

here = os.path.abspath('.')
mpath = os.path.abspath('mymodule')
epath = os.path.abspath('extension')

sys.path.append(here)
sys.path.append(mpath)

mymodule.ext_loader.load('ext_abc')


FILE mymodule/__init__.py:
__all__ = ['base', 'ext_loader']


FILE mymodule/ext_loader.py:
import imp
import os.path


def search_file(fname):
for e in ['extension']:
candidate = os.path.join(os.path.expanduser(e), fname)

if os.path.exists(candidate):
return candidate

return None


def load(modname):
fname = modname + .py
abname = search_file(fname)
fd = open(abname, rb)
mod = imp.load_module(fname, fd, abname, ['py', 'r', imp.PY_SOURCE])
fd.close()


FILE mymodule/base.py:
def func1():
print func1 called !


FILE extension/ext_abc.py:
import base

base.func1()

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


Re: Pythonic list/tuple/dict layout?

2009-01-25 Thread Dan Bishop
On Jan 25, 2:18 am, Akira Kitada akit...@gmail.com wrote:
 Hi,

 There is more than one way to write a list/tuple/dict in Python,
 and actually different styles are used in standard library.
 As a hobgoblin of little minds, I rather like to know which style is
 considered Pythonic
 in the community.

 I collected common layout from existing code and pasted them below.
 My vote would go to d1. How about yours?

 If there is consensus on this, that might be worth being included in PEP 8.

 Thanks,

 
 d1 = {
     0: ham,
     1: jam,
     2: spam,
     3: alot,
     4: knights,
     5: who,
     6: say,
     7: ni,
     8: dead,
     9: parrot,

 }
 [snip]

I use d1.

Wow!  A Python debate over curly brace placement!  Imagine that!
--
http://mail.python.org/mailman/listinfo/python-list


Re: A different kind of interface

2009-01-25 Thread Marc 'BlackJack' Rintsch
On Sat, 24 Jan 2009 01:02:01 +0100, Дамјан Георгиевски wrote:

 I don't know what an IBQ is.
 
 +IBQ- seems to be the way your newsreader displays the dashes that
 where in Ben's posting.  I see em dash characters there:
 
 I see IBQ too ... also weird is that he has Content-Type: text/plain;
 charset=utf-7

Why weird?  Makes perfect sense:

In [98]: print '+IBQ-'.decode('utf-7')
—

In [99]: unicodedata.name('+IBQ-'.decode('utf-7'))
Out[99]: 'EM DASH'

So there are newsreaders out there that can't or at least don't decode 
UTF-7.

Ciao,
Marc 'BlackJack' Rintsch
--
http://mail.python.org/mailman/listinfo/python-list


Re: Pythonic list/tuple/dict layout?

2009-01-25 Thread Akira Kitada
 Wow!  A Python debate over curly brace placement!  Imagine that!

PEP8 even deals with tabs vs spaces, where to put a blank line, etc :)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Regex for Python 2.7

2009-01-25 Thread Gabriel Genellina

En Sat, 24 Jan 2009 21:51:31 -0200, MRAB goo...@mrabarnett.plus.com
escribió:


Gabriel Genellina wrote:
  En Sat, 24 Jan 2009 18:23:51 -0200, MRAB goo...@mrabarnett.plus.com
  escribió:
 
  Some time ago I discovered this difference between regular  
expressions

  in Python and Perl:
 
  Python
 
  \A matches at start of string
  \Z matches at end of string
 
  Perl
 
  \A matches at start of string
  \Z matches before terminal newline or at end of string
  \z matches at end of string
 
  In Perl \A == ^ and \Z == $ in single-string mode, but in Python \A  
== ^

  and \Z != $ in single-string mode.
 
  Why do you want the two to be equivalent? Isn't a good thing that you
  have both alternatives (\Z and $)? Use whichever is adequate in each  
case.

 
Python's \Z is equivalent to Perl's \z, but there's no equivalent to  
Perl's \Z in multi-line mode.


I tested both:
code
import re

texts = [abc\ndef, abc\n, abc]
exprs = [
 re.compile(rc$),
 re.compile(rc\Z),
 re.compile(rc$, re.MULTILINE),
 re.compile(rc\Z, re.MULTILINE),
 ]
for text in texts:
 for expr in exprs:
   m = re.search(expr, text)
   print repr(text), expr.pattern, match if m else no match
/code

c:\temppython test_re.py
'abc\ndef' c$ no match
'abc\ndef' c\Z no match
'abc\ndef' c$ match
'abc\ndef' c\Z no match
'abc\n' c$ match
'abc\n' c\Z no match
'abc\n' c$ match
'abc\n' c\Z no match
'abc' c$ match
'abc' c\Z match
'abc' c$ match
'abc' c\Z match

code
@texts = (abc\ndef, abc\n, abc);
@exprs = (qr/c$/,
 qr/c\Z/,
 qr/c$/m,
 qr/c\Z/m,
#  qr/c\z/,
#  qr/c\z/m
 );

foreach $text (@texts) {
 ($repr = $text) =~ s/\n/\\n/g;
 foreach $expr (@exprs) {
   print $repr,  , $expr,  ;
   if ($text =~ $expr) {
 print match\n;
   } else {
 print no match\n;
   }
 }
}
/code

c:\tempperl test_re.pl
abc\ndef (?-xism:c$) no match
abc\ndef (?-xism:c\Z) no match
abc\ndef (?m-xis:c$) match
abc\ndef (?m-xis:c\Z) no match
abc\n (?-xism:c$) match
abc\n (?-xism:c\Z) match
abc\n (?m-xis:c$) match
abc\n (?m-xis:c\Z) match
abc (?-xism:c$) match
abc (?-xism:c\Z) match
abc (?m-xis:c$) match
abc (?m-xis:c\Z) match

If one wants to match end-of-line or end-of-string, use $ in multiline
mode. If one wants to match end-of-string only, use \Z. If one wants to
match end-of-line only, use \n [not shown].

--
Gabriel Genellina

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


Re: Pythonic list/tuple/dict layout?

2009-01-25 Thread Michael Iatrou
When the date was Sunday 25 January 2009, Akira Kitada wrote:

 There is more than one way to write a list/tuple/dict in Python,
 and actually different styles are used in standard library.

I would vote for d1, but I don't think that this is more pythonic, I just 
consider it more clean, according to my personal aesthetic criteria. :

-- 
 Michael Iatrou (yxzb)
--
http://mail.python.org/mailman/listinfo/python-list


IDLE/Python on Asus EEE PC

2009-01-25 Thread Alex van der Spek
Simple Python programs edited and run through IDLE work fine on my Ubuntu 
Linux system without any editing.

However on my Asus EEE PC IDLE complains about incorrect formatting 
(indentation) or possibly mixing tabs/spaces. The source code is exactly 
the same. There is no incorrect formatting and certainly no use of tabs.

I created the program on my EEE, was unable to find anything wrong with 
it and decided to test it on my desktop. I was amazed that it runs fine 
there.

Can anyone explain this? I use the full desktop Xandros OS on the EEE. I 
downloaded IDLE from the Debian repositories.

Thanks in advance,
Alex van der Spek
--
http://mail.python.org/mailman/listinfo/python-list


Re: Dynamic methods and lambda functions

2009-01-25 Thread Kay Schluehr
On 23 Jan., 13:28, unine...@gmail.com wrote:
 Hi,
 I want to add some properties dynamically to a class, and then add the
 corresponding getter methods. Something resulting in this:

 class Person:
 def Getname(self):
 return self.__name

 def Getage(self):
 return self.__age

 I've implemented the next code, creating the properties from a list:

 props = [
 (name, peter),
 (age, 31),
 (wife, mary)
 ]

 class Person:
 def __init__(self):
 for prop in props:
 setattr(self, __ + prop[0], prop[1])
 setattr(Person, Get + prop[0], lambda self: getattr
 (self, __ + prop[0]))

 if __name__ == __main__:

 person = Person()

 print person.__name
 print person.__age
 print person.__wife
 print
 print person.Getname()
 print person.Getage()
 print person.Getwife()

 And the resulting execution of this program is:

 peter
 31
 mary

 mary
 mary
 mary

 The attributes are right, but the getter are not working. The problem
 is that the lambda function always execute the last parameter passed
 for all instances of the methods. How could it be done the right way?

 Thanks in advance

The standard trick is to de-closure the lambda using a keyword
argument. So instead of writing

   lambda self: getattr(self, __ + prop[0]))

you might write

   lambda self, prop = prop: getattr(self, __ + prop[0]))

Now prop is local to the lambda and the lambda doesn't look up prop in
the enclosing environment which certainly stores its last value.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Pythonic list/tuple/dict layout?

2009-01-25 Thread Ben Finney
Akira Kitada akit...@gmail.com writes:

 I collected common layout from existing code and pasted them below.
 My vote would go to d1. How about yours?
 
 If there is consensus on this, that might be worth being included in
 PEP 8.
 
 Thanks,
 
 
 d1 = {
 0: ham,
 1: jam,
 2: spam,
 3: alot,
 4: knights,
 5: who,
 6: say,
 7: ni,
 8: dead,
 9: parrot,
 }
 
 d2 = {
 0: ham,
 1: jam,
 2: spam,
 3: alot,
 4: knights,
 5: who,
 6: say,
 7: ni,
 8: dead,
 9: parrot,}

These are the only two that follow PEP 8; the others don't have
four-space indent levels.

I actually use this style:

foo = {
0: 'spam',
1: 'eggs',
2: 'beans',
}

because that makes it clear that *all* the indented lines are a
continuation of the same statement, just like a suite of statements
are all uniformly indented under (e.g.) a function definition.

It seems that the author of the Python editing mode in Emacs agrees
with me too (the style, at least, if not the reasoning), which makes
my programming life easier.

-- 
 \   “The future always arrives too fast, and in the wrong order.” |
  `\—Alvin Toffler |
_o__)  |
Ben Finney
--
http://mail.python.org/mailman/listinfo/python-list


Re: Pythonic list/tuple/dict layout?

2009-01-25 Thread Steven D'Aprano
On Sun, 25 Jan 2009 17:18:28 +0900, Akira Kitada wrote:

 Hi,
 
 There is more than one way to write a list/tuple/dict in Python, and
 actually different styles are used in standard library. As a hobgoblin
 of little minds, I rather like to know which style is considered
 Pythonic
 in the community.
 
 I collected common layout from existing code and pasted them below. My
 vote would go to d1. How about yours?

All of them.

BTW, there's no need to use such large examples. Three items per dict 
would be sufficient to illustrate the styles, using ten items doesn't add 
anything useful to the discussion.



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


Re: Is (-1 ==True) True or False? Neither

2009-01-25 Thread Steven D'Aprano
On Sun, 25 Jan 2009 03:07:04 +0200, Oktay Şafak wrote:

 The reason is that when
 someone writes (-1 == True) he is clearly, definitely, absolutely asking
 for a boolean comparison, not a numerical one.

If I wrote (-1 == True), and I'm not sure why I would, I would expect to 
get the answer False, because -1 is not equal to True. Many things have 
truth values which are true but not equal to True.

If I wanted bool(-1) == True, I'd write bool(-1) == True. Or, if I was in 
a whimsical mood, I'd write:

((bool(-1) == True) == True) == True  # oh when to stop???

just to illustrate the foolishness of doing boolean equality comparisons 
like that.


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


Re: Pythonic list/tuple/dict layout?

2009-01-25 Thread Akira Kitada
 BTW, there's no need to use such large examples. Three items per dict
 would be sufficient to illustrate the styles, using ten items doesn't add
 anything useful to the discussion.

I worried to be told
'you can make it in a line like {ham: jam, spam: alot}'
;)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Pythonic list/tuple/dict layout?

2009-01-25 Thread Akira Kitada
 These are the only two that follow PEP 8; the others don't have
 four-space indent levels.

In those examples, the following sentence in PEP 8 would be applied.

Make sure to indent the continued line appropriately.

 I actually use this style:

foo = {
0: 'spam',
1: 'eggs',
2: 'beans',
}

 because that makes it clear that *all* the indented lines are a
 continuation of the same statement, just like a suite of statements
 are all uniformly indented under (e.g.) a function definition.

 It seems that the author of the Python editing mode in Emacs agrees
 with me too (the style, at least, if not the reasoning), which makes
 my programming life easier.

Yes, it does, but people around me tend to prefer d1 style to that one.
One purpose of this silly thread is to figure out which is most popular one...
--
http://mail.python.org/mailman/listinfo/python-list


Re: What is intvar?

2009-01-25 Thread W. eWatson

Steve Holden wrote:

W. eWatson wrote:

W. eWatson wrote:

r wrote:

here is a good explanation of control vars:
http://infohost.nmt.edu/tcc/help/pubs/tkinter/control-variables.html

Here are 3 great Tkinter refernces in order:
http://infohost.nmt.edu/tcc/help/pubs/tkinter/
http://effbot.org/tkinterbook/
http://www.pythonware.com/library/tkinter/introduction/

Thanks to all for the reference and tips.


tkinterbook is easy to follow, but it seems to have been abandoned in
2005. Did it appear in another guise somewhere else?


There hasn't been a lot of development on Tkinter in the intervening
period. It's a mature system, so there has been no need to update the
documentation.

regards
 Steve
Unfortunately, the author seems to have stopped mid-stream. I see a fair 
number of FIXMEs in it. It looks like the New Mexico pdf is a fitting 
replacement. Perhaps Intro to Tkinter should be scrapped?


Another of the author's writings, on PIL, looks good as a pdf, but is 
missing a TOC. Maybe some pdf knowledgeable person knows how to generate one 
easily.


--
   W. eWatson

 (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
  Obz Site:  39° 15' 7 N, 121° 2' 32 W, 2700 feet

Web Page: www.speckledwithstars.net/

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


Newby: how to transform text into lines of text

2009-01-25 Thread vsoler
Hello,

I'va read a text file into variable a

 a=open('FicheroTexto.txt','r')
 a.read()

a contains all the lines of the text separated by '\n' characters.

Now, I want to work with each line separately, without the '\n'
character.

How can I get variable b as a list of such lines?

Thank you for your help
--
http://mail.python.org/mailman/listinfo/python-list


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

2009-01-25 Thread Diez B. Roggisch

vsoler schrieb:

Hello,

I'va read a text file into variable a

 a=open('FicheroTexto.txt','r')
 a.read()

a contains all the lines of the text separated by '\n' characters.


No, it doesn't. a.read() *returns* the contents, but you don't assign 
it, so it is discarded.



Now, I want to work with each line separately, without the '\n'
character.

How can I get variable b as a list of such lines?



The idiomatic way would be iterating over the file-object itself - which 
will get you the lines:


with open(foo.txt) as inf:
for line in inf:
print line


The advantage is that this works even for large files that otherwise 
won't fit into memory. Your approach of reading the full contents can be 
used like this:


content = a.read()
for line in content.split(\n):
print line


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


ANN: new snapshot of the eric4 Pylons plugin released

2009-01-25 Thread Detlev Offenbach
Hi,

this is to inform everybody about the availability of the Pylons plugin
for eric4. It adds Pylons support to the eric4 Python IDE. The Plugin is
available via the eric4 web site at

http://eric-ide.python-projects.org/index.html

What is eric4
-
eric4 is a Python IDE, that comes with batteries included. It is
extensible via a plugin system. Please see the a.m. web site for more
details.

Regards,
Detlev
-- 
Detlev Offenbach
det...@die-offenbachs.de
--
http://mail.python.org/mailman/listinfo/python-list


Re: *.python.org broken?

2009-01-25 Thread Cousin Stanley

 Is anybody else having trouble accessing sites (including www, docs,
 wiki) in the python.org tree, or is it just me? (Or just .au?)

  Yes, connecting to python.org sites has been problematic
  for me as well  

  I don't remember when the trouble started, but it's been
  problematic for at least a week or longer here 


-- 
Stanley C. Kitching
Human Being
Phoenix, Arizona

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


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

2009-01-25 Thread Tim Chase
The idiomatic way would be iterating over the file-object itself - which 
will get you the lines:


with open(foo.txt) as inf:
 for line in inf:
 print line


In versions of Python before the with was introduced (as in the 
2.4 installations I've got at both home and work), this can simply be


  for line in open(foo.txt):
print line

If you are processing lots of files, you can use

  f = open(foo.txt)
  for line in f:
print line
  f.close()

One other caveat here, line contains the newline at the end, so 
you might have


 print line.rstrip('\r\n')

to remove them.



content = a.read()
for line in content.split(\n):
 print line


Strings have a splitlines() method for this purpose:

  content = a.read()
  for line in content.splitlines():
print line

-tkc



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


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

2009-01-25 Thread vsoler
On 25 ene, 14:36, Diez B. Roggisch de...@nospam.web.de wrote:
 vsoler schrieb:

  Hello,

  I'va read a text file into variable a

       a=open('FicheroTexto.txt','r')
       a.read()

  a contains all the lines of the text separated by '\n' characters.

 No, it doesn't. a.read() *returns* the contents, but you don't assign
 it, so it is discarded.

  Now, I want to work with each line separately, without the '\n'
  character.

  How can I get variable b as a list of such lines?

 The idiomatic way would be iterating over the file-object itself - which
 will get you the lines:

 with open(foo.txt) as inf:
      for line in inf:
          print line

 The advantage is that this works even for large files that otherwise
 won't fit into memory. Your approach of reading the full contents can be
 used like this:

 content = a.read()
 for line in content.split(\n):
      print line

 Diez

Thanks a lot. Very quick and clear
--
http://mail.python.org/mailman/listinfo/python-list


Re: *.python.org broken?

2009-01-25 Thread Steve Holden
Cousin Stanley wrote:
 Is anybody else having trouble accessing sites (including www, docs,
 wiki) in the python.org tree, or is it just me? (Or just .au?)
 
   Yes, connecting to python.org sites has been problematic
   for me as well  
 
   I don't remember when the trouble started, but it's been
   problematic for at least a week or longer here 
 
 
Perhaps traceroute might start to provide some information about the
problem. As far as I know there haven't been any unscheduled outages on
the python.org servers recently.

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Re: Python 3: range objects cannot be sliced

2009-01-25 Thread Alan G Isaac

On 1/16/2009 3:13 PM Alan G Isaac apparently wrote:
 It is documented:
 
http://docs.python.org/3.0/library/stdtypes.html#sequence-types-str-bytes-bytearray-list-tuple-range


But then again, the opposite is also documented,
since `range` is a sequence type.  Quoting:

Sequences also support slicing ...

Some sequences also support “extended slicing”

Is this a documentation bug, or a bug in `range`?
(I'd think the latter.)

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


Re: IDLE/Python on Asus EEE PC

2009-01-25 Thread MRAB

Alex van der Spek wrote:
Simple Python programs edited and run through IDLE work fine on my Ubuntu 
Linux system without any editing.


However on my Asus EEE PC IDLE complains about incorrect formatting 
(indentation) or possibly mixing tabs/spaces. The source code is exactly 
the same. There is no incorrect formatting and certainly no use of tabs.


I created the program on my EEE, was unable to find anything wrong with 
it and decided to test it on my desktop. I was amazed that it runs fine 
there.


Can anyone explain this? I use the full desktop Xandros OS on the EEE. I 
downloaded IDLE from the Debian repositories.



If it's not tabs, then is it line endings?
--
http://mail.python.org/mailman/listinfo/python-list


Re: *.python.org broken?

2009-01-25 Thread Akira Kitada
http://downforeveryoneorjustme.com/

On Sun, Jan 25, 2009 at 10:06 AM,  tgvaug...@gmail.com wrote:
 Hi all,

 Is anybody else having trouble accessing sites (including www, docs,
 wiki) in the python.org tree, or is it just me? (Or just .au?)

 Cheers,

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

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


[MacOS] Multiple versions of a module

2009-01-25 Thread Pierre-Alain Dorange
How to manage a module with several versions on MacOS X ?

All modules are installed in :
/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-pa
ckages/
This is the default path for Mac.

But for some modules i need several version (stable 1.8.1 and test 1.9.0
for example : for pygame here).

I've installed the stable one from source and i go into a
pygame-1.8.0release-py2.5-macosx-10.3-i386.egg directory. Nice.

I just install the 1.9 (for test) and it go into pygame directory

So now  i got 2 versions of pygame in the site-packages dir...

When i import pygam
e from python, the 1.8.0 is imported only and 1.9.0 is complely
ignored... How do python choose from several package ? 
Do package register them elsewhere ?
I don't know how to test (temporaly) the 1.9, but also be able to go
back easily to 18.8 after ?

I do a site._test() and i return my only 1.8.0 module for pygame, 1.9.0
is ignored. The same with

Any clue ?


-- 
Pierre-Alain Dorange
http://microwar.sourceforge.net/
--
http://mail.python.org/mailman/listinfo/python-list


Re: Byte oriented data types in python

2009-01-25 Thread Ravi

 Take a look at the struct and ctypes modules.

struct is really not the choice. it returns an expanded string of the
data and this means larger latency over bluetooth.

ctypes is basically for the interface with libraries written in C
(this I read from the python docs)

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


Re: Byte oriented data types in python

2009-01-25 Thread Ravi
On Jan 25, 12:52 am, Martin v. Löwis mar...@v.loewis.de wrote:
  packet_type (1 byte unsigned) || packet_length (1 byte unsigned) ||
  packet_data(variable)

  How to construct these using python data types, as int and float have
  no limits and their sizes are not well defined.

 In Python 2.x, use the regular string type: chr(n) will create a single
 byte, and the + operator will do the concatenation.

 In Python 3.x, use the bytes type (bytes() instead of chr()).

This looks really helpful thanks!

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


Re: progress: compiling python2.5 under msys (specifically but not exclusively under wine) with msvcr80

2009-01-25 Thread Luke Kenneth Casson Leighton
 Have you made some benchmarks like pystone?

 Cheers,
 Cesare

Cesare, hi, thanks for responding: unfortunately, there's absolutely
no point in making any benchmark figures under an emulated environment
which does things like take 2 billion instruction cycles to start up a
program named c:/msys/bin/sh.exe, due to it inexplicably loading 200
GUI-only truetype fonts.

and to do benchmarks on say windows would require that i install ...
windows!  so if somebody else would like to make some benchmarks, and
publish them, they are most welcome to do so.

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


Re: [MacOS] Multiple versions of a module

2009-01-25 Thread Diez B. Roggisch

Pierre-Alain Dorange schrieb:

How to manage a module with several versions on MacOS X ?

All modules are installed in :
/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-pa
ckages/
This is the default path for Mac.

But for some modules i need several version (stable 1.8.1 and test 1.9.0
for example : for pygame here).

I've installed the stable one from source and i go into a
pygame-1.8.0release-py2.5-macosx-10.3-i386.egg directory. Nice.

I just install the 1.9 (for test) and it go into pygame directory

So now  i got 2 versions of pygame in the site-packages dir...

When i import pygam
e from python, the 1.8.0 is imported only and 1.9.0 is complely
ignored... How do python choose from several package ? 
Do package register them elsewhere ?

I don't know how to test (temporaly) the 1.9, but also be able to go
back easily to 18.8 after ?

I do a site._test() and i return my only 1.8.0 module for pygame, 1.9.0
is ignored. The same with


Use the excellent virtualenv-package from Ian Bicking.

http://pypi.python.org/pypi/virtualenv

Nowaday,s nearly everything I develop first gets it's own VE before 
anything is installed.


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


Re: Logging help

2009-01-25 Thread Aahz
In article f4fc590c-f31d-46be-b769-9d746b8b8...@w39g2000prb.googlegroups.com,
koranthala  koranth...@gmail.com wrote:
   Is it possible somehow to have the logging module rotate the files
every time I start it.

If you're on Linux, why not use logrotate?
-- 
Aahz (a...@pythoncraft.com)   * http://www.pythoncraft.com/

Weinberg's Second Law: If builders built buildings the way programmers wrote 
programs, then the first woodpecker that came along would destroy civilization.
--
http://mail.python.org/mailman/listinfo/python-list


Reading the first MB of a binary file

2009-01-25 Thread Max Leason
Hi,

I'm attempting to read the first MB of a binary file and then do a md5
hash on it so that i can find the file later despite it being moved or
any file name changes that may have been made to it. These files are
large (350-1400MB) video files and i often located on a different
computer and I figure that there is a low risk for generating the same
hash between two files. The problem occurs in the read command which
returns all \x00s. Any ideas why this is happening?

Code:
open(Chuck.S01E01.HDTV.XViD-YesTV.avi, rb).read(1024)
b'\x00\x00\x00\x00\x00\x00\x00'
--
http://mail.python.org/mailman/listinfo/python-list


Re: progress: compiling python2.5 under msys (specifically but not exclusively under wine) with msvcr80

2009-01-25 Thread Ross Ridge
Luke Kenneth Casson Leighton  l...@lkcl.net wrote:
this is a progress report on compiling python using entirely free
software tools, no proprietary compilers or operating systems
involved, yet still linking and successfully running with msvcr80
assemblies.

MSVCR80.DLL is part of the Microsoft Visual C++ runtime library, and isn't
free software in the FSF sense.  It's free as in beer, but then so is
the Microsoft compiler.

Ross Ridge

-- 
 l/  //   Ross Ridge -- The Great HTMU
[oo][oo]  rri...@csclub.uwaterloo.ca
-()-/()/  http://www.csclub.uwaterloo.ca/~rridge/ 
 db  //   
--
http://mail.python.org/mailman/listinfo/python-list


Re: Reading the first MB of a binary file

2009-01-25 Thread MRAB

Max Leason wrote:
 Hi,

 I'm attempting to read the first MB of a binary file and then do a
 md5 hash on it so that i can find the file later despite it being
 moved or any file name changes that may have been made to it. These
 files are large (350-1400MB) video files and i often located on a
 different computer and I figure that there is a low risk for
 generating the same hash between two files. The problem occurs in the
 read command which returns all \x00s. Any ideas why this is
 happening?

 Code:
 open(Chuck.S01E01.HDTV.XViD-YesTV.avi, rb).read(1024)
 b'\x00\x00\x00\x00\x00\x00\x00'

You're reading the first 1024 bytes. Perhaps the first 1024 bytes of the
file _are_ all zero!

Try reading more and checking those, eg:


SIZE = 1024 ** 2
 open(Chuck.S01E01.HDTV.XViD-YesTV.avi, rb).read(SIZE) == 
b'\x00' * SIZE

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


Efficient multi-slicing technique?

2009-01-25 Thread python
Is there an efficient way to multi-slice a fixed with string into
individual fields that's logically equivalent to the way one
would slice a delimited string using .split()?
Background: I'm parsing some very large, fixed line-width text
files that have weekly columns of data (52 data columns plus
related data). My current strategy is to loop through a list of
slice()'s to build a list of the specific field values for each
line. This is fine for small files, but seems inefficient. I'm
hoping that there's a built-in (C based) or 3rd party module that
is specifically designed for doing multiple field extractions at
once.
Thank you,
Malcolm
--
http://mail.python.org/mailman/listinfo/python-list


Re: Byte oriented data types in python

2009-01-25 Thread Steve Holden
Ravi wrote:
 Take a look at the struct and ctypes modules.
 
 struct is really not the choice. it returns an expanded string of the
 data and this means larger latency over bluetooth.
 
If you read the module documentation more carefully you will see that it
converts between the various native data types and character strings.
Thus each native data type occupies only as many bytes as are required
to store it in its native form (modulo any alignments needed).

 ctypes is basically for the interface with libraries written in C
 (this I read from the python docs)
 
I believe it *is* the struct module you need.

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Counting number of objects

2009-01-25 Thread Kottiyath
Hi,
I am creating a class called people - subclasses men, women, children
etc.
I want to count the number of people at any time.
So, I created code like the following:

class a(object):
counter = 0
def __new__(cls, *args, **kwargs):
a.counter += 1
return object.__new__(cls, *args, **kwargs)

def __del__(self):
a.counter -= 1

class aa(a):
pass

Now, the code works Ok. I have the following questions:
1. Is this code Ok? Is there any straightforward mechanism other than
this to get the number of objects?
2. I read in Python Documentation that inside __del__ we should the
minimum of interaction with external parameters. So, I am a little
worried in subclassing __del__ to check the counter. Is whatever I
have done Ok?

Another question - unrelated to the major topic:
How much time does it take to be proficient in Python? I have been
working exclusively in Python for close to 3 months now, and even now
I get inferiority complex when I read the answers sent by many of you.
I have been programming for close to 7 years now (earlier in a
language similar to COBOL).
Does it take quite a bit of time to be proficient - as many of you
guys - or am I just dumb?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Counting number of objects

2009-01-25 Thread Steve Holden
Kottiyath wrote:
 Hi,
 I am creating a class called people - subclasses men, women, children
 etc.
 I want to count the number of people at any time.
 So, I created code like the following:
 
 class a(object):
 counter = 0
 def __new__(cls, *args, **kwargs):
 a.counter += 1
 return object.__new__(cls, *args, **kwargs)
 
 def __del__(self):
 a.counter -= 1
 
 class aa(a):
 pass
 
 Now, the code works Ok. I have the following questions:
 1. Is this code Ok? Is there any straightforward mechanism other than
 this to get the number of objects?
 2. I read in Python Documentation that inside __del__ we should the
 minimum of interaction with external parameters. So, I am a little
 worried in subclassing __del__ to check the counter. Is whatever I
 have done Ok?
 
Yes. Just be aware that if instances become involved in cyclic data
structures (or in implementations other than CPython, where reference
counting isn't used) __del__ might not be called until garbage
collection kicks in, so you may want a more explicit way to stop an
instance from being in the count.

 Another question - unrelated to the major topic:
 How much time does it take to be proficient in Python? I have been
 working exclusively in Python for close to 3 months now, and even now
 I get inferiority complex when I read the answers sent by many of you.
 I have been programming for close to 7 years now (earlier in a
 language similar to COBOL).
 Does it take quite a bit of time to be proficient - as many of you
 guys - or am I just dumb?

By your code above you seem to be doing OK. Python is like an iceberg -
only an eighth of what goes on is above the surface. That eighth will
suffice for many people's total programming needs.

I've been using Python ten years, and I am still learning. Just go at
your own pace, and carry on asking for help when you need it.

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Re: Byte oriented data types in python

2009-01-25 Thread Grant Edwards
On 2009-01-25, Ravi ra.ravi@gmail.com wrote:

 Take a look at the struct and ctypes modules.

 struct is really not the choice. it returns an expanded string of the
 data and this means larger latency over bluetooth.

I don't know what you mean by returns an expanded string of
the data.

I do know that struct does exactly what you requested.

It converts between Python objects and what is bascially a C
struct where you specify the endianness of each field and
what sort of packing/padding you want.

I use the struct module frequenty to impliment binary,
communications protocols in Python.  I've used Python/struct
with transport layers ranging from Ethernet (raw, TCP, and UDP)
to async serial, to CAN.

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


Re: Counting number of objects

2009-01-25 Thread Andreas Waldenburger
On Sun, 25 Jan 2009 09:23:35 -0800 (PST) Kottiyath
n.kottiy...@gmail.com wrote:

 Hi,
 I am creating a class called people - subclasses men, women, children
 etc.
 I want to count the number of people at any time.
 So, I created code like the following:
 
 class a(object):
 counter = 0
 def __new__(cls, *args, **kwargs):
 a.counter += 1
 return object.__new__(cls, *args, **kwargs)
 
 def __del__(self):
 a.counter -= 1
 
 class aa(a):
 pass
 
This looks OK, although I'd suggest using cls.counter += 1 instead of
a.counter += 1 in the __new__() method. Just seems clearer to me,
esp. when you think about subclassing. This would create an asymmetry
with __del__() then. Oh well. So maybe use self.__class__.counter -= 1
there, even if it is a bit ugly-ish.

Another way to go would be to use the weakref module and create a
weakref-set (or list) as the counter. That way you would
only need to add the objects in the __new__() method and not worry
about removing them. I will admit that this is overengineering the
problem a bit, but might be a good exercise.

A third option could be to remove the counting functions from the class
altogether. From an OO-Design point of view this would seem
appropriate, because neither any individual nor mankind as a whole
would know the exact amount of people in the world of hand. An entity
that would actually count all the people in the world, however, would
know and it makes sense to implement it separately (as a subclass of
list with birth() and death() methods, for instance). I'm just saying
this to give you something to think about, I'm not saying that it is
necessarily better or worse for your example.


 Now, the code works Ok. I have the following questions:
 1. Is this code Ok? Is there any straightforward mechanism other than
 this to get the number of objects?
I would say that you found the most obvious implementation there.
Depending on your definition of straightforward you could do it as I
outlined in my last example above, using explicit calls for births and
deaths. This would remove the behind the scenes magic a bit, which
may be a plus.


 2. I read in Python Documentation that inside __del__ we should the
 minimum of interaction with external parameters. So, I am a little
 worried in subclassing __del__ to check the counter. Is whatever I
 have done Ok?
 
Again, seems good to me, unless you do some other trickery that may
lead to __del__() not being called directly. In that case, do look at
the weakref module.


 Another question - unrelated to the major topic:
 How much time does it take to be proficient in Python?
Don't concern yourself with that question at all, is my advice.

Most people can learn to write programs in under a week, and many still
don't write *good* programs 20 years later. You'll get better over
time, as long as you keep doing it. Thinking about that you're not good
enough will just consume mental resources that would be better invested
in your programs.


 I have been
 working exclusively in Python for close to 3 months now, and even now
 I get inferiority complex when I read the answers sent by many of you.
Hello?! :)

Three months is *nothing* compared to the time those many of you
folks have invested. Don't worry. Does it matter if you don't
understand some stuff people write? As long as you pick out the stuff
you do understand you're still gaining stuff from this group.


 I have been programming for close to 7 years now (earlier in a
 language similar to COBOL).
Well, you have quite a background then. Why all the worries?


 Does it take quite a bit of time to be proficient - as many of you
 guys
YES! Of course it does.


 - or am I just dumb?
You're writing programs and you're communicating with like-minded
people about your problems (in a socially appropriate way). Not what
dumb people do, in my book.


cheers,
/W

-- 
My real email address is constructed by swapping the domain with the
recipient (local part).
--
http://mail.python.org/mailman/listinfo/python-list


Re: Does Python really follow its philosophy of Readability counts?

2009-01-25 Thread Mark Wooding
Russ P. russ.paie...@gmail.com writes:

 Calling a one-word change a fork is quite a stretch, I'd say.

I wouldn't.  I've forked a project P if I've made a different version of
it which isn't going to be reflected upstream.  Now I've got to maintain
my fork, merging in changes from upstream as they happen, and upgrading
all the things which use my new version; if I want to distribute my
program M to other people, they'll also need my forked version of
whatever.  Now suppose that two programs A and B both require one-word
changes in P: there's a combinatorial explosion of little patches which
need to be managed.

A fork is a fork, regardless of how big the change is.  The problem with
a fork is the maintenance problem it entails.

Besides, if I want to do some hacky debugging in ipython, should I
really have to recompile and reinstall piles of libraries?

  Has it occurred to you that some users might actually *want* access
  controls? Maybe some users want to actually use the library as the
  author intended it to be used. What a bizarre concept!

 Huh?
 Then... use it as the author intended. I am _not_ forcing you to use the
 obj._protected attributes!

 But what if I want an automatic check to verify that I am using it as
 the author intended? Is that unreasonable?

You mean that you can't /tell/ whether you typed mumble._seekrit?
You're very strange.  It's kind of hard to do by accident.  I'd have
thought that you could do that with grep, err...

git grep '\._' | sed 's/self\._//g' | grep '\._'

ought to do as a rough start.

If you can't trust your programmers to make it clear when they're doing
something dubious, I think you have bigger problems.

-- [mdw]
--
http://mail.python.org/mailman/listinfo/python-list


Re: Does Python really follow its philosophy of Readability counts?

2009-01-25 Thread Mark Wooding
Russ P. russ.paie...@gmail.com writes:

 Imagine a person who repairs computers. He is really annoyed that he
 constantly has to remove the cover to get at the guts of the computer.
 So he insists that computers cases should be made without covers.

Poor analogy.  He gets fed up that the computers he's meant to be
servicing are arriving in sealed containers which require specialist
tools to open.

 After all, manufacturers put covers on computers only because they
 don't trust us and think we're too stupid to safely handle an
 uncovered computer box.

It's more to do with keeping dust out, keeping air circulating, and
keeping fingers away from sharp edges.  Fortunately most computers are
actually shipped in cases one can remove easily, using household
tools -- or even no tools at all.  Why, anyone would think that you were
supposed to be able to grub about in there!

 That is logically equivalent to your position on enforced access
 restrictions in software.

It is now that I've fixed it.

-- [mdw]
--
http://mail.python.org/mailman/listinfo/python-list


Re: Pythonic list/tuple/dict layout?

2009-01-25 Thread Rhamphoryncus
d1
--
http://mail.python.org/mailman/listinfo/python-list


Re: Does Python really follow its philosophy of Readability counts?

2009-01-25 Thread Russ P.
On Jan 25, 10:04 am, Mark Wooding m...@distorted.org.uk wrote:
 Russ P. russ.paie...@gmail.com writes:
  Calling a one-word change a fork is quite a stretch, I'd say.

 I wouldn't.  I've forked a project P if I've made a different version of
 it which isn't going to be reflected upstream.  Now I've got to maintain
 my fork, merging in changes from upstream as they happen, and upgrading
 all the things which use my new version; if I want to distribute my
 program M to other people, they'll also need my forked version of
 whatever.  Now suppose that two programs A and B both require one-word
 changes in P: there's a combinatorial explosion of little patches which
 need to be managed.

 A fork is a fork, regardless of how big the change is.  The problem with
 a fork is the maintenance problem it entails.

Not really. A fork is something that *diverges* from the original.
That means the differences *grow* over time. In this case, the
differences will not grow over time (unless you access more private
attributes).

As I pointed out before, you don't even need to keep track of the
changes you made. You will be automatically reminded as soon as you
get a new version of the library and try to use it (again, assuming
that your tests provide sufficient coverage and the attribute is not
changed to public).

   Has it occurred to you that some users might actually *want* access
   controls? Maybe some users want to actually use the library as the
   author intended it to be used. What a bizarre concept!

  Huh?
  Then... use it as the author intended. I am _not_ forcing you to use the
  obj._protected attributes!

  But what if I want an automatic check to verify that I am using it as
  the author intended? Is that unreasonable?

 You mean that you can't /tell/ whether you typed mumble._seekrit?
 You're very strange.  It's kind of hard to do by accident.  I'd have

If I have a team of 200 programmers, I can't easily tell if one of
them did that somewhere. Why do people like you have such a hard time
understanding that I'm not talking here about smallish programs with
one or a few developers?

And even with only one programmer, he might access mumble._seekrit
for a debugging test, then forget to take it out.

 thought that you could do that with grep, err...

         git grep '\._' | sed 's/self\._//g' | grep '\._'

 ought to do as a rough start.

 If you can't trust your programmers to make it clear when they're doing
 something dubious, I think you have bigger problems.

Yes, I think I have bigger problems. But I like the challenge. I don't
think I'd be happy working on small problems, but to each his own.
--
http://mail.python.org/mailman/listinfo/python-list


Monitor a FTP site for arrival of new/updated files

2009-01-25 Thread python
Any suggestions on a best practice way to monitor a remote FTP
site for the arrival of new/updated files? I don't need specific
code, just some coaching on technique based on your real-world
experience including suggestions for a utility vs. code based
solution.
My goal is to maintain a local collection of files synced with a
remote FTP site and when I download a new/updated file locally,
run a script to process it. The arrival and format of the files
that I need to sync with are beyond my control (eliminating a
rsync solution) ... all I have is a generic FTP connection to a
specific FTP address. Note: The remote site I'm monitoring may
have multiple uploads occuring at the same time.
My basic strategy is to poll the remote directory on a regular
basis and compare the new directory listing to a previous
snapshot of the directory listing. If a file timestamp or size
has changed (or a new file has appeared), then track this file as
a changed file. Once a file has been marked as changed, wait N
polling cycles for the file timestamp and size to remain stable,
then download it, and trigger a local script to process the file.
In addition to detecting new or changed files, I would compare
remote directory listings to my local sync folder and delete
local files that no longer exist on the remote site.
My concern about using a utility is the utility's ability to
detect when a remote file has finished being updated. I don't
want to download files that are still in the process of being
updated - I only want to download new/updated files after they've
been closed on the remote site.
Any ideas appreciated!
Thanks,
Malcolm
--
http://mail.python.org/mailman/listinfo/python-list


Re: Efficient multi-slicing technique?

2009-01-25 Thread MRAB

pyt...@bdurham.com wrote:
Is there an efficient way to multi-slice a fixed with string into 
individual fields that's logically equivalent to the way one would slice 
a delimited string using .split()?


Background: I'm parsing some very large, fixed line-width text files 
that have weekly columns of data (52 data columns plus related data). My 
current strategy is to loop through a list of slice()'s to build a list 
of the specific field values for each line. This is fine for small 
files, but seems inefficient. I'm hoping that there's a built-in (C 
based) or 3rd party module that is specifically designed for doing 
multiple field extractions at once.



You could try the struct module:

 import struct
 struct.unpack(3s4s1s, b123abcdX)
('123', 'abcd', 'X')

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


Re: Byte oriented data types in python

2009-01-25 Thread Stephen Hansen
On Sun, Jan 25, 2009 at 7:27 AM, Ravi ra.ravi@gmail.com wrote:


  Take a look at the struct and ctypes modules.

 struct is really not the choice. it returns an expanded string of the
 data and this means larger latency over bluetooth.


Noo... struct really IS the choice; that is the explicit purpose of the
struct library. I went and included an example too which you're not noticing
:)

Python 2.5.1 (r251:54869, Apr 18 2007, 22:08:04)
[GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
Type help, copyright, credits or license for more information.
 import struct
 data = struct.pack(BB4s, 1, 4, this)
 data
'\x01\x04this'


That's precisely six raw bytes which is exactly what you specified you
needed: exactly one unsigned byte for packet type, one unsigned byte for
length, and four bytes for the data after. In real life you'd probably use
something besides 4s to pack what you marked as other and variable, of
course, but still. Replace it with whatever your message requires.

Really, this is how you do line protocols ;) Well there's other ways, but...

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


Re: Byte oriented data types in python

2009-01-25 Thread Martin v. Löwis

 Take a look at the struct and ctypes modules.
 struct is really not the choice. it returns an expanded string of the
 data and this means larger latency over bluetooth.
 
 I don't know what you mean by returns an expanded string of
 the data.
 
 I do know that struct does exactly what you requested.

I disagree. He has a format (type, length, value), with the
value being variable-sized. How do you do that in the struct
module?

 It converts between Python objects and what is bascially a C
 struct where you specify the endianness of each field and
 what sort of packing/padding you want.

Sure. However, in the specific case, there is really no C
struct that can reasonably represent the data. Hence you
cannot really use the struct module.

 I use the struct module frequenty to impliment binary,
 communications protocols in Python.  I've used Python/struct
 with transport layers ranging from Ethernet (raw, TCP, and UDP)
 to async serial, to CAN.

Do you use it for the fixed-size parts, or also for the variable-sized
data?

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


Re: Does Python really follow its philosophy of Readability counts?

2009-01-25 Thread Mark Wooding
Steven D'Aprano st...@remove-this-cybersource.com.au writes:

 On Fri, 23 Jan 2009 21:36:59 -0500, Luis Zarrabeitia wrote:
 It makes sense... if the original author is an egotist who believes he
 must control how I use that library.

 Then I guess Guido must be such an egotist, because there's plenty of 
 internals in Python that you can't (easy) mess with.

Time for some reflection.  (Apposite word, as it turns out.)

For the avoidance of doubt, I shall grant (and not grudgingly):

  * Abstraction is a useful tool in building complex systems.

  * Separating an interface from its implementation reduces the
cognitive burden on people trying to reason about the system
(including when doing design, developing clients, or trying to do
more formal kinds of reasoning).

  * It also makes maintenance of the implementation easier: in the cases
where this it's possible to improve the implementation without
changing the interface, clients can benefit without having to be
changed.

I think that one of the reasons this conversation is going on for so
long is that we haven't really talked much about what kinds of `messing'
we're talking about.

I think that, most of the time when I'm inconvenienced by some
abstraction, it's because it's hiding something that I wanted to see --
in a read-only fashion.  The implementation knows some fact that, for
whatever reason, it's unwilling to reveal to me.  I understand that, in
some future version, the implementation might change and this fact might
not be available then, or that it's an artifact of the way the
implementation works in some environment -- but for whatever reason
(debugging is a typical one as was pointed out upthread) it turns out
that I'm actually interested in this fact.  Revealing it to me can't
actually hurt the invariants of the system, though I need to be somewhat
careful about how long I assume it's correct.  Of course, that should be
entirely my responsibility.

It's this common problem of wanting to dig out some piece of information
which I'm really worried about.  And `enforced data hiding' just slams
the door in my face.  I'm not best pleased by the idea.

Anyway, in this regard, the CPython implementation is pretty much a
paragon of virtue.  It lets one get at almost everything one could want
and a whole lot else besides.

 Yes you could, and you could hack the OS to manipulate data behind the
 scenes, and you could build a hardware device to inject whatever data
 you want directly into the memory. You can do any of those things. So
 what?

 Data hiding isn't about some sort of mythical 100% certainty against
 any imaginable failure mode. Data hiding is a tool like any other, and
 like all tools, it has uses and misuses, and it works under some
 circumstances and not others.

 If you don't get 100% certainty that there will never be a failure no 
 matter what, what do you get? Just off the top of my head, it:

How much of these do you /lose/ by having a somehat more porous
interface?

 * makes it easier for an optimising compiler to give fast code if it 
 doesn't have to assume internal details can be changed;

Irrelevant for read-only inspection.  For making modifications, this
might be a valid point, though (a) I'm unaware of any compilers
sufficiently aggressive to make very effective use of this, and (b) I'm
probably willing to accommodate the compiler by being sufficiently
careful about my hacking.  That is: go ahead, use a fancy compiler, and
I'll cope as best I can.

 * makes it easier to separate interface from implementation when you
 can trust that the implementation actually isn't being used;

Irrelevant for read-only inspection.  For making modifications: you
carry on assuming that the interface is being used as you expect, and
I'll take on the job of reasoning about invariants and making sure that
everything continues to work.

 * gives the developer more freedom to change the implementation;

For read-only inspection, I might lose if you stop providing the
information I want; I'll need to change my code, but you don't need to
care.  Probably if your implementation has changed that much, the
information isn't relevant any more anyway.

Besides, if your implementation changes break my code, I get to keep
both pieces, and you get to laugh.  What's the big deal?

 * makes it possible for meaningful correctness proofs;

Irrelevant for read-only inspection.  For making modifications, I'll
take on the responsibility for amending the proofs as necessary.

 * reduces the amount of interconnections between different parts of your 
 program by ensuring that all interaction goes through the interface 
 instead of the implementation;

For read-only inspection, I'm not sure this matter much -- if your
implementation knows a fact that I want, then either I'll get it through
your interface or dredge it out of your implementation's guts, but the
module coupling's there either way.  (If there was a better way to
obtain that fact, 

Re: Does Python really follow its philosophy of Readability counts?

2009-01-25 Thread Russ P.
On Jan 25, 10:04 am, Mark Wooding m...@distorted.org.uk wrote:

  But what if I want an automatic check to verify that I am using it as
  the author intended? Is that unreasonable?

 You mean that you can't /tell/ whether you typed mumble._seekrit?
 You're very strange.  It's kind of hard to do by accident.

But what if you type mumble._seekrit in several places, then the
library implementer decides to give in to your nagging and makes it
public by changing it to mumble.seekrit. Now suppose you forget to
make the corresponding change somewhere in your code, such as

mumble._seekrit = zzz

You will get no warning at all. You will just be inadvertently
creating a new private attribute -- and the assignment that you
really want will not get done.

For that matter, the library implementer himself could make the same
mistake and get no warning.

When you think about it, you soon realize that the leading underscore
convention violates the spirit if not the letter of one of the first
principles of programming 101: if you have a constant parameter that
appears in several places, assign the literal value in one place
rather than repeating it everywhere. Then if you need to change the
value, you only need to change it in one place. That reduces effort,
but more importantly it reduces the potential for error.

The same principle applies to declaring an attribute private. If
that declaration is encoded in every occurrence of its identifier,
then if you decide to change it to public, you need to change the
identifier at each and every location. But if a private or priv
keyword were available, you would only need to make the change in one
location.
--
http://mail.python.org/mailman/listinfo/python-list


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

2009-01-25 Thread John Machin
On Jan 26, 12:54 am, Tim Chase python.l...@tim.thechases.com wrote:

 One other caveat here, line contains the newline at the end, so
 you might have

   print line.rstrip('\r\n')

 to remove them.

I don't understand the presence of the '\r' there. Any '\x0d' that
remains after reading the file in text mode and is removed by that
rstrip would be a strange occurrence in the data which the OP may
prefer to find out about and deal with; it is not part of the
newline. Why suppress one particular data character in preference to
others?

The same applies in any case to the use of rstrip('\n'); if that finds
more than one ocurrence of '\x0a' to remove, it has exceeded the
mandate of removing the newline (if any).

So, we are left with the unfortunately awkward
if line.endswith('\n'):
line = line[:-1]

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


Re: Monitor a FTP site for arrival of new/updated files

2009-01-25 Thread Steve Holden
pyt...@bdurham.com wrote:
  Any suggestions on a best practice way to monitor a remote FTP site for
 the arrival of new/updated files? I don't need specific code, just some
 coaching on technique based on your real-world experience including
 suggestions for a utility vs. code based solution.
 
 My goal is to maintain a local collection of files synced with a remote
 FTP site and when I download a new/updated file locally, run a script to
 process it. The arrival and format of the files that I need to sync with
 are beyond my control (eliminating a rsync solution) ... all I have is a
 generic FTP connection to a specific FTP address. Note: The remote site
 I'm monitoring may have multiple uploads occuring at the same time.
 
 My basic strategy is to poll the remote directory on a regular basis and
 compare the new directory listing to a previous snapshot of the
 directory listing. If a file timestamp or size has changed (or a new
 file has appeared), then track this file as a changed file. Once a file
 has been marked as changed, wait N polling cycles for the file
 timestamp and size to remain stable, then download it, and trigger a
 local script to process the file. In addition to detecting new or
 changed files, I would compare remote directory listings to my local
 sync folder and delete local files that no longer exist on the remote site.
 
 My concern about using a utility is the utility's ability to detect when
 a remote file has finished being updated. I don't want to download files
 that are still in the process of being updated - I only want to download
 new/updated files after they've been closed on the remote site.
 
 Any ideas appreciated!
 
Well, the ftpmirror will cope with most of what you want to do as it is,
but I am unsure how you can determine whether a file is in the process
of being written on the server.

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Re: Byte oriented data types in python

2009-01-25 Thread Grant Edwards
On 2009-01-25, Martin v. Löwis mar...@v.loewis.de wrote:

 Take a look at the struct and ctypes modules.
 struct is really not the choice. it returns an expanded string of the
 data and this means larger latency over bluetooth.
 
 I don't know what you mean by returns an expanded string of
 the data.
 
 I do know that struct does exactly what you requested.

 I disagree. He has a format (type, length, value), with the
 value being variable-sized. How do you do that in the struct
 module?

You construct a format string for the value portion based on
the type/length header.

 It converts between Python objects and what is bascially a C
 struct where you specify the endianness of each field and
 what sort of packing/padding you want.

 Sure. However, in the specific case, there is really no C
 struct that can reasonably represent the data.

I don't see how that can be the case.  There may not be a
single C struct that can represent all frames, but for every
frame you should be able to come up with a C struct that can
represent that frame.

 Hence you cannot really use the struct module.

Perhaps I don't understand his requirements, but I use the
struct module for protocols with type/len/value sorts of
packets.

 I use the struct module frequenty to impliment binary,
 communications protocols in Python.  I've used Python/struct
 with transport layers ranging from Ethernet (raw, TCP, and
 UDP) to async serial, to CAN.

 Do you use it for the fixed-size parts, or also for the
 variable-sized data?

Both.  For varible size/format stuff you decode the first few
bytes and use them to figure out what format/layout to use for
the next chunk of data.  It's pretty much the same thing you do
in other languages.

-- 
Grant

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


Re: Doc for extended call syntax; was: Re: unzip array of arrays?

2009-01-25 Thread Mark Wooding
Steve Holden st...@holdenweb.com writes:

 No, you aren't mistaken. Looking at the * symbol in the 2.6
 documentation index it lists only two references. The first is the
 language manual's explanation of its use in the def statement, the
 second is a transitory reference to its use in function calls, but
 that's in the tutorial where it is not likely to get much attention.

There's a full description of it in 5.4.3 in the Language Reference, but
apparently not indexed.

-- [mdw]
--
http://mail.python.org/mailman/listinfo/python-list


Re: Byte oriented data types in python

2009-01-25 Thread Martin v. Löwis
 I disagree. He has a format (type, length, value), with the
 value being variable-sized. How do you do that in the struct
 module?
 
 You construct a format string for the value portion based on
 the type/length header.

Can you kindly provide example code on how to do this?

 I don't see how that can be the case.  There may not be a
 single C struct that can represent all frames, but for every
 frame you should be able to come up with a C struct that can
 represent that frame.

Sure. You would normally have a struct such as

struct TLV{
  char type;
  char length;
  char *data;
};

However, the in-memory representation of that struct is *not*
meant to be sent over the wire. In particular, the character
pointer has no meaning outside the address space, and is thus
not to be sent.

 Both.  For varible size/format stuff you decode the first few
 bytes and use them to figure out what format/layout to use for
 the next chunk of data.  It's pretty much the same thing you do
 in other languages.

In the example he gave, I would just avoid using the struct module
entirely, as it does not provide any additional value:

def encode(type, length, value):
  return chr(type)+chr(length)+value

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


Re: Monitor a FTP site for arrival of new/updated files

2009-01-25 Thread python
 Well, the ftpmirror will cope with most of what you want to do as it is, but 
 I am unsure how you can determine whether a file is in the process
of being written on the server.

Looks like that may be a fit. Thank you Steve!

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


Re: Reading the first MB of a binary file

2009-01-25 Thread Marc 'BlackJack' Rintsch
On Sun, 25 Jan 2009 08:37:07 -0800, Max Leason wrote:

 I'm attempting to read the first MB of a binary file and then do a md5
 hash on it so that i can find the file later despite it being moved or
 any file name changes that may have been made to it. These files are
 large (350-1400MB) video files and i often located on a different
 computer and I figure that there is a low risk for generating the same
 hash between two files. The problem occurs in the read command which
 returns all \x00s. Any ideas why this is happening?
 
 Code:
open(Chuck.S01E01.HDTV.XViD-YesTV.avi, rb).read(1024)
 b'\x00\x00\x00\x00\x00\x00\x00'

As MRAB says, maybe the first 1024 actually *are* all zero bytes.  Wild 
guess:  That's a file created by a bittorrent client which preallocates 
the files and that file above isn't downloaded completely yet!?

Ciao,
Marc 'BlackJack' Rintsch
--
http://mail.python.org/mailman/listinfo/python-list


Cartesian Product of two lists (itertools)

2009-01-25 Thread Thorsten Kampe
Hi,

is there a way to make itertools.product generate triples instead of 
pairs from two lists?

For example:
 list1 = [1, 2]; list2 = [4, 5]; list3 = [7, 8]
 from itertools import product
 list(product(list1, list2, list3))
[(1, 4, 7), (1, 4, 8), (1, 5, 7), (1, 5, 8), (2, 4, 7), (2, 4, 8), (2, 
5, 7), (2, 5, 8)]

so far so good... Now...
 list(product(product(list1, list2), list3))
[((1, 4), 7), ((1, 4), 8), ((1, 5), 7), ((1, 5), 8), ((2, 4), 7), ((2, 
4), 8), ((2, 5), 7), ((2, 5), 8)]

Oops, pairs of pairs instead triples. Not what I wanted.

What's the best way to pre-process the arguments to itertools.product 
or to post-process the result of itertools.product to get what I 
want?!

I have an older utility which I would like to replace with 
itertools.product. The old one uses a rather clumsy way to indicate that 
a triple was wanted:

def cartes(seq0, seq1, modus = 'pair'):
 return the Cartesian Product of two sequences 
if   modus == 'pair':
return [[item0, item1] for item0 in seq0 for item1 in seq1]
elif modus == 'triple':
return [item0 + [item1] for item0 in seq0 for item1 in seq1]


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


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

2009-01-25 Thread gert
raise ValueError(errmsg(Expecting property name, s, end))
http://docs.python.org/library/json.html
What am I doing wrong ?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Pythonic list/tuple/dict layout?

2009-01-25 Thread Mark Wooding
Akira Kitada akit...@gmail.com writes:

 I collected common layout from existing code and pasted them below.
 My vote would go to d1. How about yours?

It seems that I use both d1 and d4, though in both cases I omit the
trailing commas.  I use d1 when each item is on a separate line, and d4
when I'm packing them onto multiple lines.

e.g.,

op = XT.make_optparse \
 ([('E', 'error',
{'action': 'store_const', 'dest': 'type', 'const': 'error',
 'help': Mark the window as reporting an error.}),
   ## ...
   ('t', 'title',
{'dest': 'title',
 'help': Set the window's title string.})],
  version = VERSION,
  usage = '%prog [-EIQWm] [-t TITLE] [-d HEADLINE] '
  'MESSAGE [BUTTONS...]')

and

service_info = [('watch', T.VERSION, {
  'adopted': (0, 0, '', cmd_adopted),
  'kick': (1, 1, 'PEER', cmd_kick)
})]

In this latter case, were I defining multiple services, I'd indent it
differently:

service_info = [
  ('watch', T.VERSION, {
'adopted': (0, 0, '', cmd_adopted),
'kick': (1, 1, 'PEER', cmd_kick)
  }),
  ##...
]

-- [mdw]
--
http://mail.python.org/mailman/listinfo/python-list


Re: Byte oriented data types in python

2009-01-25 Thread John Machin
On Jan 26, 2:28 am, Ravi ra.ravi@gmail.com wrote:
 On Jan 25, 12:52 am, Martin v. Löwis mar...@v.loewis.de wrote:

   packet_type (1 byte unsigned) || packet_length (1 byte unsigned) ||
   packet_data(variable)

   How to construct these using python data types, as int and float have
   no limits and their sizes are not well defined.

  In Python 2.x, use the regular string type: chr(n) will create a single
  byte, and the + operator will do the concatenation.

  In Python 3.x, use the bytes type (bytes() instead of chr()).

 This looks really helpful thanks!

Provided that you don't take Martin's last sentence too literally :-)


| Python 2.6.1 (r261:67517, Dec  4 2008, 16:51:00) [MSC v.1500 32 bit
(Intel)] on win32
|  p_data = babcd # Omit the b prefix if using 2.5 or earlier
|  p_len = len(p_data)
|  p_type = 3
|  chr(p_type) + chr(p_len) + p_data
| '\x03\x04abcd'

| Python 3.0 (r30:67507, Dec  3 2008, 20:14:27) [MSC v.1500 32 bit
(Intel)] on win32
|  p_data = babcd
|  p_len = len(p_data)
|  p_type = 3
|  bytes(p_type) + bytes(p_len) + p_data # literal translation
| b'\x00\x00\x00\x00\x00\x00\x00abcd'
|  bytes(3)
| b'\x00\x00\x00'
|  bytes(10)
| b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
|  bytes([p_type]) + bytes([p_len]) + p_data
| b'\x03\x04abcd'
|  bytes([p_type, p_len]) + p_data
| b'\x03\x04abcd'

Am I missing a better way to translate chr(n) from 2.x to 3.x? The
meaning assigned to bytes(n) in 3.X is interesting:

2.X:
nuls = '\0' * n
out_byte = chr(n)

3.X:
nuls = b'\0' * n
or
nuls = bytes(n)
out_byte = bytes([n])

Looks to me like there was already a reasonable way of getting a bytes
object containing a variable number of zero bytes. Any particular
reason why bytes(n) was given this specialised meaning? Can't be the
speed, because the speed of bytes(n) on my box is about 50% of the
speed of the * expression for n = 16 and about 65% for n = 1024.

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


Re: Byte oriented data types in python

2009-01-25 Thread Martin v. Löwis
 Looks to me like there was already a reasonable way of getting a bytes
 object containing a variable number of zero bytes. Any particular
 reason why bytes(n) was given this specialised meaning?

I think it was because bytes() was originally mutable, and you need a
way to create a buffer of n bytes. Now that bytes() ended up immutable
(and bytearray was added), it's perhaps not so useful anymore. Of
course, it would be confusing if bytes(4) created a sequence of one
byte, yet bytearray(4) created four bytes.

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


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

2009-01-25 Thread Peter Otten
gert wrote:

 raise ValueError(errmsg(Expecting property name, s, end))
 http://docs.python.org/library/json.html
 What am I doing wrong ?

You need proper quotation marks:

 s = json.dumps({'test':'test'})
 s
'{test: test}'
 json.loads(s)
{u'test': u'test'}

The JSON format is described here: http://www.json.org/

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


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

2009-01-25 Thread livibetter
On Jan 26, 5:12 am, gert gert.cuyk...@gmail.com wrote:
 raise ValueError(errmsg(Expecting property name, s, 
 end))http://docs.python.org/library/json.html
 What am I doing wrong ?

You use wrong quotes, it should be wrapped by double quotes not single
quotes. Read http://json.org/:

  A string is a collection of zero or more Unicode characters,
wrapped in double quotes, ...

 v = json.loads('{test:test}')
 v
{u'test': u'test'}
--
http://mail.python.org/mailman/listinfo/python-list


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

2009-01-25 Thread J. Clifford Dyer
Please include all relevant information in the *body* of your message,
not just in the subject.  It's a pain having to piece a question back
together between the subject.

On Sun, 2009-01-25 at 13:12 -0800, gert wrote:
 raise ValueError(errmsg(Expecting property name, s, end))
 http://docs.python.org/library/json.html
 What am I doing wrong ?

JSON requires strings to be enclosed in double quotes.  It is not as
flexible as python when it comes to quotation.  If you change your
example to

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

it will work.  (Note JSON also doesn't allow trailing commas, so
'{test:test,}' will also fail)

Cheers,
Cliff


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

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


Re: Securing a database

2009-01-25 Thread M.-A. Lemburg
On 2009-01-23 08:26, kt83...@gmail.com wrote:
 My company provides some services online, which now they are planning
 to make it offline and sell to customers who can use it in their
 networks.
 
 One of our major moneywinners is some data which is stored in a
 database. Now, this data inside the database was obtained after paying
 through the nose - so the company does not want to disclose the data
 in the DB to the outside world - not to mention the lawsuits which the
 original providers of data will start which will sink the company if
 the data goes out.
 
 Now, the code is in Python - and we have a big problem. How to secure
 the data in DB? One idea was to encrypt it and store the password in
 the code. I dont believe security through obscurity - and python code
 can easily be reverse-engineered too - right?
 
 Is it even possible to secure a data in this case?

That depends a lot on what you call secure. The data will have
to get processed by the CPU one way or another and there are lots
of ways to monitor such operations, either through software (debuggers,
loggers, etc.) or hardware (low-level debuggers, signal analyzers, etc.).

The best you can do is make it just a little harder to get at
the data, ie. implement a simple but non-trivial data protection
mechanism, and then use legal means to protect yourself from
any wrong-doing of your customers.

One way to do this, is by encrypting the data for the database
and decrypting it whenever you start the application. If you
use an in-memory database for the application, this will provide
such a simple but non-trivial data protection scheme.

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Jan 25 2009)
 Python/Zope Consulting and Support ...http://www.egenix.com/
 mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
 mxODBC, mxDateTime, mxTextTools ...http://python.egenix.com/


::: Try our new mxODBC.Connect Python Database Interface for free ! 


   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
   Registered at Amtsgericht Duesseldorf: HRB 46611
   http://www.egenix.com/company/contact/
--
http://mail.python.org/mailman/listinfo/python-list


Re: Byte oriented data types in python

2009-01-25 Thread Grant Edwards
On 2009-01-25, Martin v. Löwis mar...@v.loewis.de wrote:

 You construct a format string for the value portion based on
 the type/length header.

 Can you kindly provide example code on how to do this?

OK, something like this to handle received data where there is
an initial 8-bit type field that is 1 for 16-bit unsigned
integers in network byte-order, 2 for 32-bit IEEE floats in
network byte-order.  We'll further assume that the 'length'
field comes next as a 16 bit unsigned value in network order
and represents how many objects of the specified type follow:

  dtype = ord(rawdata[0])
  dcount = struct.unpack(!H,rawdata[1:3])
  if dtype == 1:
 fmtstr = ! + H*dcount
  elif dtype == 2:
 fmtstr = ! + f*dcount
  rlen = struct.calcsize(fmtstr)
  
  data = struct.unpack(fmtstr,rawdata[3:3+rlen])
  
  leftover = rawdata[3+rlen:]

 I don't see how that can be the case.  There may not be a
 single C struct that can represent all frames, but for every
 frame you should be able to come up with a C struct that can
 represent that frame.

 Sure. You would normally have a struct such as

 struct TLV{
   char type;
   char length;
   char *data;
 };

 However, the in-memory representation of that struct is *not*
 meant to be sent over the wire. In particular, the character
 pointer has no meaning outside the address space, and is thus
 not to be sent.

Well if it's not representing the layout of the data we're
trying to deal with, then it's irrelevent.  We are talking
about how convert python objects to/from data in the
'on-the-wire' format, right?

Or isn't that what the OP is asking about?

 Both.  For varible size/format stuff you decode the first few
 bytes and use them to figure out what format/layout to use for
 the next chunk of data.  It's pretty much the same thing you do
 in other languages.

 In the example he gave, I would just avoid using the struct module
 entirely, as it does not provide any additional value:

 def encode(type, length, value):
   return chr(type)+chr(length)+value

Like this?

 def encode(type,length,value):
...  return chr(type)+chr(length)+value
... 
 print encode('float', 1, 3.14159)
Traceback (most recent call last):
  File stdin, line 1, in module
  File stdin, line 2, in encode
TypeError: an integer is required
 

-- 
Grant

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


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

2009-01-25 Thread Дамјан Георгиевски
 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.


-- 
дамјан ( http://softver.org.mk/damjan/ )

A: Because it reverses the logical flow of converstion.
Q: Why is top posting frowned upon?

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


Re: Cartesian Product of two lists (itertools)

2009-01-25 Thread Mensanator
On Jan 25, 3:12�pm, Thorsten Kampe thors...@thorstenkampe.de wrote:
 Hi,

 is there a way to make itertools.product generate triples instead of
 pairs from two lists?

 For example: list1 = [1, 2]; list2 = [4, 5]; list3 = [7, 8]
  from itertools import product
  list(product(list1, list2, list3))

 [(1, 4, 7), (1, 4, 8), (1, 5, 7), (1, 5, 8), (2, 4, 7), (2, 4, 8), (2,
 5, 7), (2, 5, 8)]

 so far so good... Now... list(product(product(list1, list2), list3))

 [((1, 4), 7), ((1, 4), 8), ((1, 5), 7), ((1, 5), 8), ((2, 4), 7), ((2,
 4), 8), ((2, 5), 7), ((2, 5), 8)]

 Oops, pairs of pairs instead triples. Not what I wanted.

 What's the best way to pre-process the arguments to itertools.product
 or to post-process the result of itertools.product to get what I
 want?!

 I have an older utility which I would like to replace with
 itertools.product. The old one uses a rather clumsy way to indicate that
 a triple was wanted:

 def cartes(seq0, seq1, modus = 'pair'):
 � �  return the Cartesian Product of two sequences 
 � � if � modus == 'pair':
 � � � � return [[item0, item1] for item0 in seq0 for item1 in seq1]
 � � elif modus == 'triple':
 � � � � return [item0 + [item1] for item0 in seq0 for item1 in seq1]

 Thorsten

Will this work for you?

 list4 = [(i,) for i in list3]

 list4
[(7,), (8,)]

 a = list(itertools.product(itertools.product(list1, list2), list4))
 a
[((1, 4), (7,)), ((1, 4), (8,)), ((1, 5), (7,)), ((1, 5), (8,)), ((2,
4), (7,)), ((2, 4), (8,)), ((2, 5), (7,)), ((2, 5), (8,))]

 def flatten(listOfLists):
return tuple(itertools.chain.from_iterable(listOfLists))

 list5 = [flatten(i) for i in a]
 list5
[(1, 4, 7), (1, 4, 8), (1, 5, 7), (1, 5, 8), (2, 4, 7), (2, 4, 8), (2,
5, 7), (2, 5, 8)]

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


super behavior

2009-01-25 Thread TP
Hi,

Hereafter is an example using super.
At the execution, we obtain:

coucou
init_coucou2
coucou1
coucou2
Traceback (most recent call last):
  File essai_heritage.py, line 34, in module
print b.a
AttributeError: 'coucou' object has no attribute 'a'

Why Python does not enter in the __init__ method of coucou1?
If I replace the two lines using super by the two following lines, it
works perfectly:

coucou1.__init__( self )
coucou2.__init__( self )

##
class coucou1( object ):

def __init__( self
, a = 1 ):
self.a = a
print init_coucou1

def print_coucou1( self ):
print coucou1


class coucou2( object ):

def __init__( self
, b = 2 ):
self.b = b
print init_coucou2

def print_coucou2( self ):
print coucou2


class coucou( coucou1, coucou2 ):

def __init__( self ):

print coucou
super( coucou1, self ).__init__( )
super( coucou2, self ).__init__( )

b = coucou()
b.print_coucou1()
b.print_coucou2()
print b.a
print b.b
##
-- 
python -c print ''.join([chr(154 - ord(c)) for c in '*9(9(18%.\
91+,\'Z4(55l4('])

When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong. (first law of AC Clarke)
--
http://mail.python.org/mailman/listinfo/python-list


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

2009-01-25 Thread gert
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 ?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Byte oriented data types in python

2009-01-25 Thread Martin v. Löwis
   dtype = ord(rawdata[0])
   dcount = struct.unpack(!H,rawdata[1:3])
   if dtype == 1:
  fmtstr = ! + H*dcount
   elif dtype == 2:
  fmtstr = ! + f*dcount
   rlen = struct.calcsize(fmtstr)
   
   data = struct.unpack(fmtstr,rawdata[3:3+rlen])
   
   leftover = rawdata[3+rlen:]

Unfortunately, that does not work in the example. We have
a message type (an integer), and a variable-length string.
So how do you compute the struct format for that?

 Sure. You would normally have a struct such as

 struct TLV{
   char type;
   char length;
   char *data;
 };

 However, the in-memory representation of that struct is *not*
 meant to be sent over the wire. In particular, the character
 pointer has no meaning outside the address space, and is thus
 not to be sent.
 
 Well if it's not representing the layout of the data we're
 trying to deal with, then it's irrelevent.  We are talking
 about how convert python objects to/from data in the
 'on-the-wire' format, right?

Right: ON-THE-WIRE, not IN MEMORY. In memory, there is a
pointer. On the wire, there are no pointers.

 Like this?
 
 def encode(type,length,value):
 ...  return chr(type)+chr(length)+value
 ... 
 print encode('float', 1, 3.14159)
 Traceback (most recent call last):
   File stdin, line 1, in module
   File stdin, line 2, in encode
 TypeError: an integer is required

No:

py CONNECT_REQUEST=17
py payload=call me
py encode(CONNECT_REQUEST, len(payload), payload)
'\x11\x07call me'

Regards,
Martin

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


Re: Counting number of objects

2009-01-25 Thread Gabriel Genellina
En Sun, 25 Jan 2009 16:06:47 -0200, Andreas Waldenburger  
geekm...@usenot.de escribió:



On Sun, 25 Jan 2009 09:23:35 -0800 (PST) Kottiyath
n.kottiy...@gmail.com wrote:

I am creating a class called people - subclasses men, women, children  
etc.

I want to count the number of people at any time.
So, I created code like the following:

class a(object):
counter = 0
def __new__(cls, *args, **kwargs):
a.counter += 1
return object.__new__(cls, *args, **kwargs)

def __del__(self):
a.counter -= 1

class aa(a):
pass


This looks OK, although I'd suggest using cls.counter += 1 instead of
a.counter += 1 in the __new__() method. Just seems clearer to me,
esp. when you think about subclassing. This would create an asymmetry
with __del__() then. Oh well. So maybe use self.__class__.counter -= 1
there, even if it is a bit ugly-ish.


Using self.__class__ is safer, from a technical point of view. When  
__del__ is executed at interpreter shutdown, a may not be available --  
in general, __del__ methods should not rely on any globals (one can  
inject names into the local namespace using default arguments).

See http://bugs.python.org/issue1717900

--
Gabriel Genellina

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


Re: Counting number of objects

2009-01-25 Thread Gabriel Genellina
En Sun, 25 Jan 2009 16:06:47 -0200, Andreas Waldenburger  
geekm...@usenot.de escribió:



On Sun, 25 Jan 2009 09:23:35 -0800 (PST) Kottiyath
n.kottiy...@gmail.com wrote:

I am creating a class called people - subclasses men, women, children  
etc.

I want to count the number of people at any time.
So, I created code like the following:

class a(object):
counter = 0
def __new__(cls, *args, **kwargs):
a.counter += 1
return object.__new__(cls, *args, **kwargs)

def __del__(self):
a.counter -= 1

class aa(a):
pass


This looks OK, although I'd suggest using cls.counter += 1 instead of
a.counter += 1 in the __new__() method. Just seems clearer to me,
esp. when you think about subclassing. This would create an asymmetry
with __del__() then. Oh well. So maybe use self.__class__.counter -= 1
there, even if it is a bit ugly-ish.


Using self.__class__ is safer, from a technical point of view. When  
__del__ is executed at interpreter shutdown, a may not be available --  
in general, __del__ methods should not rely on any globals (one can  
inject names into the local namespace using default arguments).

See http://bugs.python.org/issue1717900

--
Gabriel Genellina

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


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

2009-01-25 Thread Diez B. Roggisch

gert schrieb:

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'}


So what? That's python deciding to print strings using single-quotes. 
That has nothing to do with JSON.


The important part is this:

 json.dumps(json.loads('{test:test}'))
'{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 hard, but it's not standard-conform.

Most browsers even accept something like this:

{foo : bar}

But all of this is not JSON.

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


Re: syntax color lang source code in blogs or website

2009-01-25 Thread Cliff MacGillivray

Xah Lee wrote:

For those of you using emacs, here's the elisp code that allows you to
syntax color computer language source code in your blog or website.

http://xahlee.org/emacs/elisp_htmlize.html

to comment, here:
http://xahlee.blogspot.com/2009/01/dehtmlize-source-code-in-emacs-lisp.html

Xah,
Very nice!
If nothing else you seem to be a pretty clever programmer.
Indeed, you seem to understand more theoretical aspects than
most simple minded software developers.
I am not sure why you live out of your car?
Has that situation changed at all? Are you working right now?
--
http://mail.python.org/mailman/listinfo/python-list


Re: super behavior

2009-01-25 Thread Diez B. Roggisch

TP schrieb:

Hi,

Hereafter is an example using super.
At the execution, we obtain:

coucou
init_coucou2
coucou1
coucou2
Traceback (most recent call last):
  File essai_heritage.py, line 34, in module
print b.a
AttributeError: 'coucou' object has no attribute 'a'

Why Python does not enter in the __init__ method of coucou1?


Because you use super wrong. It's not supposed to be called with a 
superclass, but with the current class. And each class needs to call 
super itself in it's own __init__-method.


Like this:


class coucou1( object ):

def __init__( self
, a = 1 ):
self.a = a
print init_coucou1
super( coucou1, self ).__init__( )

def print_coucou1( self ):
print coucou1


class coucou2( object ):

def __init__( self
, b = 2 ):
self.b = b
print init_coucou2
super( coucou2, self ).__init__( )

def print_coucou2( self ):
print coucou2


class coucou( coucou1, coucou2 ):

def __init__( self ):
print coucou
super( coucou, self ).__init__( )

b = coucou()
b.print_coucou1()
b.print_coucou2()
print b.a
print b.b


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


Re: Byte oriented data types in python

2009-01-25 Thread Grant Edwards
On 2009-01-25, Martin v. Löwis mar...@v.loewis.de wrote:
   dtype = ord(rawdata[0])
   dcount = struct.unpack(!H,rawdata[1:3])
   if dtype == 1:
  fmtstr = ! + H*dcount
   elif dtype == 2:
  fmtstr = ! + f*dcount
   rlen = struct.calcsize(fmtstr)
   
   data = struct.unpack(fmtstr,rawdata[3:3+rlen])
   
   leftover = rawdata[3+rlen:]

 Unfortunately, that does not work in the example. We have
 a message type (an integer), and a variable-length string.
 So how do you compute the struct format for that?

I'm confused. Are you asking for an introductory tutorial on
programming in Python?

 Right: ON-THE-WIRE, not IN MEMORY. In memory, there is a
 pointer. On the wire, there are no pointers.

I don't understand your point.

 py CONNECT_REQUEST=17
 py payload=call me
 py encode(CONNECT_REQUEST, len(payload), payload)
 '\x11\x07call me'

If all your data is comprised of 8-bit bytes, then you don't
need the struct module.

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


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

2009-01-25 Thread gert
On Jan 25, 11:51 pm, Diez B. Roggisch de...@nospam.web.de wrote:
 gert schrieb:

  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'}

 So what? That's python deciding to print strings using single-quotes.
 That has nothing to do with JSON.

 The important part is this:

   json.dumps(json.loads('{test:test}'))
 '{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 hard, but it's not standard-conform.

 Most browsers even accept something like this:

 {foo : bar}

 But all of this is not JSON.

Yes it is, you just make it more python dictionary compatible :)
What is this json person email address so I can ask that he makes a
very small update on his site.

Besides if you can make lightweight versions of standards
http://docs.python.org/library/xml.dom.minidom.html

You can defenatly add lightweight quotes to json.

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


Re: syntax color lang source code in blogs or website

2009-01-25 Thread Tim Greer
Cliff MacGillivray wrote:

 Xah Lee wrote:
 For those of you using emacs, here's the elisp code that allows you
 to syntax color computer language source code in your blog or
 website.
 
 http:/
 
 to comment, here:
 http://...
 Xah,
 Very nice!
 If nothing else you seem to be a pretty clever programmer.
 Indeed, you seem to understand more theoretical aspects than
 most simple minded software developers.
 I am not sure why you live out of your car?
 Has that situation changed at all? Are you working right now?

Please don't encourage this guy.  He posts to groups purely to try and
tell everyone how smart he thinks he is (regardless of how wrong he
usually is), and argues and uses vulgar language to anyone that asks
him to stop posting in non relevent news groups.  He seems to have
selected the Python and Perl groups to regularly post to, even when his
posts rarely to never have any relation to said groups.  Please, do not
encourage his behavior.  Thanks.
-- 
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated  Semi-Dedicated servers
and Custom Hosting.  24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!
--
http://mail.python.org/mailman/listinfo/python-list


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

2009-01-25 Thread Tim Chase

One other caveat here, line contains the newline at the end, so
you might have

  print line.rstrip('\r\n')

to remove them.


I don't understand the presence of the '\r' there. Any '\x0d' that
remains after reading the file in text mode and is removed by that
rstrip would be a strange occurrence in the data which the OP may
prefer to find out about and deal with; it is not part of the
newline. Why suppress one particular data character in preference to
others?


In an ideal world where everybody knew how to make a proper 
text-file, it wouldn't be an issue.  Recreating the form of some 
of the data I get from customers/providers:


  f = file('tmp/x.txt', 'wb')
  f.write('headers\n')  # headers in Unix format
  f.write('data1\r\n')  # data in Dos format
  f.write('data2\r\n')
  f.write('data3')   # no trailing newline of any sort
  f.close()

Then reading it back in:

  for line in file('tmp/x.txt'): print repr(line)
 ...
 'headers\n'
 'data1\r\n'
 'data2\r\n'
 'data3'

As for wanting to know about stray '\r' characters, I only want 
the data -- I don't particularly like to be reminded of the 
incompetence of those who send me malformed text-files ;-)



The same applies in any case to the use of rstrip('\n'); if that finds
more than one ocurrence of '\x0a' to remove, it has exceeded the
mandate of removing the newline (if any).


I believe that using the formulaic for line in file(FILENAME) 
iteration guarantees that each line will have at most only one 
'\n' and it will be at the end (again, a malformed text-file with 
no terminal '\n' may cause it to be absent from the last line)



So, we are left with the unfortunately awkward
if line.endswith('\n'):
line = line[:-1]


You're welcome to it, but I'll stick with my more DWIM solution 
of get rid of anything that resembles an attempt at a CR/LF.


Thank goodness I haven't found any of my data-sources using 
\n\r instead, which would require me to left-strip '\r' 
characters as well.  Sigh.  My kingdom for competency. :-/


-tkc





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


Re: Byte oriented data types in python

2009-01-25 Thread Martin v. Löwis
 Unfortunately, that does not work in the example. We have
 a message type (an integer), and a variable-length string.
 So how do you compute the struct format for that?
 
 I'm confused. Are you asking for an introductory tutorial on
 programming in Python?

Perhaps. I honestly do not know how to deal with variable-sized
strings in the struct module in a reasonable way, and thus believe
that this module is incapable of actually supporting them
(unless you use inappropriate trickery).

However, as you keep claiming that the struct module is what
should be used, I must be missing something about the struct
module.

 I don't understand your point.
 
 py CONNECT_REQUEST=17
 py payload=call me
 py encode(CONNECT_REQUEST, len(payload), payload)
 '\x11\x07call me'
 
 If all your data is comprised of 8-bit bytes, then you don't
 need the struct module.

Go back to the original message of the OP. It says

#  I have following packet format which I have to send over Bluetooth.
# packet_type (1 byte unsigned) || packet_length (1 byte unsigned) ||
# packet_data(variable)

So yes, all his date is comprised of 8-bit bytes, and yes, he doesn't
need the struct module. Hence I'm puzzled why people suggest that
he uses the struct module.

I think the key answer is use the string type, it is appropriate
to represent byte oriented data in python (also see the subject
of this thread)

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


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

2009-01-25 Thread Diez B. Roggisch


But all of this is not JSON.


Yes it is, you just make it more python dictionary compatible :)


No, what you do is to make it more incompatible with other 
json-implementations. Which defies the meaning of a standard.


Besides, {foo : bar} is *not* python dictionary compatible, at least 
not unless you defined foo beforehand, and then there is no guarantee 
that foo is actually as string containing 'foo'.



What is this json person email address so I can ask that he makes a
very small update on his site.


Go try your luck - http://www.json.org/


Besides if you can make lightweight versions of standards
http://docs.python.org/library/xml.dom.minidom.html


minidom is a lightweight version of the DOM-API. But it reads and writes 
standard-conform XML documents.


The same applies for element-tree and lxml.

So it does not serve as a counter-example.


You can defenatly add lightweight quotes to json.


If you bring all other implementors of all other languages to 
simultaneously do so - yes, you can. Again, good luck with that.



Diez

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


Re: Byte oriented data types in python

2009-01-25 Thread Grant Edwards
On 2009-01-25, Martin v. Löwis mar...@v.loewis.de wrote:
 Unfortunately, that does not work in the example. We have
 a message type (an integer), and a variable-length string.
 So how do you compute the struct format for that?
 
 I'm confused. Are you asking for an introductory tutorial on
 programming in Python?

 Perhaps. I honestly do not know how to deal with variable-sized
 strings in the struct module in a reasonable way, and thus believe
 that this module is incapable of actually supporting them
 (unless you use inappropriate trickery).

It deals with variable sized fields just fine:

dtype = 18
dlength = 32
format = !BB%ds % dlength

rawdata = struct.pack(format, (dtype,dlength,data))

 However, as you keep claiming that the struct module is what
 should be used, I must be missing something about the struct
 module.

http://docs.python.org/library/struct.html

 I don't understand your point.
 
 py CONNECT_REQUEST=17
 py payload=call me
 py encode(CONNECT_REQUEST, len(payload), payload)
 '\x11\x07call me'
 
 If all your data is comprised of 8-bit bytes, then you don't
 need the struct module.

 Go back to the original message of the OP. It says

 #  I have following packet format which I have to send over Bluetooth.
 # packet_type (1 byte unsigned) || packet_length (1 byte unsigned) ||
 # packet_data(variable)

 So yes, all his date is comprised of 8-bit bytes,

He doesn't specify what format the packet_data is, and we all
assumed he needed to handle conversion of various data types
to/from raw byte-strings.

 and yes, he doesn't need the struct module. Hence I'm puzzled
 why people suggest that he uses the struct module.

We all assumed that packet_data might contain values of
various types such as 16 or 32 bit integers, floating point
values -- that packet_data was not solely arbitrary-length
strings of 8-bit bytes.

 I think the key answer is use the string type, it is
 appropriate to represent byte oriented data in python (also
 see the subject of this thread)

I, for one, interpreted byte-oriented to mean that the data
was received/sent as blocks of bytes but needed to be converted
into other data types.  If the data really is just strings of
bytes, and it's sent as strings of bytes, then I have no idea
what the OP was asking, since there's nothing that needs to be
done with the data.

-- 
Grant

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


Re: Byte oriented data types in python

2009-01-25 Thread Martin v. Löwis
 It deals with variable sized fields just fine:
 
 dtype = 18
 dlength = 32
 format = !BB%ds % dlength
 
 rawdata = struct.pack(format, (dtype,dlength,data))

I wouldn't call this just fine, though - it involves
a % operator to even compute the format string. IMO,
it is *much* better not to use the struct module for this
kind of problem, and instead rely on regular string
concatenation.

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


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

2009-01-25 Thread Andreas Waldenburger
On Sun, 25 Jan 2009 23:51:41 +0100 Diez B. Roggisch
de...@nospam.web.de wrote:

 gert schrieb:
  {'test': 'test'}
  {test: test}
  
  It can not be that hard to support both notation can it ?
 
 It's not hard, but it's not standard-conform.
 
OK, playing the devil's advocate here: Doesn't practicality beat purity?

/W

-- 
My real email address is constructed by swapping the domain with the
recipient (local part).
--
http://mail.python.org/mailman/listinfo/python-list


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

2009-01-25 Thread Gabriel Genellina

En Sun, 25 Jan 2009 21:08:04 -0200, gert gert.cuyk...@gmail.com escribió:

On Jan 25, 11:51 pm, Diez B. Roggisch de...@nospam.web.de wrote:

gert schrieb:

 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.
 It can not be that hard to support both notation can it ?
It's not hard, but it's not standard-conform.

Most browsers even accept something like this:

{foo : bar}

But all of this is not JSON.


Yes it is, you just make it more python dictionary compatible :)


What do you mean? The above is not valid Python.
JSON is whatever the author says it is. And he says A string is a  
collection of zero or more Unicode characters, wrapped in double quotes,  
using backslash escapes.



What is this json person email address so I can ask that he makes a
very small update on his site.


Try http://www.json.org/ -- good luck.


Besides if you can make lightweight versions of standards
http://docs.python.org/library/xml.dom.minidom.html


This is not a lightweight version of XML, but a lightweight version of an  
API. minidom reads and writes the same valid XML documents.



You can defenatly add lightweight quotes to json.


JSON is ligthweight *already*: JSON (JavaScript Object Notation) is a  
lightweight data-interchange format.
Introducing single quoted strings, apart from being incompatible with the  
previous version, would make parsing more complex.


--
Gabriel Genellina

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


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

2009-01-25 Thread Steve Holden
Andreas Waldenburger wrote:
 On Sun, 25 Jan 2009 23:51:41 +0100 Diez B. Roggisch
 de...@nospam.web.de wrote:
 
 gert schrieb:
 {'test': 'test'}
 {test: test}

 It can not be that hard to support both notation can it ?
 It's not hard, but it's not standard-conform.

 OK, playing the devil's advocate here: Doesn't practicality beat purity?
 
It's not practical to expect a standard to be rewritten to conform with
the ideas of one individual, as well as all the implementations of that
standard.

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Re: Byte oriented data types in python

2009-01-25 Thread Grant Edwards
On 2009-01-25, Martin v. Löwis mar...@v.loewis.de wrote:
 It deals with variable sized fields just fine:
 
 dtype = 18
 dlength = 32
 format = !BB%ds % dlength
 
 rawdata = struct.pack(format, (dtype,dlength,data))

 I wouldn't call this just fine, though - it involves
 a % operator to even compute the format string. IMO,
 it is *much* better not to use the struct module for this
 kind of problem, and instead rely on regular string
 concatenation.

If all you need to do is concatenate strings, then you're
correct, there's no advantage to using struct or ctypes.

If you need a generic way to deal with arbitrary data types,
then that's what the struct and ctypes modules are designed to
do.  The protocols I've implemented always required the ability
to deal with integers greater than 8 bits wide as well as
various other data types.

-- 
Grant

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


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

2009-01-25 Thread gert
On Jan 26, 12:40 am, Diez B. Roggisch de...@nospam.web.de wrote:
  But all of this is not JSON.

  Yes it is, you just make it more python dictionary compatible :)

 No, what you do is to make it more incompatible with other
 json-implementations. Which defies the meaning of a standard.

 Besides, {foo : bar} is *not* python dictionary compatible, at least
 not unless you defined foo beforehand, and then there is no guarantee
 that foo is actually as string containing 'foo'.

  What is this json person email address so I can ask that he makes a
  very small update on his site.

 Go try your luck -http://www.json.org/

  Besides if you can make lightweight versions of standards
 http://docs.python.org/library/xml.dom.minidom.html

 minidom is a lightweight version of the DOM-API. But it reads and writes
 standard-conform XML documents.

 The same applies for element-tree and lxml.

 So it does not serve as a counter-example.

yes it does because adding ' does not mean replacing  so it will
still load standard json. Like every browser does and is exactly the
same philosofie as

http://docs.python.org/library/xml.dom.minidom.html

The xml.dom.minidom module is essentially a DOM 1.0-compatible DOM
with some DOM 2 features (primarily namespace features).

or

unlink() is a xml.dom.minidom-specific extension to the DOM API. After
calling unlink() on a node, the node and its descendants are
essentially useless.







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


ob_type in shared memory

2009-01-25 Thread Aaron Brady
Hello,

I am writing an extension using shared memory.  I need a data type
that is able to reassign its 'ob_type' field depending on what process
is calling it.

Object 'A' is of type 'Ta'.  When process 'P' is looking at it, it
needs to have an 'ob_type' that is 'Ta' as process 'P' sees it.  When
process 'Q' is looking at it, it needs to have an 'ob_type' that is
'Ta' as process 'Q' sees it.  If it referred to 'Ta' in process 'P'
when 'Q' was calling it, 'Q' would have to access memory that is in
another process.

Therefore, I need a field and an array.  The field indicates which
type should be loaded, and the array contains the types.  Quick
example:

PyTypeObject* array_of_types[]= { SharedList, SharedTuple };

Then, when a list is being accessed, it can set its own 'ob_type'
field to 'array_of_types[ 0 ]', and similarly for a tuple.

However, I'm having trouble getting 'array_of_types' in the right
module during compilation.  My question is: Where do 'array_of_types'
and the forward declarations for the types go?

My fallback is what 'pickle' does: store types as strings, then load
them dynamically for 'ob_type'.  That is, obtain a pointer to the type
from the string.

Thank you for reading!  Any ideas?  Thanks in advance.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python 3: range objects cannot be sliced

2009-01-25 Thread Fuzzyman
On Jan 25, 2:28 pm, Alan G Isaac alan.is...@gmail.com wrote:
 On 1/16/2009 3:13 PM Alan G Isaac apparently wrote:
   It is documented:
  http://docs.python.org/3.0/library/stdtypes.html#sequence-types-str-b...

 But then again, the opposite is also documented,
 since `range` is a sequence type.  Quoting:

      Sequences also support slicing ...

      Some sequences also support “extended slicing”

 Is this a documentation bug, or a bug in `range`?
 (I'd think the latter.)

 Cheers,
 Alan Isaac

Where does the documentation say that range objects are sequences?
They're iterables.

Michael Foord
--
http://www.ironpythoninaction.com/
--
http://mail.python.org/mailman/listinfo/python-list


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

2009-01-25 Thread John Machin

On 26/01/2009 10:34 AM, Tim Chase wrote:

I believe that using the formulaic for line in file(FILENAME) 
iteration guarantees that each line will have at most only one '\n' 
and it will be at the end (again, a malformed text-file with no terminal 
'\n' may cause it to be absent from the last line)


It seems that you are right -- not that I can find such a guarantee 
written anywhere. I had armchair-philosophised that writing 
foo\n\r\nbar\r\n to a file in binary mode and reading it on Windows in 
text mode would be strict and report the first line as foo\n\n; I was 
wrong.





So, we are left with the unfortunately awkward
if line.endswith('\n'):
line = line[:-1]


You're welcome to it, but I'll stick with my more DWIM solution of get 
rid of anything that resembles an attempt at a CR/LF.


Thanks, but I don't want it. My point was that you didn't TTOPEWYM (tell 
the OP exactly what you meant).


My approach to DWIM with data is, given
   norm_space = lambda s: u' '.join(s.split())
to break up the line into fields first (just in case the field delimiter 
== '\t') then apply norm_space to each field. This gets rid of your '\r' 
at end (or start!) of line, and multiple whitespace characters are 
replaced by a single space. Whitespace includes NBSP (U+00A0) as an 
added bonus for being righteous and using Unicode :-)


Thank goodness I haven't found any of my data-sources using \n\r 
instead, which would require me to left-strip '\r' characters as well.  
Sigh.  My kingdom for competency. :-/


Indeed. I actually got data in that format once from a *x programmer who 
was so kind as to do it that way just for me because he knew that I use 
Windows and he thought that's what Windows text files looked like. No 
kidding.


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


Re: Efficient multi-slicing technique?

2009-01-25 Thread Tim Chase

Is there an efficient way to multi-slice a fixed with string
into individual fields that's logically equivalent to the way
one would slice a delimited string using .split()? Background:
I'm parsing some very large, fixed line-width text files that
have weekly columns of data (52 data columns plus related
data). My current strategy is to loop through a list of 
slice()'s to build a list of the specific field values for

each line. This is fine for small files, but seems
inefficient. I'm hoping that there's a built-in (C based)


I'm not sure if it's more efficient, but there's the struct 
module[1]:


  from struct import unpack
  for line in file('sample.txt'):
(num, a, b, c, nl) = unpack(2s9s7s4sc, line)
print num:, repr(num)
print a:, repr(a)
print b:, repr(b)
print c:, repr(c)

Adjust the formatting string for your data (the last c is the 
newline character -- you might be able to use x here to just 
ignore the byte so it doesn't get returned). The sample data I 
threw was 2/9/7/4 character data.  The general pattern would be


  lengths = [3,18,24,5,1,8]
  FORMAT_STR = (
''.join(%ss % length for length in lengths) +
'c')
  for line in file(INFILE):
(f1, f2,..., fn, _) = unpack(FORMAT_STR, line)


-tkc

[1]
http://docs.python.org/library/struct.html







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


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

2009-01-25 Thread Scott David Daniels

John Machin wrote:

On 26/01/2009 10:34 AM, Tim Chase wrote:

I believe that using the formulaic for line in file(FILENAME) 
iteration guarantees that each line will have at most only one '\n' 
and it will be at the end (again, a malformed text-file with no 
terminal '\n' may cause it to be absent from the last line)


It seems that you are right -- not that I can find such a guarantee 
written anywhere. I had armchair-philosophised that writing 
foo\n\r\nbar\r\n to a file in binary mode and reading it on Windows in 
text mode would be strict and report the first line as foo\n\n; I was 
wrong.


Here's how I'd do it:
with open('deheap/deheap.py', 'rU') as source:
for line in source:
print line.rstrip()  # Avoid trailing spaces as well.

This should handle \n, \r\n, and \n\r lines.

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


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

2009-01-25 Thread Andreas Waldenburger
On Sun, 25 Jan 2009 19:04:44 -0500 Steve Holden st...@holdenweb.com
wrote:

 Andreas Waldenburger wrote:
  On Sun, 25 Jan 2009 23:51:41 +0100 Diez B. Roggisch
  de...@nospam.web.de wrote:
  
  gert schrieb:
  {'test': 'test'}
  {test: test}
 
  It can not be that hard to support both notation can it ?
  It's not hard, but it's not standard-conform.
 
  OK, playing the devil's advocate here: Doesn't practicality beat
  purity?
  
 It's not practical to expect a standard to be rewritten to conform
 with the ideas of one individual, as well as all the implementations
 of that standard.
 
But as gert says, the standard is broken by many many browsers
already (I don't know if that's true, though; I just assume it is). Why
not make it compatible with, and as forgiving as, those?

(I feel a bit stupid here, because I'm basically on the adhere to the
standard side. I just noticed that the Zen (or what I make of it)
seems to suggest otherwise.)

regards,
/W

-- 
My real email address is constructed by swapping the domain with the
recipient (local part).
--
http://mail.python.org/mailman/listinfo/python-list


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

2009-01-25 Thread Steven D'Aprano
On Sun, 25 Jan 2009 17:34:18 -0600, Tim Chase wrote:

 Thank goodness I haven't found any of my data-sources using \n\r
 instead, which would require me to left-strip '\r' characters as well. 
 Sigh.  My kingdom for competency. :-/

If I recall correctly, one of the accounting systems I used eight years 
ago gave you the option of exporting text files with either \r\n or \n\r 
as the end-of-line mark. Neither \n nor \r (POSIX or classic Mac) line 
endings were supported, as that would have been useful.

(It may have been Arrow Accounting, but don't quote me on that.)

I can only imagine the developer couldn't remember which order the 
characters were supposed to go, so rather than look it up, he made it 
optional.



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


  1   2   3   >