Re: deque and thread-safety

2012-10-12 Thread Dieter Maurer
Christophe Vandeplas  writes:

> ...
> From the documentation I understand that deques are thread-safe:
>> Deques are a generalization of stacks and queues (the name is pronounced 
>> “deck”
>> and is short for “double-ended queue”). Deques support thread-safe, memory
>> efficient appends and pops from either side of the deque with approximately 
>> the
>> same O(1) performance in either direction.
>
> It seems that appending to deques is indeed thread-safe, but not
> iterating over them.

You are right.

And when you think about it, then there is not much point in striving
for thread safety for iteration (alone).
Iteration is (by nature) a non atomic operation: you iterate because
you want to do something with the intermediate results; this "doing"
is not part of the iteration itself.
Thus, you are looking for thread safety not for only the iteration
but for the iteration combined with additional operations (which
may well extend beyond the duration of the iteration).

Almost surely, the "deque" implementation is using locks
to ensure thread safety for its "append" and "pop". Check whether
this lock is exposed to the application. In this case, use
it to protect you atomic sections involving iteration.

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


Re: serialization and versioning

2012-10-12 Thread Dieter Maurer
Neal Becker  writes:

> I wonder if there is a recommended approach to handle this issue.
>
> Suppose objects of a class C are serialized using python standard pickling.  
> Later, suppose class C is changed, perhaps by adding a data member and a new 
> constructor argument.
>
> It would see the pickling protocol does not directly provide for this - but 
> is 
> there a recommended method?
>
> I could imagine that a class could include a class __version__ property that 
> might be useful - although I would further expect that it would not have been 
> defined in the original version of class C (but only as an afterthought when 
> it 
> became necessary).

The ZODB (Zope Object DataBase) is based on Python's pickle.

In the ZODB world, the following strategy is used:

  *  if the class adds a new data attribute, give it (in addition) a
 corresponding class level attribute acting as "default" value
 in case the pickled state of an instance lacks this
 instance level attribute

  *  for more difficult cases, define an appropriate "__getstate__"
 for the class that handles the necessary model upgrades

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


Feedback on my python framework I'm building.

2012-10-12 Thread nbvfour
http://giotto.readthedocs.org/en/latest/tutorial.html

Can someone give me some feedback on what they think of this framework? I came 
up with the idea of this framework a few months ago. I gave a talk at a local 
python user group regarding these ideas, but no one seemed to think I was onto 
anything special or useful.

Basically its a framework that forces the developer(s) to strictly separate the 
model from the view and controller. You can 'hook up' multiple controllers to a 
project. The model layer can be completely mocked out so front end designers 
don't have to bother setting up Postgres/rabbitmq/whatever.

Does anyone have any throughts or feedback?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Basic JSON question: Do I really need the quotes

2012-10-12 Thread rusi
On Oct 13, 5:03 am, Roy Smith  wrote:
> In article ,
>
>  moo...@yahoo.co.uk wrote:
> > I need to define some configuration in a file that will be manually created.
> > [...]
> > json seemed a quick an easy way of achieving this
>
> JSON would not be my first choice for a file which needs to be
> maintained by hand.
>
> I've only recently started using a system that has YAML config files.
> I've quickly become enamored of the format for config files.  I don't
> know if it's capable of expressing everything you can with JSON, but it
> certainly can do anything you would reasonably want to put in a config
> file, it's easy to read, and easy to hand-edit.

Yaml is a superset of json http://en.wikipedia.org/wiki/YAML#JSON

I find it a bit mysterious: yaml's structure-via-indentation
philosophy makes it more in line with python than most other modern
languages. And yet its the ruby community that seems to most eagerly
embrace yaml. Specially ironic given that ruby's syntax is reminiscent
of Pascal -- statements dont just close with '}' but with 'end'
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to use "while" within the command in -c option of python?

2012-10-12 Thread Ned Deily
In article 
,
 Herman  wrote:
> I was just trying to do in a shell to quickly monitor a file. Imagine
> instead of printing hello, it is "os.system("cat somefile")", etc.
> Look like it works if i press an enter after the "import xxx". Thanks.

If you are using a POSIX-compatible shell, the canonical approach for 
use cases like this is to use a "here document", for example:

python - 

Re: How to use "while" within the command in -c option of python?

2012-10-12 Thread Herman
I was just trying to do in a shell to quickly monitor a file. Imagine
instead of printing hello, it is "os.system("cat somefile")", etc.
Look like it works if i press an enter after the "import xxx". Thanks.

On Fri, Oct 12, 2012 at 5:29 PM, Dave Angel  wrote:
> On 10/12/2012 06:51 PM, Herman wrote:
>>  python -c "import os; while True: print('hello')"
>>  File "", line 1
>>  import os; while True: print('hello')
>>   ^
>> SyntaxError: invalid syntax
> See the recent thread:
>"for-loop on cmd-line"
>
> The problem has nothing to do with the command line, it's caused by
> trying to use a keyword 'while' somewhere other than the beginning of
> the statement.
>
> I'll ask you the same question I had:  why do you bother?  What's wrong
> with making a separate file for the source code?
>
> But as for solutions, first question is what OS you're running on.  If
> not Windows, you can probably use \n at appropriate points.
>
> If that's not good enough, what about simply running the script instead
> of a batch file?
>
> If that's not good enough, how about merging the two languages, with a
> trick like starting the python code with rem = """  followed by the
> shell script?
>
> There are many others, but we cannot choose without knowing your other
> constraints.
>
>
>
> --
>
> DaveA
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to use "while" within the command in -c option of python?

2012-10-12 Thread Steven D'Aprano
On Fri, 12 Oct 2012 19:04:20 -0400, Etienne Robillard wrote:

> On Fri, 12 Oct 2012 15:51:19 -0700
> Herman  wrote:
> 
>>  python -c "import os; while True: print('hello')" File "",
>>  line 1
>>  import os; while True: print('hello')
>>   ^
>> SyntaxError: invalid syntax
>> --
>> http://mail.python.org/mailman/listinfo/python-list
> 
> You get a syntax error since you didn't used tabs indents in your string
> which is normal for python AFAIK.

Incorrect. Python lets you use either spaces or tabs for indents, and in 
this case the SyntaxError is BEFORE the missing indent. He gets 
SyntaxError because you can't follow a semicolon with a statement that 
begins a block. It's the `while` that causes the error, not the lack of 
indent. Here's an example with `if` and an indented block that also 
fails:


[steve@ando ~]$ python -c "pass; pass"
[steve@ando ~]$ python -c "pass; if True:pass"
  File "", line 1
pass; if True:   pass
   ^
SyntaxError: invalid syntax


Solution: don't use semi-colons, use newlines.

In this example, I use `Ctrl-Q ENTER` to insert a literal newline in the 
string, which shows as ^M. Note that you *cannot* just type ^ M (caret 
M), that won't work, it must be an actual control character.


[steve@ando ~]$ python -c 'import time^Mwhile True:^Mprint("hello"); 
time.sleep(2)'
hello
hello
hello
Traceback (most recent call last):
  File "", line 3, in 
KeyboardInterrupt


In this example I just hit the Enter key to insert a newline, and let the 
shell deal with it. The ">" marks are part of the shell's prompt.

[steve@ando ~]$ python -c "import time
> while True:
> print('hello'); time.sleep(2)"
hello
hello
hello
Traceback (most recent call last):
  File "", line 3, in 
KeyboardInterrupt




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


Re: How to use "while" within the command in -c option of python?

2012-10-12 Thread Dave Angel
On 10/12/2012 06:51 PM, Herman wrote:
>  python -c "import os; while True: print('hello')"
>  File "", line 1
>  import os; while True: print('hello')
>   ^
> SyntaxError: invalid syntax
See the recent thread:
   "for-loop on cmd-line"

The problem has nothing to do with the command line, it's caused by
trying to use a keyword 'while' somewhere other than the beginning of
the statement.

I'll ask you the same question I had:  why do you bother?  What's wrong
with making a separate file for the source code?

But as for solutions, first question is what OS you're running on.  If
not Windows, you can probably use \n at appropriate points.

If that's not good enough, what about simply running the script instead
of a batch file?

If that's not good enough, how about merging the two languages, with a
trick like starting the python code with rem = """  followed by the
shell script?

There are many others, but we cannot choose without knowing your other
constraints.



-- 

DaveA

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


New Taylor & Francis Int. Journal “CMBBE: Imaging & Visualization”: Announcement and CfP

2012-10-12 Thread tava...@fe.up.pt
-

Announcement and Call for Contributions

New International Journal: Computer Methods in Biomechanics and Biomedical 
Engineering: Imaging & Visualization

Publisher: Taylor & Francis Group, ISSN: 2168-1163 (print) - 2168-1171 (online)

Editor-in-Chief: Prof. João Manuel R. S. Tavares - University of Porto, Portugal

Journal webpage: http://www.tandfonline.com/tciv

Journal in Facebook: http://www.facebook.com/Cmbbe.Image.Visualization

-

Dear Colleague,

I would like to introduce you the new international journal “Computer Methods 
in Biomechanics and Biomedical Engineering: Imaging & Visualization” (CMBBE: 
Imaging & Visualization) to be published by Taylor & Francis Group (T&F).
Your contribution will be welcome.

My best regards,

João Manuel R. S. Tavares
(Editor-in-chief of CMBBE: Imaging & Visualization)


AIMS & SCOPE

Computer Methods in Biomechanics and Biomedical Engineering: Imaging & 
Visualization is an international journal whose main goals are to promote 
solutions of excellence for both imaging and visualization of biomedical data, 
and establish links among researchers, clinicians, the medical technology 
sector and end-users.
The journal provides a comprehensive forum for discussion of the current 
state-of-the-art in the scientific fields related to imaging and visualization, 
including, but not limited to:

- Applications of Imaging and Visualization
- Computational Bio- imaging and Visualization
- Computer Aided Diagnosis, Surgery, Therapy and Treatment
- Data Processing and Analysis
- Devices for Imaging and Visualization
- Grid and High Performance Computing for Imaging and Visualization
- Human Perception in Imaging and Visualization
- Image Processing and Analysis
- Image-based Geometric Modelling
- Imaging and Visualization in Biomechanics
- Imaging and Visualization in Biomedical Engineering
- Medical Clinics
- Medical Imaging and Visualization
- Multi-modal Imaging and Visualization
- Multiscale Imaging and Visualization
- Scientific Visualization
- Software Development for Imaging and Visualization
- Telemedicine Systems and Applications
- Virtual Reality
- Visual Data Mining and Knowledge Discovery

The journal welcomes contributions covering theories, methodologies, devices 
and applications of imaging and visualization and assures a fast publishing 
process of original research manuscripts, position manuscripts expressing 
stimulating viewpoints and philosophies, survey manuscripts, technical notes 
and short communications, in regular and special issues.


BOARDS

Advisory Board
- Chandrajit Bajaj, University of Texas at Austin, USA 
- Chris Johnson, University of Utah, USA
- Demetri Terzopoulos, University of California, Los Angeles, USA
- John Middleton, Cardiff University, UK
- Jos Vander Sloten, Katholieke Universiteit Leuven, Belgium 
- Shuo Li, University of Western Ontario, Canada
- Thomas J. R. Hughes, University of Texas at Austin, USA

Associated Editors
- USA and Canada: Yongjie Zhang, Carnegie Mellon University, USA
- Asia: Jun Zhao, Shanghai Jiao Tong University, China
- Europe: Daniela Iacoviello, Università degli Studi di Roma "La Sapienza", 
Italy
- Rest of the world: Zeyun Yu, University of Wisconsin at Milwaukee, USA

Editorial Board
- Alejandro Frangi, University of Sheffield, Sheffield, UK
- Alexandre Cunha, California Institute of Technology, USA 
- Alexandre X. Falcão, Universidade Estadual de Campinas, Brazil
- Bernard Gosselin, University of Mons, Belgium
- Christos E. Constantinou, Stanford University, USA
- Constantine Kotropoulos, Aristotle University of Thessaloniki, Greece 
- Daniel Cremers, Technische Universität München, Germany 
- David Steinman, University of Toronto, Canada
- Durval C. Costa, Fundação Champalimaud, Portugal
- Eduardo Soudah, International Center for Numerical Methods in Engineering, 
Spain
- Fatima L. S. Nunes, Universidade de São Paulo, Brazil
- Fiorella Sgallari, University of Bologna, Italy
- George Bebis, University of Nevada, USA
- Joachim Hornegger, Friedrich-Alexander University Erlangen-Nuremberg, Germany
- João Paulo Papa, Universidade Estadual Paulista, Brazil
- Jorge S. Marques, Instituto Superior Técnico, Portugal
- Khan M Iftekharuddin, Old Dominion University, USA
- Laurent Cohen, Universite Paris Dauphine, France
- Lionel Moisan, Université Paris Descartes, France
- Lorenzo Mannelli, University of Washington, USA
- M. Emre Celebi, Louisiana State University in Shreveport, USA
- Manuel González Hidalgo, Balearic Islands University, Spain
- Marc Thiriet, Universite Pierre et Marie Curie (Paris VI), France
- Mário Forjaz Secca, Universidade Nova de Lisboa, Portugal 
- Nicolai Petkov, University of Groningen, The Netherlands 
- Paola Lecca, University of Trento, Italy
- Paolo Di Giamberard

Re: Basic JSON question: Do I really need the quotes

2012-10-12 Thread Roy Smith
In article ,
 moo...@yahoo.co.uk wrote:

> I need to define some configuration in a file that will be manually created.
> [...]
> json seemed a quick an easy way of achieving this

JSON would not be my first choice for a file which needs to be 
maintained by hand.

I've only recently started using a system that has YAML config files.  
I've quickly become enamored of the format for config files.  I don't 
know if it's capable of expressing everything you can with JSON, but it 
certainly can do anything you would reasonably want to put in a config 
file, it's easy to read, and easy to hand-edit.

> The problem is that I don't want to make users have to type redundant 
> characters.

I think what you're saying is, "My users would prefer YAML over JSON" :-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to use "while" within the command in -c option of python?

2012-10-12 Thread Etienne Robillard
On Fri, 12 Oct 2012 15:51:19 -0700
Herman  wrote:

>  python -c "import os; while True: print('hello')"
>  File "", line 1
>  import os; while True: print('hello')
>   ^
> SyntaxError: invalid syntax
> -- 
> http://mail.python.org/mailman/listinfo/python-list

You get a syntax error since you didn't used tabs indents in your string
which is normal for python AFAIK.
-- 
http://mail.python.org/mailman/listinfo/python-list


How to use "while" within the command in -c option of python?

2012-10-12 Thread Herman
 python -c "import os; while True: print('hello')"
 File "", line 1
 import os; while True: print('hello')
  ^
SyntaxError: invalid syntax
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Basic JSON question: Do I really need the quotes

2012-10-12 Thread Adam Tauno Williams
On Fri, 2012-10-12 at 19:27 +0200, Roel Schroeven wrote:
> moo...@yahoo.co.uk schreef:
> > Hi,
> > I need to define some configuration in a file that will be manually created.
> > Internally, the data will be stored as a dict, which contains various 
> > properties related to a design
> > e.g. Design Name, dependencies, lists of files (and associated libraries).
> > json seemed a quick an easy way of achieving this
> > Ayway, in simple terms my question - if I know everything is a string, how 
> > can I omit the quotation marks?
> > The problem is that I don't want to make users have to type redundant 
> > characters.
> > Is it possible?
> Not in JSON. Maybe you could try YAML?

If you want a human-readable human-editable markup for data structures I
strongly encourage you to look at YAML.  JSON is not wetware friendly.

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


Re: Checking for dlls in ctypes

2012-10-12 Thread Wanderer
On Friday, October 12, 2012 12:29:02 PM UTC-4, Dave Angel wrote:
> On 10/12/2012 11:36 AM, Wanderer wrote:
> 
> > I'm trying to write some code that will load one of three dll depending on 
> > the one available. I've tried the code below, but it doesn't work. The try 
> > except doesn't catch the exception. Is there a way to do this?
> 
> >
> 
> > try:
> 
> > self.dll = windll.pvcam64
> 
> > except:
> 
> > print "No pvcam64"
> 
> > try:
> 
> > self.dll = windll.pvcam32
> 
> > except:
> 
> > print "No pvcam32"
> 
> > try:
> 
> > self.dll = windll.pvcam
> 
> > except:
> 
> > print "No pvcam"
> 
> > return
> 
> > else:
> 
> > print "installed pvcam"
> 
> > else:
> 
> > print "installed pvcam32"
> 
> > else:
> 
> > print "installed pvcam64"
> 
> >
> 
> 
> 
> I can't help you find the dll's, because I don't run Windows.  But I
> 
> could help you write a clearer question:
> 
> 
> 
> "doesn't work" is thoroughly useless for describing errors.  If you're
> 
> getting an exception, show us the full traceback.  That will show which
> 
> statement got the exception that wasn't caught.  Next question is which
> 
> of the dlls is missing.  Are you getting an exception because it's
> 
> missing or because of something more fundamental, like nesting exception
> 
> handlers?
> 
> 
> 
> Using bare excepts is almost never a good idea.  If it "works" you get
> 
> no clues what went wrong.  For example, a typo in source code can
> 
> trigger a bare exception, as can a user typing Ctrl-C.   So when you're
> 
> using bare excepts, you have robbed the user of any way to terminate the
> 
> program.
> 
> 
> 
> If I were you, I'd be writing a loop so there's only one try block.  Too
> 
> much duplicated code in the way you're doing it.
> 
> 
> 
> 
> 
> 
> 
> -- 
> 
> 
> 
> DaveA

Sorry. It was a WindowsError, but the code I posted now works for me and I 
can't reproduce the problem. I'll be more diligent in the future.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Checking for dlls in ctypes

2012-10-12 Thread Wanderer
On Friday, October 12, 2012 12:57:06 PM UTC-4, MRAB wrote:
> On 2012-10-12 16:36, Wanderer wrote:
> 
> > I'm trying to write some code that will load one of three dll depending on 
> > the one available. I've tried the code below, but it doesn't work. The try 
> > except doesn't catch the exception. Is there a way to do this?
> 
> >
> 
> >  try:
> 
> >  self.dll = windll.pvcam64
> 
> >  except:
> 
> >  print "No pvcam64"
> 
> >  try:
> 
> >  self.dll = windll.pvcam32
> 
> >  except:
> 
> >  print "No pvcam32"
> 
> >  try:
> 
> >  self.dll = windll.pvcam
> 
> >  except:
> 
> >  print "No pvcam"
> 
> >  return
> 
> >  else:
> 
> >  print "installed pvcam"
> 
> >  else:
> 
> >  print "installed pvcam32"
> 
> >  else:
> 
> >  print "installed pvcam64"
> 
> >
> 
> This works for me:
> 
> 
> 
>  for name in ("pvcam64", "pvcam32", "pvcam"):
> 
>  try:
> 
>  self.dll = getattr(windll, name)
> 
>  except OSError:
> 
>  print "No " + name
> 
>  else:
> 
>  print "Installed " + name
> 
>  return

Yes that works for me, too. Thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Basic JSON question: Do I really need the quotes

2012-10-12 Thread Roel Schroeven

moo...@yahoo.co.uk schreef:

Hi,
I need to define some configuration in a file that will be manually created.
Internally, the data will be stored as a dict, which contains various 
properties related to a design
e.g. Design Name, dependencies, lists of files (and associated libraries).
json seemed a quick an easy way of achieving this
Anyway, in simple terms my question - if I know everything is a string, how can 
I omit the quotation marks?

The problem is that I don't want to make users have to type redundant 
characters.
Is it possible?


Not in JSON. Maybe you could try YAML?

--
"Too often we hold fast to the cliches of our forebears. We subject all
facts to a prefabricated set of interpretations. Too often we enjoy the
comfort of opinion without the discomfort of thought."
-- John F Kennedy

r...@roelschroeven.net

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


Re: Insert item before each element of a list

2012-10-12 Thread Joshua Landau
On 9 October 2012 13:55, Peter Otten <__pete...@web.de> wrote:

> Duncan Booth wrote:
>
> > mooremath...@gmail.com wrote:
> >
> >> What's the best way to accomplish this?  Am I over-complicating it?
> >> My gut feeling is there is a better way than the following:
> >>
> > import itertools
> > x = [1, 2, 3]
> > y = list(itertools.chain.from_iterable(('insertme', x[i]) for i in
> > range(len(x y
> >> ['insertme', 1, 'insertme', 2, 'insertme', 3]
> >>
> >> I appreciate any and all feedback.
> >>
> >
> > Given the myriad of proposed solutions, I'm surprised nobody has
> suggested
> > good old list slicing:
>
> My post on gmane
>
> http://thread.gmane.org/gmane.comp.python.general/718940/focus=718947
>
> apparently didn't make it through to the list.
>
>  x = [1,2,3]
>  y = ['insertme']*(2*len(x))
>  y[1::2] = x
>  y
> > ['insertme', 1, 'insertme', 2, 'insertme', 3]
>
> An advantage of this approach -- it is usually much faster.
>

It did, at least for me. People often don't see my posts too, I've been
told. That said, sometimes people just skim threads so it may have been
that.

On topic, the best methods are either:

inserts = itertools.repeat("insert")

itertools.chain.from_iterable(zip(lst, inserts))


or the more explicit:

def interleave(*lsts):

interleaved_in_tuples = zip(*lsts)

return itertools.chain.from_iterable(interleaved_in_tuples)



inserts = itertools.repeat("insert")
> interleave(inserts, lst)


They explain what they want and they do it. Anyone who's interleaving
without zip needs to re-read the built-ins. The manual two-yields is good,
too, but it's much less flexible. If the speed benefits of slicing are
important but the scaling is not, you really shouldn't be using CPython.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Checking for dlls in ctypes

2012-10-12 Thread MRAB

On 2012-10-12 16:36, Wanderer wrote:

I'm trying to write some code that will load one of three dll depending on the 
one available. I've tried the code below, but it doesn't work. The try except 
doesn't catch the exception. Is there a way to do this?

 try:
 self.dll = windll.pvcam64
 except:
 print "No pvcam64"
 try:
 self.dll = windll.pvcam32
 except:
 print "No pvcam32"
 try:
 self.dll = windll.pvcam
 except:
 print "No pvcam"
 return
 else:
 print "installed pvcam"
 else:
 print "installed pvcam32"
 else:
 print "installed pvcam64"


This works for me:

for name in ("pvcam64", "pvcam32", "pvcam"):
try:
self.dll = getattr(windll, name)
except OSError:
print "No " + name
else:
print "Installed " + name
return

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


Re: __setitem__ without position

2012-10-12 Thread Peter Otten
Ethan Furman wrote:

> Terry Reedy wrote:
>> In 3.x, you would write __setitem__ to recognize that the 'key' is a
>> slice object rather than an int and act accordingly. (In 2.x, you would
>> write __setslice__.)
> 
> I'm not sure how far back it goes, but at least from 2.4 forward
> __setitem__ works with slices just fine.

The __...slice__() methods are deprecated since 2.0.

http://docs.python.org/release/2.0/ref/sequence-methods.html

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


Re: Checking for dlls in ctypes

2012-10-12 Thread Dave Angel
On 10/12/2012 11:36 AM, Wanderer wrote:
> I'm trying to write some code that will load one of three dll depending on 
> the one available. I've tried the code below, but it doesn't work. The try 
> except doesn't catch the exception. Is there a way to do this?
>
> try:
> self.dll = windll.pvcam64
> except:
> print "No pvcam64"
> try:
> self.dll = windll.pvcam32
> except:
> print "No pvcam32"
> try:
> self.dll = windll.pvcam
> except:
> print "No pvcam"
> return
> else:
> print "installed pvcam"
> else:
> print "installed pvcam32"
> else:
> print "installed pvcam64"
>

I can't help you find the dll's, because I don't run Windows.  But I
could help you write a clearer question:

"doesn't work" is thoroughly useless for describing errors.  If you're
getting an exception, show us the full traceback.  That will show which
statement got the exception that wasn't caught.  Next question is which
of the dlls is missing.  Are you getting an exception because it's
missing or because of something more fundamental, like nesting exception
handlers?

Using bare excepts is almost never a good idea.  If it "works" you get
no clues what went wrong.  For example, a typo in source code can
trigger a bare exception, as can a user typing Ctrl-C.   So when you're
using bare excepts, you have robbed the user of any way to terminate the
program.

If I were you, I'd be writing a loop so there's only one try block.  Too
much duplicated code in the way you're doing it.



-- 

DaveA

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


Re: __setitem__ without position

2012-10-12 Thread Ethan Furman

Terry Reedy wrote:
In 3.x, you would write __setitem__ to recognize that the 'key' is a 
slice object rather than an int and act accordingly. (In 2.x, you would 
write __setslice__.) 


I'm not sure how far back it goes, but at least from 2.4 forward 
__setitem__ works with slices just fine.


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


Checking for dlls in ctypes

2012-10-12 Thread Wanderer
I'm trying to write some code that will load one of three dll depending on the 
one available. I've tried the code below, but it doesn't work. The try except 
doesn't catch the exception. Is there a way to do this?

try:
self.dll = windll.pvcam64
except:
print "No pvcam64"
try:
self.dll = windll.pvcam32
except:
print "No pvcam32"
try:
self.dll = windll.pvcam
except:
print "No pvcam"
return
else:
print "installed pvcam"
else:
print "installed pvcam32"
else:
print "installed pvcam64"

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


Re: Basic JSON question: Do I really need the quotes

2012-10-12 Thread Kwpolska
On Fri, Oct 12, 2012 at 4:09 PM,   wrote:
> Hi,
> I need to define some configuration in a file that will be manually created.
> Internally, the data will be stored as a dict, which contains various 
> properties related to a design
> e.g. Design Name, dependencies, lists of files (and associated libraries).
> json seemed a quick an easy way of achieving this
> Anyway, in simple terms my question - if I know everything is a string, how 
> can I omit the quotation marks?

Nope.  JSON has those rules for a reason.  You need to be specific.  A
more “human-friendly” format is the one used by ConfigParser (close to
INI, but not quite).

Also, JSON is supposed to be generated by computers, not humans.
-- 
Kwpolska 
stop html mail  | always bottom-post
www.asciiribbon.org | www.netmeister.org/news/learn2quote.html
GPG KEY: 5EAAEA16
-- 
http://mail.python.org/mailman/listinfo/python-list


Basic JSON question: Do I really need the quotes

2012-10-12 Thread moogyd
Hi,
I need to define some configuration in a file that will be manually created.
Internally, the data will be stored as a dict, which contains various 
properties related to a design
e.g. Design Name, dependencies, lists of files (and associated libraries).
json seemed a quick an easy way of achieving this
Anyway, in simple terms my question - if I know everything is a string, how can 
I omit the quotation marks?

i.e. I can do

>>> json.loads('{"mykey":["data0", "data1"]}')
{u'mykey': [u'data0', u'data1']}

But I would like to do
>>> json.loads('{mykey:[data0, data1]}')
Traceback (most recent call last):

The problem is that I don't want to make users have to type redundant 
characters.
Is it possible?
Thanks,
Steven





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


Re: deque and thread-safety

2012-10-12 Thread Antoine Pitrou

Hello,

Christophe Vandeplas  vandeplas.com> writes:
> 
> From the documentation I understand that deques are thread-safe:
> > Deques are a generalization of stacks and queues (the name is pronounced 
“deck”
> > and is short for “double-ended queue”). Deques support thread-safe, memory
> > efficient appends and pops from either side of the deque with approximately 
the
> > same O(1) performance in either direction.
> 
> It seems that appending to deques is indeed thread-safe, but not
> iterating over them.

Indeed, if you read the above sentence, deques only support "thread-safe [...] 
appends and pops". Other operations are not necessarily thread-safe.
Moreover, any operation which will call back into Python (such as iterating
several times) are *not* thread-safe.

Regardless, deques are not the right container for containment tests
("url in seen"). Like with lists or tuples, a containment test in a deque will 
be O(n). So if you want efficient containment tests, you should use a set or a 
dict.

Regards

Antoine.


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


Re: Posix call (execve) breaks mercurial?

2012-10-12 Thread Antoine Pitrou

Hello,

Wayne Werner  waynewerner.com> writes:
> 
> So... curiouser and curiouser - it looks like it's not *actually* execve's 
> fault after all. I just compiled the code from the man page, tweaked it to 
> run 'hg root', and passed it a new environment. No problems. Well, then I 
> manually called the posix one from Python and thing worked fine. *Then* I 
> actually tried the above code, and *it* worked fine.
> 
> However I *still* get problems with the post-review code. So it looks like 
> when I get back to work on Monday I'll be looking to see  what the 
> difference in environment is there.

Your problem is a user question for the Mercurial mailing-list, not a Python
problem. See http://selenic.com/mailman/listinfo/mercurial

Regards

Antoine.


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


Re: serialization and versioning

2012-10-12 Thread Robert Kern

On 10/12/12 11:42 AM, Neal Becker wrote:

I wonder if there is a recommended approach to handle this issue.

Suppose objects of a class C are serialized using python standard pickling.
Later, suppose class C is changed, perhaps by adding a data member and a new
constructor argument.

It would see the pickling protocol does not directly provide for this - but is
there a recommended method?

I could imagine that a class could include a class __version__ property that
might be useful - although I would further expect that it would not have been
defined in the original version of class C (but only as an afterthought when it
became necessary).


You don't need to add anything to the actual attributes on the class. Just 
define your __getstate__() dict to include a '__version__' item. You can treat 
the absence of '__version__' in the dict that __setstate__() receives as 
implying "version 0".


--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

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


Re: deque and thread-safety

2012-10-12 Thread Christophe Vandeplas
Ok sorry for the mail,
I found the solution by using deque.count(url) that's available
starting from python 2.7


On Fri, Oct 12, 2012 at 2:40 PM, Christophe Vandeplas
 wrote:
> Hello,
>
> I have a question about deque and thread-safety.
>
> My application has multiple threads running concurrently and doing the
> same action (downloading pages)
> To know what has already been downloaded I created the variable:
>   seen = deque('', 1000)   (keeps list of max 1000 urls in memory)
>
> In one place of the code I do:  seen.append(url)
>
> And another place:
> def seenPage()
>   if url in seen:
> return True
>   return False
>
> From the documentation I understand that deques are thread-safe:
>> Deques are a generalization of stacks and queues (the name is pronounced 
>> “deck”
>> and is short for “double-ended queue”). Deques support thread-safe, memory
>> efficient appends and pops from either side of the deque with approximately 
>> the
>> same O(1) performance in either direction.
>
> It seems that appending to deques is indeed thread-safe, but not
> iterating over them.
> I've seen a similar, but different, situation here:
> http://comments.gmane.org/gmane.comp.python.devel/85487
>
> Forgetting the above url, and considering my situation this behavior
> screws up the concept I need:
> - Keeping a thread-safe collection of seen urls,
> - Being able to check if something is in that collection
> - No need to clean the collection to prevent the memory from filling up
>
> So I know I could work around this problem by using a lock.
> But then I don't only need to use the lock around the iterator, but
> also around the append(), but that defeats the purpose of deque being
> thread-safe.
>
> In short, what's your advice:
>
> 1/ build a lock around the .append() and the iterator. Using the
> already-existing lock in the deque. But HOW?
>
> 1/ simply build a lock around the .append() and the iterator.
> Defeating the build-in thread-safety.
>
> 2/ use another collection that does what I need
>
>
> Thanks for your expertise.
>
> Christophe
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: serialization and versioning

2012-10-12 Thread Neal Becker
Etienne Robillard wrote:

> On Fri, 12 Oct 2012 06:42:03 -0400
> Neal Becker  wrote:
> 
>> I wonder if there is a recommended approach to handle this issue.
>> 
>> Suppose objects of a class C are serialized using python standard pickling.
>> Later, suppose class C is changed, perhaps by adding a data member and a new
>> constructor argument.
>> 
>> It would see the pickling protocol does not directly provide for this - but
>> is there a recommended method?
>> 
>> I could imagine that a class could include a class __version__ property that
>> might be useful - although I would further expect that it would not have been
>> defined in the original version of class C (but only as an afterthought when
>> it became necessary).
>> 
>> --
>> http://mail.python.org/mailman/listinfo/python-list
> 
> i guess a easy answer is to say to try python 3.3 but how would this translate
> in python (2) code ?

So are you saying python 3.3 has such a feature?  Where is it described?

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


deque and thread-safety

2012-10-12 Thread Christophe Vandeplas
Hello,

I have a question about deque and thread-safety.

My application has multiple threads running concurrently and doing the
same action (downloading pages)
To know what has already been downloaded I created the variable:
  seen = deque('', 1000)   (keeps list of max 1000 urls in memory)

In one place of the code I do:  seen.append(url)

And another place:
def seenPage()
  if url in seen:
return True
  return False

From the documentation I understand that deques are thread-safe:
> Deques are a generalization of stacks and queues (the name is pronounced 
> “deck”
> and is short for “double-ended queue”). Deques support thread-safe, memory
> efficient appends and pops from either side of the deque with approximately 
> the
> same O(1) performance in either direction.

It seems that appending to deques is indeed thread-safe, but not
iterating over them.
I've seen a similar, but different, situation here:
http://comments.gmane.org/gmane.comp.python.devel/85487

Forgetting the above url, and considering my situation this behavior
screws up the concept I need:
- Keeping a thread-safe collection of seen urls,
- Being able to check if something is in that collection
- No need to clean the collection to prevent the memory from filling up

So I know I could work around this problem by using a lock.
But then I don't only need to use the lock around the iterator, but
also around the append(), but that defeats the purpose of deque being
thread-safe.

In short, what's your advice:

1/ build a lock around the .append() and the iterator. Using the
already-existing lock in the deque. But HOW?

1/ simply build a lock around the .append() and the iterator.
Defeating the build-in thread-safety.

2/ use another collection that does what I need


Thanks for your expertise.

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


Re: serialization and versioning

2012-10-12 Thread Etienne Robillard
On Fri, 12 Oct 2012 06:42:03 -0400
Neal Becker  wrote:

> I wonder if there is a recommended approach to handle this issue.
> 
> Suppose objects of a class C are serialized using python standard pickling.  
> Later, suppose class C is changed, perhaps by adding a data member and a new 
> constructor argument.
> 
> It would see the pickling protocol does not directly provide for this - but 
> is 
> there a recommended method?
> 
> I could imagine that a class could include a class __version__ property that 
> might be useful - although I would further expect that it would not have been 
> defined in the original version of class C (but only as an afterthought when 
> it 
> became necessary).
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list

i guess a easy answer is to say to try python 3.3 but how would this translate 
in
python (2) code ?

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


Re: scipy and numpy for Python3

2012-10-12 Thread chip9munk
ok, i have solved it by the help from the previously mentioned page.

python 3.2 should be used, 3.3 has issues with building numpy and scipy.

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


serialization and versioning

2012-10-12 Thread Neal Becker
I wonder if there is a recommended approach to handle this issue.

Suppose objects of a class C are serialized using python standard pickling.  
Later, suppose class C is changed, perhaps by adding a data member and a new 
constructor argument.

It would see the pickling protocol does not directly provide for this - but is 
there a recommended method?

I could imagine that a class could include a class __version__ property that 
might be useful - although I would further expect that it would not have been 
defined in the original version of class C (but only as an afterthought when it 
became necessary).

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


scipy and numpy for Python3

2012-10-12 Thread chip9munk
Hello!

I am an absolute beginner in this.
Does anyone know hot to build scipy for python 3?

here: http://scipy.org/Installing_SciPy/Windows
it says that it was not yet tested... (?)

any help is appreciated!

any additional help for numpy is also welcome!

Thanx in advance!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: an error in python lib?

2012-10-12 Thread Ulrich Eckhardt

Am 12.10.2012 00:06, schrieb Wenhua Zhao:

On Wed, Oct 10, 2012 at 12:21 PM, Ian Kelly  wrote:

Can you demonstrate an API bug that is caused by this?


A simple demo of this error is:

[...]

 print "in main cv._is_owned: ", cv._is_owned()


That is kind of cheating, because as far as I can tell that function is 
private and not even documented in any way. You can use wait() though, 
which should always raise a RuntimeError:


--->8>8--

import time
from threading import Condition, Lock, Thread

cv = Condition(Lock())

def do_acquire():
cv.acquire()
print "cv acquired in thread"
time.sleep(5)
cv.release()
print "cv released in thread"

thread = Thread(target=do_acquire)
thread.start()

for i in range(10):
try:
cv.wait()
print "in main cv.wait() succeeded"
except RuntimeError:
print "in main cv.wait() raised RuntimeError"
time.sleep(1)

--->8>8--

This gives me the following output:

in main cv.wait() raised RuntimeErrorcv acquired in thread

Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Python27\lib\threading.py", line 551, in __bootstrap_inner
self.run()
  File "C:\Python27\lib\threading.py", line 504, in run
self.__target(*self.__args, **self.__kwargs)
  File "ttest.py", line 10, in do_acquire
cv.release()
error: release unlocked lock

Following that, the program hangs. It seems the wait() released the lock 
that it didn't own, causing the error in do_acquire(). It then hung in 
wait(), although it should have raised a RuntimeError instead.



Uli

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