ANN: eGenix pyOpenSSL Distribution 0.13.0-1.0.1c

2012-12-05 Thread eGenix Team: M.-A. Lemburg

ANNOUNCING

   eGenix.com pyOpenSSL Distribution

 Version 0.13.0-1.0.1c


 An easy-to-install and easy-to-use distribution
 of the pyOpenSSL Python interface for OpenSSL -
available for Windows, Mac OS X and Unix platforms


This announcement is also available on our web-site for online reading:
http://www.egenix.com/company/news/eGenix-pyOpenSSL-Distribution-0.13.0-1.0.1c-1.html


INTRODUCTION

The eGenix.com pyOpenSSL Distribution includes everything you need to
get started with SSL in Python.

It comes with an easy-to-use installer that includes the most recent
OpenSSL library versions in pre-compiled form, making your application
independent of OS provided OpenSSL libraries:

http://www.egenix.com/products/python/pyOpenSSL/

pyOpenSSL is an open-source Python add-on that allows writing SSL/TLS-
aware network applications as well as certificate management tools:

https://launchpad.net/pyopenssl/

OpenSSL is an open-source implementation of the SSL/TLS protocol:

http://www.openssl.org/


NEWS

This new release of the eGenix.com pyOpenSSL Distribution updates the
included OpenSSL version to 1.0.1c.

New features in OpenSSL 1.0.1c since 1.0.0j
---

OpenSSL 1.0.1c includes several new features:

 * TLS/DTLS heartbeat support

 * Next Protocol Negotiation

 * Support TLS v1.2 and TLS v1.1

as well as several other new features:

http://lwn.net/Articles/486426/

fixes vulnerabilities relative to 1.0.0j:

http://openssl.org/news/vulnerabilities.html

and includes a number of stability enhancements as well as extra
protection against attacks:

http://openssl.org/news/changelog.html

New features in the eGenix pyOpenSSL Distribution
-

 * Added the openssl binary to the OpenSSL package directory. This can
   be used to access OpenSSL functionality not exposed by pyOpenSSL.

 * Changed the Windows OPENSSLDIR default to c:\openssl\ to simplify
   OpenSSL configuration.

 * Fixed OpenSSL assembler build issues on Windows x64 and Mac OS X
   PPC/x86.

As always, we provide binaries that include both pyOpenSSL and the
necessary OpenSSL libraries for all supported platforms:
Windows x86 and x64, Linux x86 and x64, Mac OS X PPC, x86 and x64.

We've also added egg-file distribution versions of our eGenix.com
pyOpenSSL Distribution for Windows, Linux and Mac OS X to the
available download options. These make setups using e.g. zc.buildout
and other egg-file based installers a lot easier.


DOWNLOADS

The download archives and instructions for installing the package can
be found at:

http://www.egenix.com/products/python/pyOpenSSL/


UPGRADING

Before installing this version of pyOpenSSL, please make sure that
you uninstall any previously installed pyOpenSSL version. Otherwise,
you could end up not using the included OpenSSL libs.

___
SUPPORT

Commercial support for these packages is available from eGenix.com.
Please see

http://www.egenix.com/services/support/

for details about our support offerings.


MORE INFORMATION

For more information about the eGenix pyOpenSSL Distributon, licensing
and download instructions, please visit our web-site or write to
sa...@egenix.com.

Enjoy,
-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Dec 05 2012)
 Python Projects, Consulting and Support ...   http://www.egenix.com/
 mxODBC.Zope/Plone.Database.Adapter ...   http://zope.egenix.com/
 mxODBC, mxDateTime, mxTextTools ...http://python.egenix.com/

2012-11-28: Released eGenix mx Base 3.2.5 ... http://egenix.com/go36
2013-01-22: Python Meeting Duesseldorf ... 48 days to go

::: 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-announce-list

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


Re: How to check if Pexpect child already exist?

2012-12-05 Thread Dave Angel
On 12/05/2012 02:41 AM, Thomas Elsgaard wrote:
 Hi List

 I am wondering, how can i check if child already exist before i spawn
 ? 

By definition, before you call the spawn, the child doesn't exist.  So
presumably you must mean something else.

 child.isalive() cannot be done on child before it has been spawned.

 ---
 import pexpect

 child=pexpect.spawn('ssh mysurface@192.168.1.105')
 child.sendline('test')
 ---



All we have to do is guess what you really mean.  Do you mean you want
to know if there are any instances of ssh running?  Regardless of how
they got spawned, or from what process?  Or are you trying to determine
if the current process (your script) has any children, of any kind?  Or
what?

Also, you should probably tell what OS and Python versions you're running.

One approach on Linux might be to capture the output of  'ps aux' and
parse it for whatever match criteria you have.

-- 

DaveA

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


ANN: eGenix pyOpenSSL Distribution 0.13.0-1.0.1c

2012-12-05 Thread eGenix Team: M.-A. Lemburg

ANNOUNCING

   eGenix.com pyOpenSSL Distribution

 Version 0.13.0-1.0.1c


 An easy-to-install and easy-to-use distribution
 of the pyOpenSSL Python interface for OpenSSL -
available for Windows, Mac OS X and Unix platforms


This announcement is also available on our web-site for online reading:
http://www.egenix.com/company/news/eGenix-pyOpenSSL-Distribution-0.13.0-1.0.1c-1.html


INTRODUCTION

The eGenix.com pyOpenSSL Distribution includes everything you need to
get started with SSL in Python.

It comes with an easy-to-use installer that includes the most recent
OpenSSL library versions in pre-compiled form, making your application
independent of OS provided OpenSSL libraries:

http://www.egenix.com/products/python/pyOpenSSL/

pyOpenSSL is an open-source Python add-on that allows writing SSL/TLS-
aware network applications as well as certificate management tools:

https://launchpad.net/pyopenssl/

OpenSSL is an open-source implementation of the SSL/TLS protocol:

http://www.openssl.org/


NEWS

This new release of the eGenix.com pyOpenSSL Distribution updates the
included OpenSSL version to 1.0.1c.

New features in OpenSSL 1.0.1c since 1.0.0j
---

OpenSSL 1.0.1c includes several new features:

 * TLS/DTLS heartbeat support

 * Next Protocol Negotiation

 * Support TLS v1.2 and TLS v1.1

as well as several other new features:

http://lwn.net/Articles/486426/

fixes vulnerabilities relative to 1.0.0j:

http://openssl.org/news/vulnerabilities.html

and includes a number of stability enhancements as well as extra
protection against attacks:

http://openssl.org/news/changelog.html

New features in the eGenix pyOpenSSL Distribution
-

 * Added the openssl binary to the OpenSSL package directory. This can
   be used to access OpenSSL functionality not exposed by pyOpenSSL.

 * Changed the Windows OPENSSLDIR default to c:\openssl\ to simplify
   OpenSSL configuration.

 * Fixed OpenSSL assembler build issues on Windows x64 and Mac OS X
   PPC/x86.

As always, we provide binaries that include both pyOpenSSL and the
necessary OpenSSL libraries for all supported platforms:
Windows x86 and x64, Linux x86 and x64, Mac OS X PPC, x86 and x64.

We've also added egg-file distribution versions of our eGenix.com
pyOpenSSL Distribution for Windows, Linux and Mac OS X to the
available download options. These make setups using e.g. zc.buildout
and other egg-file based installers a lot easier.


DOWNLOADS

The download archives and instructions for installing the package can
be found at:

http://www.egenix.com/products/python/pyOpenSSL/


UPGRADING

Before installing this version of pyOpenSSL, please make sure that
you uninstall any previously installed pyOpenSSL version. Otherwise,
you could end up not using the included OpenSSL libs.

___
SUPPORT

Commercial support for these packages is available from eGenix.com.
Please see

http://www.egenix.com/services/support/

for details about our support offerings.


MORE INFORMATION

For more information about the eGenix pyOpenSSL Distributon, licensing
and download instructions, please visit our web-site or write to
sa...@egenix.com.

Enjoy,
-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Dec 05 2012)
 Python Projects, Consulting and Support ...   http://www.egenix.com/
 mxODBC.Zope/Plone.Database.Adapter ...   http://zope.egenix.com/
 mxODBC, mxDateTime, mxTextTools ...http://python.egenix.com/

2012-11-28: Released eGenix mx Base 3.2.5 ... http://egenix.com/go36
2013-01-22: Python Meeting Duesseldorf ... 48 days to go

::: 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: ANN: eGenix mx Base Distribution 3.2.5 (mxDateTime, mxTextTools, etc.)

2012-12-05 Thread M.-A. Lemburg
On 01.12.2012 21:12, Piet van Oostrum wrote:
 M.-A. Lemburg m...@egenix.com writes:
 
 

 ANNOUNCING

eGenix.com mx Base Distribution

   Version 3.2.5 for Python 2.4 - 2.7

Open Source Python extensions providing
  important and useful services
 for Python programmers.

 This announcement is also available on our web-site for online reading:
 http://www.egenix.com/company/news/eGenix-mx-Base-Distribution-3.2.5-GA.html
 
 It says 3.2.4.
 

Thanks for the report. I guess we forgot to update the internal
package version numbers in the release process.

We'll fix that with the next release.

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Dec 05 2012)
 Python Projects, Consulting and Support ...   http://www.egenix.com/
 mxODBC.Zope/Plone.Database.Adapter ...   http://zope.egenix.com/
 mxODBC, mxDateTime, mxTextTools ...http://python.egenix.com/

2012-11-28: Released eGenix mx Base 3.2.5 ... http://egenix.com/go36
2013-01-22: Python Meeting Duesseldorf ... 48 days to go

::: 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


pydoc links prepend a domain

2012-12-05 Thread Gnarlodious
The pydoc.html.docmodule sends a page with clickable links relative to the 
script location. Is there a way to tell pydoc to prepend a string to those URLs?

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


Re: Good use for itertools.dropwhile and itertools.takewhile

2012-12-05 Thread Neil Cerutti
On 2012-12-05, Nick Mellor thebalance...@gmail.com wrote:
 Hi Terry,

 For my money, and especially in your versions, despite several
 expert solutions using other features, itertools has it. It
 seems to me to need less nutting out than the other approaches.
 It's short, robust, has a minimum of symbols, uses simple
 expressions and is not overly clever. If we could just get used
 to using takewhile.

The main reason most of the solutions posted failed is lack of
complete specification to work with while sumultaneously trying
to make as tiny and simplistic a solution as possible.

I'm struggling with the empty description bug right now. ;)

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


Re: Good use for itertools.dropwhile and itertools.takewhile

2012-12-05 Thread Chris Angelico
On Wed, Dec 5, 2012 at 12:17 PM, Nick Mellor thebalance...@gmail.com wrote:

 takewhile mines for gold at the start of a sequence, dropwhile drops the 
 dross at the start of a sequence.

When you're using both over the same sequence and with the same
condition, it seems odd that you need to iterate over it twice.
Perhaps a partitioning iterator would be cleaner - something like
this:

def partitionwhile(predicate, iterable):
iterable = iter(iterable)
while True:
val = next(iterable)
if not predicate(val): break
yield val
raise StopIteration # Signal the end of Phase 1
for val in iterable: yield val # or just yield from iterable, I think

Only the cold hard boot of reality just stomped out the spark of an
idea. Once StopIteration has been raised, that's it, there's no
resuming the iterator. Is there a way around that? Is there a clean
way to say Done for now, but next time you ask, there'll be more?

I tested it on Python 3.2 (yeah, time I upgraded, I know).

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


Re: Good use for itertools.dropwhile and itertools.takewhile

2012-12-05 Thread Neil Cerutti
On 2012-12-05, Chris Angelico ros...@gmail.com wrote:
 On Wed, Dec 5, 2012 at 12:17 PM, Nick Mellor thebalance...@gmail.com wrote:

 takewhile mines for gold at the start of a sequence, dropwhile
 drops the dross at the start of a sequence.

 When you're using both over the same sequence and with the same
 condition, it seems odd that you need to iterate over it twice.
 Perhaps a partitioning iterator would be cleaner - something
 like this:

 def partitionwhile(predicate, iterable):
 iterable = iter(iterable)
 while True:
 val = next(iterable)
 if not predicate(val): break
 yield val
 raise StopIteration # Signal the end of Phase 1
 for val in iterable: yield val # or just yield from iterable, I think

 Only the cold hard boot of reality just stomped out the spark
 of an idea. Once StopIteration has been raised, that's it,
 there's no resuming the iterator. Is there a way around that?
 Is there a clean way to say Done for now, but next time you
 ask, there'll be more?

 I tested it on Python 3.2 (yeah, time I upgraded, I know).

Well, shoot! Then this is a job for groupby, not takewhile.

def prod_desc(s):
split s into product name and product description.

 prod_desc(CAR FIFTY TWO Chrysler LeBaron.)
['CAR FIFTY TWO', 'Chrysler LeBaron.']

 prod_desc(MR. JONESEY Saskatchewan's finest)
['MR. JONESEY', Saskatchewan's finest]

 prod_desc(no product name?)
['', 'no product name?']

 prod_desc(NO DESCRIPTION)
['NO DESCRIPTION', '']

prod = ''
desc = ''
for k, g in itertools.groupby(s.split(),
key=lambda w: any(c.islower() for c in w)):
a = ' '.join(g)
if k:
desc = a 
else:
prod = a
return [prod, desc]

This has no way to preserve odd white space which could break
evil product name differences.

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


http://docs.python.org/2.7/tutorial/index.html

2012-12-05 Thread EAGLE001101

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


Re: using smtp sent large file upto 60MB

2012-12-05 Thread Michael Torrie
On 12/04/2012 05:54 PM, moonhkt wrote:
 Our SMTP can send file more than 60MB. But our notes server can
 configured 100MB,30MB or 10MB. My notes Mail box can receive 100MB.
 
 In UNIX, by below command send  smtp mail.
 uuencode $xfn $xfn | mail -s $SUBJECT $NAME

Just continue to use this set of commands.  You can use the subprocess
module to interact with these programs.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: problem with usbtmc-communication

2012-12-05 Thread wrw
On Dec 4, 2012, at 11:12 AM, Jean Dubois jeandubois...@gmail.com wrote:

 On 4 dec, 15:33, w...@mac.com wrote:
 On Dec 4, 2012, at 7:14 AM, Jean Dubois jeandubois...@gmail.com wrote:
 
 
 
 The following test program which tries to communicate with a Keithley
 2200 programmable power supply using usbtmc in Python does not work as

Is usbtmc a software layer (library of some sort) or some sort of hardware 
adapter?

 expected. I have connected a 10 ohm resistor to its terminals and I
 apply 0.025A, 0.050A, 0.075A en 0.1A,
 I then measure the current and the voltage en write them in a file
 De data produced looks like this:
 0.00544643 0.254061; first current value is wrong, voltage value is
 correct
 0.0250807 0.509289; second current value is wrong, but it corresponds
 to the first, second voltage is correct
 0.0501099 0.763945; 3rd current value is wrong, but it corresponds to
 the second, 3rd voltage is right
 0.075099 1.01792; 4th current value is wrong,  it corresponds to the
 3rd, 4th voltage is right
4th correct current value is missing
 
 But is should be (numerical inaccuracy taking into account)(these data
 were produced by a similar octave-program):
 0.0248947 0.254047
 0.0499105 0.509258
 0.0749044 0.764001
 0.0998926 1.01828
 
 Here is the python-program:
 #!/usr/bin/python
 import time
 import os
 import sys
 measurementcurr=''
 measurementvolt=''
 timesleepdefault=1
 filename ='mydata.txt'
 usbkeith = open('/dev/usbtmc1','r+')
 usbkeith.flush()
 usbkeith.write(*IDN?\n)
 #strip blank line:
 identification=usbkeith.readline().strip()
 print 'Found device: ',identification
 usbkeith.write(SYST:REM + \n)
 usbkeith.write(:SENS:VOLT:PROT 1.5\n)
 keithdata = open(filename,'w')
 #start first measurement
 usbkeith.write(:SOUR:CURR 0.025\n)
 usbkeith.write(:OUTP:STAT ON\n)
 time.sleep(timesleepdefault)
 usbkeith.write(:MEAS:CURR?\n)
 time.sleep(timesleepdefault)
 measurementcurr=usbkeith.readline()
 print 'Measured current 1: ',measurementcurr
 usbkeith.write(MEAS:VOLT?\n)
 time.sleep(timesleepdefault)
 measurementvolt=usbkeith.readline()

Without knowing anything about the usbtmc hardware/software it is hard to make 
real recommendations, but it seems pretty clear that you are being stepped on 
by a buffer problem of some sort.  I'm VERY suspicious of using of readline() 
as a way of getting the data out of the usbtmc thingy.  That makes python treat 
the Keithley as a file-like object and there are way too many ways the file 
pointer may not be where you think it is.  

I note that in your Octave example you are reading characters rather than 
lines.  It seems to me that you have two choices here, either do the equivalent 
in python or dig through the Keithley documentation to find the hex codes that 
usbtmc is presumably translating your commands into.  If you can find those, 
and if you are interested, I can send you off-line the handler I wrote a couple 
of years ago that used a dictionary to translate English commands into hex, 
then assembled those with a checksum and sent the string out to a Keyspan usb 
to serial converter.

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


Re: Good use for itertools.dropwhile and itertools.takewhile

2012-12-05 Thread Ian Kelly
On Wed, Dec 5, 2012 at 7:34 AM, Neil Cerutti ne...@norwich.edu wrote:
 Well, shoot! Then this is a job for groupby, not takewhile.

The problem with groupby is that you can't just limit it to two groups.

 prod_desc(CAPSICUM RED fresh from QLD)
['QLD', 'fresh from']

Once you've got a false key from the groupby, you would need to
pretend that any subsequent groups are part of the false group and
tack them on.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Good use for itertools.dropwhile and itertools.takewhile

2012-12-05 Thread Mark Lawrence

On 05/12/2012 13:45, Chris Angelico wrote:


I tested it on Python 3.2 (yeah, time I upgraded, I know).


Bad move, fancy wanting to go to the completely useless version of 
Python that simply can't handle unicode properly :)


--
Cheers.

Mark Lawrence.

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


Re: http://docs.python.org/2.7/tutorial/index.html

2012-12-05 Thread Mark Lawrence

On 05/12/2012 14:54, EAGLE001101 wrote:




Well speaking personally I'm convinced that the majority of wrong 
thinking people in this country are right, and I'm sick and tired of 
being told that they're not.


An alternative is that you wanted some data regarding the Python 
tutorial, but dear old gmane delivered me precisely nothing worth 
commenting on, perhaps because it smelt the Google Screen Of Death? :)


Please try Plan B.

--
Cheers.

Mark Lawrence.

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


why does dead code costs time?

2012-12-05 Thread Bruno Dupuis
Hi,

I'm interested in compilers optimizations, so I study python compilation process

I ran that script:

import timeit

def f(x):
return None

def g(x):
return None
print(x)

number = 1

print(timeit.timeit('f(1)',setup=from __main__ import f, number=number))
print(timeit.timeit('g(1)',setup=from __main__ import g, number=number))  
 

print(dis.dis(f))
print(dis.dis(g))

It gives this output:

0.003460251959040761
0.004164454061537981
 17   0 LOAD_CONST   0 (None) 
  3 RETURN_VALUE 
None
 20   0 LOAD_GLOBAL  0 (None) 
  3 RETURN_VALUE 

 21   4 LOAD_GLOBAL  1 (print) 
  7 LOAD_FAST0 (x) 
 10 CALL_FUNCTION1 (1 positional, 0 keyword pair) 
 13 POP_TOP  
None

I do not understand why the dead code `print(x)` takes time (~20% in
that case). As we see in the opcode, a call to g(1) returns immediately, so
there should be no delay at all. Where am i wrong?

mmhh... it comes to me now that the gap must be in function loading time...
I'll check ceval.c

However, isn't there a room for a slight optim here? (in this case, the
dead code is obvious, but it may be hidden by complex loops and
conditions)

Cheers


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


Software Developer NEEDED! Morrisville, NC

2012-12-05 Thread Whitney Holman
PYTHON DEVELOPER NEEDED - EXCITING OPPORTUNITY IN MORRISVILLE, NC

The Select Group is seeking a Python software engineer for fun, energetic, and 
growing company in Morrisville, NC.  The ideal candidate will have hands-on 
development experience, and must have working knowledge of Python.  A very 
casual, but fast-paced environment, the company promotes internal growth for 
people with a willingness to learn and a passion for software development.

 RESPONSIBILITIES
.   New Product Development, and Existing Product Support
.   In Charge of Multiple Internal  External Websites

REQUIREMENTS
.   1-3 years of development experience 
.   Python experience

PLUSES
.   Experience with C++, Java, or Perl 
.   Hands on work with SQL, PHP, and Linux 
.   Previously worked with a Content Management System:  WordPress, Joomla, 
or Drupal

Whitney Holman
Technical Recruiter
THE SELECT GROUP

Direct: 919.459.1040 |  Cell: 704.264.6273
whit...@selectgroup.com    |  LinkedIn 
5420 Wade Park Blvd. Ste 100  |  Raleigh, NC 27607  

Web Site| Corporate Video | Awards | Career Resources

At The Select Group we're dedicated to delivering the best experience for our 
candidates and clients. 
We appreciate any and all feedback. Please contact my manager, Byron Dunn at 
by...@selectgroup.com.

Connecting great companies with quality talent..see how at The Select Group 
Advantage




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


Re: Good use for itertools.dropwhile and itertools.takewhile

2012-12-05 Thread Neil Cerutti
On 2012-12-05, Ian Kelly ian.g.ke...@gmail.com wrote:
 On Wed, Dec 5, 2012 at 7:34 AM, Neil Cerutti ne...@norwich.edu wrote:
 Well, shoot! Then this is a job for groupby, not takewhile.

 The problem with groupby is that you can't just limit it to two groups.

 prod_desc(CAPSICUM RED fresh from QLD)
 ['QLD', 'fresh from']

 Once you've got a false key from the groupby, you would need to
 pretend that any subsequent groups are part of the false group
 and tack them on.

Whoops! Yep, that was from the very beginning of the thread.

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


Re: Good use for itertools.dropwhile and itertools.takewhile

2012-12-05 Thread Ian Kelly
On Wed, Dec 5, 2012 at 6:45 AM, Chris Angelico ros...@gmail.com wrote:
 On Wed, Dec 5, 2012 at 12:17 PM, Nick Mellor thebalance...@gmail.com wrote:

 takewhile mines for gold at the start of a sequence, dropwhile drops the 
 dross at the start of a sequence.

 When you're using both over the same sequence and with the same
 condition, it seems odd that you need to iterate over it twice.
 Perhaps a partitioning iterator would be cleaner - something like
 this:

 def partitionwhile(predicate, iterable):
 iterable = iter(iterable)
 while True:
 val = next(iterable)
 if not predicate(val): break
 yield val
 raise StopIteration # Signal the end of Phase 1
 for val in iterable: yield val # or just yield from iterable, I think

 Only the cold hard boot of reality just stomped out the spark of an
 idea. Once StopIteration has been raised, that's it, there's no
 resuming the iterator. Is there a way around that? Is there a clean
 way to say Done for now, but next time you ask, there'll be more?

Return two separate iterators, with the contract that the second
iterator can't be used until the first has completed.  Combined with
Neil's groupby suggestion, we end up with something like this:

def partitionwhile(predicate, iterable):
it = itertools.groupby(iterable, lambda x: bool(predicate(x)))
pushback = missing = object()
def first():
nonlocal pushback
pred, subit = next(it)
if pred:
yield from subit
pushback = None
else:
pushback = subit
def second():
if pushback is missing:
raise TypeError(can't yield from second iterator before
first iterator completes)
elif pushback is not None:
yield from pushback
yield from itertools.chain.from_iterable(subit for key, subit in it)
return first(), second()

 list(map(' '.join, partitionwhile(lambda x: x.upper() == x, CAPSICUM RED 
 fresh from QLD.split(
['CAPSICUM RED', 'fresh from QLD']
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: why does dead code costs time?

2012-12-05 Thread Neil Cerutti
On 2012-12-05, Bruno Dupuis python.ml.bruno.dup...@lisael.org wrote:
 Hi,

 I'm interested in compilers optimizations, so I study python
 compilation process

 I ran that script:

 import timeit

 def f(x):
 return None

 def g(x):
 return None
 print(x)

 number = 1

 print(timeit.timeit('f(1)',setup=from __main__ import f, number=number))
 print(timeit.timeit('g(1)',setup=from __main__ import g, 
 number=number))   

 print(dis.dis(f))
 print(dis.dis(g))

 It gives this output:

 0.003460251959040761
 0.004164454061537981
  17   0 LOAD_CONST   0 (None) 
   3 RETURN_VALUE 
 None
  20   0 LOAD_GLOBAL  0 (None) 
   3 RETURN_VALUE 

  21   4 LOAD_GLOBAL  1 (print) 
   7 LOAD_FAST0 (x) 
  10 CALL_FUNCTION1 (1 positional, 0 keyword pair) 
  13 POP_TOP  
 None

 I do not understand why the dead code `print(x)` takes time (~20% in
 that case). As we see in the opcode, a call to g(1) returns immediately, so
 there should be no delay at all. Where am i wrong?

 mmhh... it comes to me now that the gap must be in function loading time...
 I'll check ceval.c

 However, isn't there a room for a slight optim here? (in this case, the
 dead code is obvious, but it may be hidden by complex loops and
 conditions)

Maybe it's the difference between LOAD_CONST and LOAD_GLOBAL. We
can wonder why g uses the latter.

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


Re: why does dead code costs time?

2012-12-05 Thread Bruno Dupuis
On Wed, Dec 05, 2012 at 04:15:59PM +, Neil Cerutti wrote:
 On 2012-12-05, Bruno Dupuis python.ml.bruno.dup...@lisael.org wrote:
  Hi,
 
  I'm interested in compilers optimizations, so I study python
  compilation process
 
  I ran that script:
 
  import timeit
 
  def f(x):
  return None
 
  def g(x):
  return None
  print(x)
 
  number = 1
 
  print(timeit.timeit('f(1)',setup=from __main__ import f, 
  number=number))
  print(timeit.timeit('g(1)',setup=from __main__ import g, 
  number=number))   
 
  print(dis.dis(f))
  print(dis.dis(g))
 
  It gives this output:
 
  0.003460251959040761
  0.004164454061537981
   17   0 LOAD_CONST   0 (None) 
3 RETURN_VALUE 
  None
   20   0 LOAD_GLOBAL  0 (None) 
3 RETURN_VALUE 
 
   21   4 LOAD_GLOBAL  1 (print) 
7 LOAD_FAST0 (x) 
   10 CALL_FUNCTION1 (1 positional, 0 keyword 
  pair) 
   13 POP_TOP  
  None
 
  I do not understand why the dead code `print(x)` takes time (~20% in
  that case). As we see in the opcode, a call to g(1) returns immediately, so
  there should be no delay at all. Where am i wrong?
 
  mmhh... it comes to me now that the gap must be in function loading time...
  I'll check ceval.c
 
  However, isn't there a room for a slight optim here? (in this case, the
  dead code is obvious, but it may be hidden by complex loops and
  conditions)
 
 Maybe it's the difference between LOAD_CONST and LOAD_GLOBAL. We
 can wonder why g uses the latter.

Good point! I didn't even noticed that. It's weird... Maybe the
difference comes from a peehole optim on f which is not possible on g as
g is to complex.

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

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


Re: Good use for itertools.dropwhile and itertools.takewhile

2012-12-05 Thread Nick Mellor
Hi Neil,

Here's some sample data. The live data is about 300 minor variations on the 
sample data, about 20,000 lines.

Nick

Notes:

1. Whitespace is only used for word boundaries. Surplus whitespace is not 
significant and can be stripped

2. Retain punctuation and parentheses

3. Product is zero or more words in all caps at start of line

4. Description is zero or more words beginning with first word that is not all 
caps. Description continues to the end of the line

5. Return tuple of strings (product, description)


Sample data
---

BEANS hand picked
BEETROOT certified organic
BOK CHOY (bunch)
BROCCOLI Mornington Peninsula
BRUSSEL  SPROUTS
CABBAGE green
CABBAGE Red
CAPSICUM RED
CARROTS
CARROTS loose
CARROTS juicing, certified organic
CARROTS Trentham, large seconds, certified organic
CARROTS Trentham, firsts, certified organic
CAULIFLOWER
CELERY Mornington Peninsula IPM grower 
CELERY Mornington Peninsula IPM grower 
CUCUMBER
EGGPLANT
FENNEL
GARLIC (from Argentina)
GINGER fresh uncured
KALE (bunch)
KOHL RABI certified organic
LEEKS
 LETTUCE iceberg
MUSHROOM cup or flat
MUSHROOM Swiss brown
ONION brown
ONION red
ONION spring (bunch)
PARSNIP, certified organic
POTATOES certified organic
POTATOES Sebago
POTATOES Desiree
POTATOES Bullarto chemical free
POTATOES Dutch Cream
POTATOES Nicola
POTATOES Pontiac
POTATOES Otway Red
POTATOES teardrop
PUMPKIN certified organic
SCHALLOTS brown
SNOW PEAS
SPINACH I'll try to get certified organic (bunch)
SWEET POTATO gold certified organic 
SWEET POTATO red small
SWEDE certified organic
TOMATOES  Qld
TURMERIC fresh certified organic
ZUCCHINI
APPLES Harcourt  Pink Lady, Fuji, Granny Smith
APPLES Harcourt 2 kg bags, Pink Lady or Fuji (bag)
AVOCADOS
AVOCADOS certified organic, seconds
BANANAS Qld, organic
GRAPEFRUIT
GRAPES crimson seedless
KIWI FRUIT Qld certified organic
LEMONS
LIMES
MANDARINS
ORANGES Navel
PEARS Beurre Bosc Harcourt new season
PEARS Packham, Harcourt new season
SULTANAS 350g pre-packed bags
EGGS Melita free range, Barker's Creek
BASIL (bunch)
CORIANDER (bunch)
DILL (bunch)
MINT (bunch)
PARSLEY (bunch)


On Thursday, 6 December 2012 00:29:13 UTC+11, Neil Cerutti  wrote:
 On 2012-12-05, Nick Mellor thebalance...@gmail.com wrote:
 
  Hi Terry,
 
 
 
  For my money, and especially in your versions, despite several
 
  expert solutions using other features, itertools has it. It
 
  seems to me to need less nutting out than the other approaches.
 
  It's short, robust, has a minimum of symbols, uses simple
 
  expressions and is not overly clever. If we could just get used
 
  to using takewhile.
 
 
 
 The main reason most of the solutions posted failed is lack of
 
 complete specification to work with while sumultaneously trying
 
 to make as tiny and simplistic a solution as possible.
 
 
 
 I'm struggling with the empty description bug right now. ;)
 
 
 
 -- 
 
 Neil Cerutti

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


Re: why does dead code costs time?

2012-12-05 Thread Bruno Dupuis
On Wed, Dec 05, 2012 at 05:40:51PM +0100, Bruno Dupuis wrote:
 On Wed, Dec 05, 2012 at 04:15:59PM +, Neil Cerutti wrote:
  Maybe it's the difference between LOAD_CONST and LOAD_GLOBAL. We
  can wonder why g uses the latter.
 
 Good point! I didn't even noticed that. It's weird... Maybe the
 difference comes from a peehole optim on f which is not possible on g as
 g is to complex.
 

Neil, you were right, thanks. I patched peehole.c to remove this optim, and
now the figures are the same. I investigate to find out why the latter
function is not optimized the same way (and if it can be, I'll propose a
patch for that)

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


Re: why does dead code costs time?

2012-12-05 Thread Steven D'Aprano
On Wed, 05 Dec 2012 16:46:39 +0100, Bruno Dupuis wrote:

 Hi,
 
 I'm interested in compilers optimizations, so I study python compilation
 process
 
 I ran that script:
 
 import timeit
 
 def f(x):
 return None
 
 def g(x):
 return None
 print(x)
 
 number = 1
 
 print(timeit.timeit('f(1)',setup=from __main__ import f,
 number=number)) print(timeit.timeit('g(1)',setup=from __main__
 import g, number=number))
 
 print(dis.dis(f))
 print(dis.dis(g))
 
 It gives this output:
 
 0.003460251959040761
 0.004164454061537981
  17   0 LOAD_CONST   0 (None)
   3 RETURN_VALUE
 None
  20   0 LOAD_GLOBAL  0 (None)
   3 RETURN_VALUE
 
  21   4 LOAD_GLOBAL  1 (print)
   7 LOAD_FAST0 (x)
  10 CALL_FUNCTION1 (1 positional, 0 keyword
  pair) 13 POP_TOP
 None
 
 I do not understand why the dead code `print(x)` takes time (~20% in
 that case). As we see in the opcode, a call to g(1) returns immediately,
 so there should be no delay at all. Where am i wrong?

The difference is almost certain between the LOAD_CONST and the 
LOAD_GLOBAL.

As to *why* there is such a difference, I believe that's a leftover from 
early Python days when None was not a keyword and could be reassigned.


[steve@ando ~]$ python1.5
Python 1.5.2 (#1, Aug 27 2012, 09:09:18)  [GCC 4.1.2 20080704 (Red Hat 
4.1.2-52)] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
 from dis import dis

 def h():
... x = 1
... return None
...
 dis(h)
  0 SET_LINENO  1

  3 SET_LINENO  2
  6 LOAD_CONST  1 (1)
  9 STORE_FAST  0 (x)

 12 SET_LINENO  3
 15 LOAD_GLOBAL 1 (None)
 18 RETURN_VALUE
 19 LOAD_CONST  0 (None)
 22 RETURN_VALUE
 None = 42
 h()
42


Now that None is a keyword, it should always be a LOAD_CONST.


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


Re: How does one make argparse print usage when no options are provided on the command line?

2012-12-05 Thread Bruno Dupuis
On Wed, Dec 05, 2012 at 08:48:30AM -0800, rh wrote:
 I have argparse working with one exception. I wanted the program to print out
 usage when no command line options are given. But I only came across
 other examples where people didn't use argparse but instead printed out
 a separate usage statement. So they used argparse for everything but the
 case where no command line args are given.
 

this is quite raw, but i'd add 

import sys
if len(sys.argv) == 1:
sys.argv.append('-h')

before I call parser.parse_args()

Should work


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


Re: why does dead code costs time?

2012-12-05 Thread Steven D'Aprano
On Wed, 05 Dec 2012 17:34:57 +, Steven D'Aprano wrote:

 I believe that's a leftover from
 early Python days when None was not a keyword and could be reassigned.

Oops! Wrong copy and paste! Here's a simpler version:

[steve@ando ~]$ python1.5
Python 1.5.2 (#1, Aug 27 2012, 09:09:18)  [GCC 4.1.2 20080704 (Red Hat 
4.1.2-52)] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
 from dis import dis
 def h():
... return None
...
 dis(h)
  0 SET_LINENO  1

  3 SET_LINENO  2
  6 LOAD_GLOBAL 0 (None)
  9 RETURN_VALUE
 10 LOAD_CONST  0 (None)
 13 RETURN_VALUE


The conclusion remains the same: calling LOAD_GLOBAL None is likely a 
fossil from ancient Python before it was a keyword.


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


Re: Good use for itertools.dropwhile and itertools.takewhile

2012-12-05 Thread MRAB

On 2012-12-05 17:04, Nick Mellor wrote:

Hi Neil,

Here's some sample data. The live data is about 300 minor variations on the 
sample data, about 20,000 lines.


[snip]
You have a duplicate:


CELERY Mornington Peninsula IPM grower
CELERY Mornington Peninsula IPM grower


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


Re: Good use for itertools.dropwhile and itertools.takewhile

2012-12-05 Thread MRAB

On 2012-12-05 13:45, Chris Angelico wrote:

On Wed, Dec 5, 2012 at 12:17 PM, Nick Mellor thebalance...@gmail.com wrote:


takewhile mines for gold at the start of a sequence, dropwhile drops the dross 
at the start of a sequence.


When you're using both over the same sequence and with the same
condition, it seems odd that you need to iterate over it twice.
Perhaps a partitioning iterator would be cleaner - something like
this:

def partitionwhile(predicate, iterable):
 iterable = iter(iterable)
 while True:
 val = next(iterable)
 if not predicate(val): break
 yield val
 raise StopIteration # Signal the end of Phase 1
 for val in iterable: yield val # or just yield from iterable, I think

Only the cold hard boot of reality just stomped out the spark of an
idea. Once StopIteration has been raised, that's it, there's no
resuming the iterator. Is there a way around that? Is there a clean
way to say Done for now, but next time you ask, there'll be more?


Perhaps you could have some kind of partitioner object:

class Partitioner:
_SENTINEL = object()

def __init__(self, iterable):
self._iterable = iter(iterable)
self._unused_item = self._SENTINEL

def takewhile(self, condition):
if self._unused_item is not self._SENTINEL:
if not condition(self._unused_item):
raise StopIteration

yield self._unused_item
self._unused_item = self._SENTINEL

for item in self._iterable:
if not condition(item):
self._unused_item = item
break

yield item

raise StopIteration

def remainder(self):
if self._unused_item is not self._SENTINEL:
yield self._unused_item
self._unused_item = self._SENTINEL

for item in self._iterable:
yield item

raise StopIteration

def is_all_caps(word):
return word == word.upper()

part = Partitioner(CAPSICUM RED fresh from QLD.split())
product =  .join(part.takewhile(is_all_caps))
description =  .join(part.remainder())
print([product, description])

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


Re: why does dead code costs time?

2012-12-05 Thread Ian Kelly
On Wed, Dec 5, 2012 at 10:34 AM, Steven D'Aprano
steve+comp.lang.pyt...@pearwood.info wrote:
 The difference is almost certain between the LOAD_CONST and the
 LOAD_GLOBAL.

 As to *why* there is such a difference, I believe that's a leftover from
 early Python days when None was not a keyword and could be reassigned.

I think this should even be considered a bug, not just a missing
optimization.  Consider:

 globals()['None'] = 42
 def f(x):
... return None
... print(x)
...
 f('test')
42

The use of the LOAD_GLOBAL allows None to effectively be reassigned.

It's also worth noting that:

 def f(x):
... return
... print(x)
...
 dis.dis(f)
  2   0 LOAD_CONST   0 (None)
  3 RETURN_VALUE

  3   4 LOAD_GLOBAL  0 (print)
  7 LOAD_FAST0 (x)
 10 CALL_FUNCTION1 (1 positional, 0 keyword pair)
 13 POP_TOP

So if you just write 'return' rather than 'return None', you get the
correct bytecode.  Additionally, the use of LOAD_GLOBAL instead of
LOAD_CONST seems to be linked to having unreachable code at the end of
the function.  This is fine:

 def f(x):
... if x:
... return None
... print(x)
...
 dis.dis(f)
  2   0 LOAD_FAST0 (x)
  3 POP_JUMP_IF_FALSE   10

  3   6 LOAD_CONST   0 (None)
  9 RETURN_VALUE

  410 LOAD_GLOBAL  1 (print)
 13 LOAD_FAST0 (x)
 16 CALL_FUNCTION1 (1 positional, 0 keyword pair)
 19 POP_TOP
 20 LOAD_CONST   0 (None)
 23 RETURN_VALUE

But this is not.  Note here that *both* loads of None become
LOAD_GLOBAL in this case:

 def f(x):
... if x:
... return None
... return None
... print(x)
...
 dis.dis(f)
  2   0 LOAD_FAST0 (x)
  3 POP_JUMP_IF_FALSE   13

  3   6 LOAD_GLOBAL  0 (None)
  9 RETURN_VALUE
 10 JUMP_FORWARD 0 (to 13)

  413 LOAD_GLOBAL  0 (None)
 16 RETURN_VALUE

  5  17 LOAD_GLOBAL  1 (print)
 20 LOAD_FAST0 (x)
 23 CALL_FUNCTION1 (1 positional, 0 keyword pair)
 26 POP_TOP
-- 
http://mail.python.org/mailman/listinfo/python-list


Help joining two files delimited with pipe character (|)

2012-12-05 Thread Daniel Doo
Hello,

I am new to Python.  Is there a method to join two pipe delimited files using 
a unique key that appears in both files?  I would like to implement something 
similar to the Unix join command.

Thanks for your help!


Topeka Capital Markets Disclaimers - 
http://topekacapitalmarkets.com/Disclaimers.aspx
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Help joining two files delimited with pipe character (|)

2012-12-05 Thread Miki Tebeka
On Wednesday, December 5, 2012 9:57:31 AM UTC-8, Daniel Doo wrote:
 I am new to Python.  Is there a method to “join” two pipe delimited files 
 using a unique key that appears in both files? 
Have a look at Panda's concat 
(http://pandas.pydata.org/pandas-docs/dev/merging.html). It also have utilities 
to read delimited files into DataFrame.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Help joining two files delimited with pipe character (|)

2012-12-05 Thread Ian Kelly
On Wed, Dec 5, 2012 at 11:18 AM, Ian Kelly ian.g.ke...@gmail.com wrote:
 On Wed, Dec 5, 2012 at 10:57 AM, Daniel Doo d...@topekacapitalmarkets.com 
 wrote:
 I am new to Python.  Is there a method to “join” two pipe delimited files
 using a unique key that appears in both files?  I would like to implement
 something similar to the Unix join command.

 If the files are small enough to fit in virtual memory, load one of
 the files into a dict where the key is the join field and the value is
 the full line.  Then iterate over the other file one line at a time,
 checking to see if the join field is in the dict.  If it is, output
 the joined line.  Otherwise, skip it.

Note that this assumes that the join fields are unique.  If they're
not, then you would need to store lists of lines rather than single
lines in the dict.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Help joining two files delimited with pipe character (|)

2012-12-05 Thread Ian Kelly
On Wed, Dec 5, 2012 at 10:57 AM, Daniel Doo d...@topekacapitalmarkets.com 
wrote:
 I am new to Python.  Is there a method to “join” two pipe delimited files
 using a unique key that appears in both files?  I would like to implement
 something similar to the Unix join command.

If the files are small enough to fit in virtual memory, load one of
the files into a dict where the key is the join field and the value is
the full line.  Then iterate over the other file one line at a time,
checking to see if the join field is in the dict.  If it is, output
the joined line.  Otherwise, skip it.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: why does dead code costs time?

2012-12-05 Thread Bruno Dupuis
On Wed, Dec 05, 2012 at 10:59:26AM -0700, Ian Kelly wrote:
 On Wed, Dec 5, 2012 at 10:34 AM, Steven D'Aprano
 steve+comp.lang.pyt...@pearwood.info wrote:
  The difference is almost certain between the LOAD_CONST and the
  LOAD_GLOBAL.
 
  As to *why* there is such a difference, I believe that's a leftover from
  early Python days when None was not a keyword and could be reassigned.
 
 I think this should even be considered a bug, not just a missing
 optimization.  Consider:

This is definitely a bug

  globals()['None'] = 42
  def f(x):
 ... return None
 ... print(x)
 ...
  f('test')
 42

This one is pretty scary

The difference between `return None` and `return` leads to inconsistency and
is in contradiction with the specs, AFAIK. I'm glad we pointed this out.

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


Re: Good use for itertools.dropwhile and itertools.takewhile

2012-12-05 Thread Neil Cerutti
On 2012-12-05, Nick Mellor thebalance...@gmail.com wrote:
 Hi Neil,

 Here's some sample data. The live data is about 300 minor
 variations on the sample data, about 20,000 lines.

Thanks, Nick.

This slight variation on my first groupby try seems to work for
the test data.

def prod_desc(s):
prod = []
desc = []
for k, g in itertools.groupby(s.split(),
key=lambda w: any(c.islower() for c in w)):
if prod or k:
desc.extend(g)
else:
prod.extend(g)
return [' '.join(prod), ' '.join(desc)]

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


Re: How does one make argparse print usage when no options are provided on the command line?

2012-12-05 Thread Ian Kelly
On Wed, Dec 5, 2012 at 9:48 AM, rh richard_hubb...@lavabit.com wrote:
 I have argparse working with one exception. I wanted the program to print out
 usage when no command line options are given. But I only came across
 other examples where people didn't use argparse but instead printed out
 a separate usage statement. So they used argparse for everything but the
 case where no command line args are given.

if len(sys.argv) = 1:
parser.print_usage()
sys.exit(1)
else:
args = parser.parse_args()
-- 
http://mail.python.org/mailman/listinfo/python-list


Need porting of German Wotan AI into Python

2012-12-05 Thread Mentifex
The Artificial General Intelligence (AGI) at

http://www.scn.org/~mentifex/mindforth.txt

has been re-recreated in the German language at

http://www.scn.org/~mentifex/DeKi.txt

and needs porting into the Python language.

There are no funds available for payment but
mega kudos will accrue to the successful Python
programmer who creates the Python True AI
based on the various AI Minds such as

http://www.scn.org/~mentifex/AiMind.html

in English over JavaScript and the Russian

http://www.scn.org/~mentifex/Dushka.html

also in JavaScript.

Mentifex (Arthur)
--
http://mind.sourceforge.net/python.html
http://www.listbox.com/subscribe/?list_id=303
http://www.chatbots.org/ai_zone/viewthread/240
http://www.amazon.com/The-Art-Meme-ebook/dp/B007ZI66FS/
-- 
http://mail.python.org/mailman/listinfo/python-list


Secretly passing parameter to function

2012-12-05 Thread Olivier Scalbert

Hi all !

I have a problem that is not easy to explained, so I have tried to 
reduce it a lot.


We are using a framework, that we can not modify.

in framework.py:
def do(something):
   '''
   Here we are in a framework that can not be modified ...
   It does a lot of things

   and finally:
   '''
   something()

in test.py:
from framework import *

def step1():
   print Do step1

def step2():
   print Do step2


# We ask the framework to do some work.
do(step1)
do(step2)
do(step3)


We are writing step1, step2, ... and asking the framework to process them.
Everything is ok, until we want to add a parameter to some steps.
We want to be able to do that:

in test.py:
from framework import *

def step1(param):
   print Do step1 with param

def step2():
   print Do step2


# We ask the framework to do some work.

do(step1, param = None)
do(step1, param = [0, 1, 5]) # again
do(step2)

Of course it does not work ...
TypeError: do() takes exactly 1 argument (2 given)

And we can not modify the framework (in which do is defined.

One solution would be to use a global variable that can be set before 
each step. But it is not very elegant ...


One other approach would be to add dynamically an attribute the the 
step1 function, and retrieve it inside the function, but it is perhaps 
overkill.


Do you have some ideas ?

Thanks,


Olivier

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


Re: Good use for itertools.dropwhile and itertools.takewhile

2012-12-05 Thread Nick Mellor
Neil,

Further down the data, found another edge case:

Spring ONION from QLD

Following the spec, the whole line should be description (description starts at 
first word that is not all caps.) This case breaks the latest groupby.

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


Remote server: running a Python script and making *.csv files publicly available

2012-12-05 Thread Jason Hsu
I have a Python 2.7 script that produces *.csv files.  I'd like to run this 
Python script on a remote server and make the *.csv files publicly available to 
read.

Can this be done on Heroku?  I've gone through the tutorial, but it seems to be 
geared towards people who want to create a whole web site.

If Heroku isn't the solution for me, what are the alternatives?  I tried Google 
App Engine, but it requires Python 2.5 and won't work with 2.7.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Secretly passing parameter to function

2012-12-05 Thread Dave Angel
On 12/05/2012 01:50 PM, Olivier Scalbert wrote:
 Hi all !

 I have a problem that is not easy to explained, so I have tried to
 reduce it a lot.

 We are using a framework, that we can not modify.

 in framework.py:
 def do(something):
'''
Here we are in a framework that can not be modified ...
It does a lot of things

and finally:
'''
something()

 in test.py:
 from framework import *

 def step1():
print Do step1

 def step2():
print Do step2


 # We ask the framework to do some work.
 do(step1)
 do(step2)
 do(step3)


 We are writing step1, step2, ... and asking the framework to process
 them.
 Everything is ok, until we want to add a parameter to some steps.
 We want to be able to do that:

 in test.py:
 from framework import *

 def step1(param):
print Do step1 with param

 def step2():
print Do step2


 # We ask the framework to do some work.

 do(step1, param = None)
 do(step1, param = [0, 1, 5]) # again
 do(step2)

 Of course it does not work ...
 TypeError: do() takes exactly 1 argument (2 given)

 And we can not modify the framework (in which do is defined.

 One solution would be to use a global variable that can be set before
 each step. But it is not very elegant ...

 One other approach would be to add dynamically an attribute the the
 step1 function, and retrieve it inside the function, but it is perhaps
 overkill.

 Do you have some ideas ?

Other approaches are  lamba, default-argument trick, a function closure,
a callable class instance, and functools.partial.

The real question you have to ask is what is the scope AND LIFETIME of
this parameter.  Suppose you want to want to have five of these same
calls, with five different parameters?  (Example, a GUI where you have a
single function which might be called on an event of any of five buttons
-- you want to pass the button-object to the function)


import functools

def step1(param):
print Do step1 with param, param

def step2():
print Do step2

class Framework:
def __init__(self):
self.pending = []
def do(self, func):
print current, self.pending
self.pending.append(func)
def flush(self):
for func in self.pending:
func()


frame = Framework()
frame.do(step2)
frame.do(step2)
frame.do(step2)
frame.do(functools.partial(step1, 45))

frame.flush()



-- 

DaveA

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


Re: Secretly passing parameter to function

2012-12-05 Thread Chris Kaynor
On Wed, Dec 5, 2012 at 10:50 AM, Olivier Scalbert 
olivier.scalb...@algosyn.com wrote:

 Hi all !

 I have a problem that is not easy to explained, so I have tried to reduce
 it a lot.

 We are using a framework, that we can not modify.

 in framework.py:
 def do(something):
'''
Here we are in a framework that can not be modified ...
It does a lot of things

and finally:
'''
something()

 in test.py:
 from framework import *

 def step1():
print Do step1

 def step2():
print Do step2


 # We ask the framework to do some work.
 do(step1)
 do(step2)
 do(step3)


 We are writing step1, step2, ... and asking the framework to process them.
 Everything is ok, until we want to add a parameter to some steps.
 We want to be able to do that:

 in test.py:
 from framework import *

 def step1(param):
print Do step1 with param

 def step2():
print Do step2


 # We ask the framework to do some work.

 do(step1, param = None)
 do(step1, param = [0, 1, 5]) # again
 do(step2)

 Probably the easiest solution would be to use functools.partial to create
a proxy function, as follows:

import functools
do(functools.partial(step1, param=None))
do(functools.partial(step1, param=[0,1,5]))
do(step2)

Effectively what functools.partial does is returns a new function with the
arguments defined in the constructor. It looks something like:

def partial(func, *args, **kwargs):
def newFunc(*cargs, **ckwargs):
return func(*args+cargs, **kwargs+ckwargs)
return newFunc

Note, that implementation of partial may not exactly match the semantics of
the real thing, and is untested and may just flat out not work...I'd
recommend using the real one instead.

In this case, it will produce a function that really takes no additional
arguments, as all arguments are defined as part of the creation, meaning
the new function will match the requirements by the framework of taking 0
arguments, despite the actual function taking and receiving one keyword
argument (param).

There is one caveat of using functools.partial: for positional arguments,
you cal only fill the arguments in left-to-right order; you cannot specify
the second argument of a list, other than specifying it by name. For
example, for the function def test(a, b, c), you can specify arguments
a, a and b, or a and b and c by position, but you cannot specify only
the arguments b, c, a and c, or b and c by position. You can,
however, specify them by name, as keyword-arguments.



An alternative approach to the problem is to either use lamdas or write a
specialized wrapper like my example partial above to specify the arguments.
These eliminate the restriction on argument order for partial.

An example for lamdas would be:
do(lambda: step1(param=None))
do(lambda: step1(param=[0,1,5]))
do(step2)

This works much the same way as partial, in that it creates a new, unnamed
function which specifies the arguments.



 Of course it does not work ...
 TypeError: do() takes exactly 1 argument (2 given)

 And we can not modify the framework (in which do is defined.

 One solution would be to use a global variable that can be set before each
 step. But it is not very elegant ...

 One other approach would be to add dynamically an attribute the the step1
 function, and retrieve it inside the function, but it is perhaps overkill.

 Do you have some ideas ?

 Thanks,


 Olivier

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

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


mini browser with python

2012-12-05 Thread inq1ltd
Python help.

I can connect to and download a web page,
html code, and save it to a file.  If connected 
to the web, I can use KWrite to open the file 
and navigate the page.

I want to view the html file without using a browser 
or KWrite as I do now. 

In other words I need a mini, simple browser; 
something I can build that will open, read and 
display a saved html file.

I would appreciate some direction.

jimonlinux

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


Re: Good use for itertools.dropwhile and itertools.takewhile

2012-12-05 Thread Neil Cerutti
On 2012-12-05, Nick Mellor thebalance...@gmail.com wrote:
 Neil,

 Further down the data, found another edge case:

 Spring ONION from QLD

 Following the spec, the whole line should be description
 (description starts at first word that is not all caps.) This
 case breaks the latest groupby.

A-ha! I did check your samples for the case of an empty product
name and not find any started to think it couldn't happen.

Change

   if prod or k:

to

   if desc or prod or k:

If this data file gets any weirder, let me know. ;)

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


Re: Python Noob Question.

2012-12-05 Thread Owatch
Re

On Monday, December 3, 2012 4:19:51 PM UTC+2, Alexander Blinne wrote:
 Hello,
 
 
 
 by having a quick look at their website i found a plugin for CoreTemp
 
 which acts as a server and can be asked for status information of the
 
 cpu. Now your task is really simple: write a little function or class
 
 that opens a network socket, connects to that plugin und asks it for the
 
 information you require. You just need to find out what network protocol
 
 this plugin uses to communicate. If it is no standard protocol for which
 
 a higher level module is present (xmlrpc or something), see
 
 http://docs.python.org/3/library/socket.html for low level sockets.
 
 
 
 Greetings

Really sorry for the late reply, but I've got a lot going on lately. 

Thanks a TON for your answer thought, this is exactly what I really hoped for.
The problem for me is that I don't actually know anything about writing a 
function that opens a network socket, and connects to that plugin und asks it 
for the 
information you require.

That's all really beyond me, all I can do is what I did so far, which is make 
it ask for your temperature value, and then test it to see if its an integer

Then (I added this for testing) It asks for any temperature value. And if it 
exceeds the given limit, it rings an alarm. Until it freezes and becomes 
unresponsive :D

I don't know how to make it 'query' or grab values constantly, if you don't 
mind my potentially incorrect terminology. 

If you know how to do this, or could explain it really step by step to me, I 
would be ever so thankful. Or even if you know of somewhere which I could ask 
this.

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


Re: mini browser with python

2012-12-05 Thread David Hutto
I think this is the snippe that you want:

david@david-desktop:~$ python
Python 2.7.3 (default, Aug  1 2012, 05:16:07)
[GCC 4.6.3] on linux2
Type help, copyright, credits or license for more information.
 import urlliib
 url_to_read = urllib.urlopen('http://hitwebdevelopment.com')
 for line in url_to_read:
... print line



-- 
Best Regards,
David Hutto
CEO: http://www.hitwebdevelopment.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: problem with usbtmc-communication

2012-12-05 Thread Jean Dubois
On 5 dec, 16:26, w...@mac.com wrote:
 On Dec 4, 2012, at 11:12 AM, Jean Dubois jeandubois...@gmail.com wrote:

  On 4 dec, 15:33, w...@mac.com wrote:
  On Dec 4, 2012, at 7:14 AM, Jean Dubois jeandubois...@gmail.com wrote:

  The following test program which tries to communicate with a Keithley
  2200 programmable power supply using usbtmc in Python does not work as

 Is usbtmc a software layer (library of some sort) or some sort of hardware 
 adapter?
This is the information concerning usbtmc from the National
Instruments site:
USBTMC stands for USB Test  Measurement Class. USBTMC is a protocol
built on top of USB that allows GPIB-like communication with USB
devices. From the user's point of view, the USB device behaves just
like a GPIB device. For example, you can use VISA Write to send the
*IDN? query and use VISA Read to get the response. The USBTMC protocol
supports service request, triggers and other GPIB specific operations.









  expected. I have connected a 10 ohm resistor to its terminals and I
  apply 0.025A, 0.050A, 0.075A en 0.1A,
  I then measure the current and the voltage en write them in a file
  De data produced looks like this:
  0.00544643 0.254061; first current value is wrong, voltage value is
  correct
  0.0250807 0.509289; second current value is wrong, but it corresponds
  to the first, second voltage is correct
  0.0501099 0.763945; 3rd current value is wrong, but it corresponds to
  the second, 3rd voltage is right
  0.075099 1.01792; 4th current value is wrong,  it corresponds to the
  3rd, 4th voltage is right
                             4th correct current value is missing

  But is should be (numerical inaccuracy taking into account)(these data
  were produced by a similar octave-program):
  0.0248947 0.254047
  0.0499105 0.509258
  0.0749044 0.764001
  0.0998926 1.01828

  Here is the python-program:
  #!/usr/bin/python
  import time
  import os
  import sys
  measurementcurr=''
  measurementvolt=''
  timesleepdefault=1
  filename ='mydata.txt'
  usbkeith = open('/dev/usbtmc1','r+')
  usbkeith.flush()
  usbkeith.write(*IDN?\n)
  #strip blank line:
  identification=usbkeith.readline().strip()
  print 'Found device: ',identification
  usbkeith.write(SYST:REM + \n)
  usbkeith.write(:SENS:VOLT:PROT 1.5\n)
  keithdata = open(filename,'w')
  #start first measurement
  usbkeith.write(:SOUR:CURR 0.025\n)
  usbkeith.write(:OUTP:STAT ON\n)
  time.sleep(timesleepdefault)
  usbkeith.write(:MEAS:CURR?\n)
  time.sleep(timesleepdefault)
  measurementcurr=usbkeith.readline()
  print 'Measured current 1: ',measurementcurr
  usbkeith.write(MEAS:VOLT?\n)
  time.sleep(timesleepdefault)
  measurementvolt=usbkeith.readline()

 Without knowing anything about the usbtmc hardware/software it is hard to 
 make real recommendations, but it seems pretty clear that you are being 
 stepped on by a buffer problem of some sort.  I'm VERY suspicious of using of 
 readline() as a way of getting the data out of the usbtmc thingy.  That makes 
 python treat the Keithley as a file-like object and there are way too many 
 ways the file pointer may not be where you think it is.

 I note that in your Octave example you are reading characters rather than 
 lines.  It seems to me that you have two choices here, either do the 
 equivalent in python or dig through the Keithley documentation to find the 
 hex codes that usbtmc is presumably translating your commands into.  If you 
 can find those, and if you are interested, I can send you off-line the 
 handler I wrote a couple of years ago that used a dictionary to translate 
 English commands into hex, then assembled those with a checksum and sent the 
 string out to a Keyspan usb to serial converter.

If you could show me how to do the equivalent in Python I'd
appreciate that very much

best regards,
jean
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: why does dead code costs time?

2012-12-05 Thread Terry Reedy

On 12/5/2012 1:24 PM, Bruno Dupuis wrote:

On Wed, Dec 05, 2012 at 10:59:26AM -0700, Ian Kelly wrote:

On Wed, Dec 5, 2012 at 10:34 AM, Steven D'Aprano
steve+comp.lang.pyt...@pearwood.info wrote:

The difference is almost certain between the LOAD_CONST and the
LOAD_GLOBAL.

As to *why* there is such a difference, I believe that's a leftover from
early Python days when None was not a keyword and could be reassigned.


I think this should even be considered a bug, not just a missing
optimization.  Consider:


This is definitely a bug


globals()['None'] = 42
def f(x):

... return None
... print(x)
...

f('test')

42


This one is pretty scary

The difference between `return None` and `return` leads to inconsistency and
is in contradiction with the specs, AFAIK. I'm glad we pointed this out.


You did not specify version, but I confirmed in 3.3.0. Please open a 
tracker issue.


--
Terry Jan Reedy

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


Re: why does dead code costs time?

2012-12-05 Thread Chris Kaynor
On Wed, Dec 5, 2012 at 12:41 PM, Terry Reedy tjre...@udel.edu wrote:

 On 12/5/2012 1:24 PM, Bruno Dupuis wrote:

 On Wed, Dec 05, 2012 at 10:59:26AM -0700, Ian Kelly wrote:

 I think this should even be considered a bug, not just a missing
 optimization.  Consider:


 This is definitely a bug

  globals()['None'] = 42
 def f(x):

 ... return None
 ... print(x)
 ...

  f('test')

 42


 This one is pretty scary

 The difference between `return None` and `return` leads to inconsistency
 and
 is in contradiction with the specs, AFAIK. I'm glad we pointed this out.


 You did not specify version, but I confirmed in 3.3.0. Please open a
 tracker issue.


It also occurs in 2.6.4
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: why does dead code costs time?

2012-12-05 Thread Bruno Dupuis
On Wed, Dec 05, 2012 at 03:41:19PM -0500, Terry Reedy wrote:
 On 12/5/2012 1:24 PM, Bruno Dupuis wrote:
 On Wed, Dec 05, 2012 at 10:59:26AM -0700, Ian Kelly wrote:
 On Wed, Dec 5, 2012 at 10:34 AM, Steven D'Aprano
 steve+comp.lang.pyt...@pearwood.info wrote:
 The difference is almost certain between the LOAD_CONST and the
 LOAD_GLOBAL.
 
 As to *why* there is such a difference, I believe that's a leftover from
 early Python days when None was not a keyword and could be reassigned.
 
 I think this should even be considered a bug, not just a missing
 optimization.  Consider:
 
 This is definitely a bug
 
 globals()['None'] = 42
 def f(x):
 ... return None
 ... print(x)
 ...
 f('test')
 42
 
 This one is pretty scary
 
 The difference between `return None` and `return` leads to inconsistency and
 is in contradiction with the specs, AFAIK. I'm glad we pointed this out.
 
 You did not specify version, but I confirmed in 3.3.0. Please open a
 tracker issue.

It is also in 2.7 and 3.4 head, I didn't test other versions. I forgot
to mention here the issue I have just opened:
http://bugs.python.org/issue16619

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


Re: Remote server: running a Python script and making *.csv files publicly available

2012-12-05 Thread Miki Tebeka
On Wednesday, December 5, 2012 11:14:42 AM UTC-8, Jason Hsu wrote:
 make the *.csv files publicly available to read.

 Can this be done on Heroku?  I've gone through the tutorial, but it seems to 
 be geared towards people who want to create a whole web site.
See one option - 
http://stackoverflow.com/questions/8470733/how-can-i-handle-static-files-with-python-webapp2-in-heroku

 If Heroku isn't the solution for me, what are the alternatives?  I tried 
 Google App Engine, but it requires Python 2.5 and won't work with 2.7.
AppEngine does support Python 2.7, see 
https://developers.google.com/appengine/docs/python/runtime
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: mini browser with python

2012-12-05 Thread Miki Tebeka
 In other words I need a mini, simple browser; 
 something I can build that will open, read and 
 display a saved html file.
If you want to view the raw HTML, use any editor.
If you want to view the rendered HTML (like in a browser), you can point your 
favorite browser to a local file or use something like Qt+Webkit.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: mini browser with python

2012-12-05 Thread Hans Mulder
On 5/12/12 20:36:04, inq1ltd wrote:
 Python help.

?This is not a Python question.

 I can connect to and download a web page,
 html code, and save it to a file. If connected
 to the web, I can use KWrite to open the file
 and navigate the page.

 I want to view the html file without using a browser
 or KWrite as I do now.

 In other words I need a mini, simple browser;
 something I can build that will open, read and
 display a saved html file.

Why don't you use the browser you already know?

 I would appreciate some direction.

You're more likely to get good recommendations on
a forum dedicated to whichever OS you are using.



Hope this help,

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


Re: mini browser with python

2012-12-05 Thread Grant Edwards
On 2012-12-05, Hans Mulder han...@xs4all.nl wrote:
 On 5/12/12 20:36:04, inq1ltd wrote:
 Python help.

 ? This is not a Python question.

It sounds to me lik he's asking about using Python to render HTML and
display the result.  Back when I used Scheme/Tk for knocking out quick
GUI apps, there was an HTML widget that allowed you to do that.
Does Tkinter have something like that?

-- 
Grant Edwards   grant.b.edwardsYow! This ASEXUAL PIG
  at   really BOILS my BLOOD
  gmail.com... He's so ... so
   ... URGENT!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: mini browser with python

2012-12-05 Thread inq1ltd
On Wednesday, December 05, 2012 03:36:04 PM David 
Hutto wrote:
 I think this is the snippe that you want:
 
 david@david-desktop:~$ python
 Python 2.7.3 (default, Aug  1 2012, 05:16:07)
 [GCC 4.6.3] on linux2
 Type help, copyright, credits or license for more 
information.
 
  import urlliib
  url_to_read = 
urllib.urlopen('http://hitwebdevelopment.com')
 
  for line in url_to_read:
 ... print line

Print line will send the html code to my
sys.stdout
I need to view the code as if
I were opening it with a browser.

Now, I set KWrite to read the code just
as Firefox does. 
I want to incorporate a mini browser 
in a module to do the same.

regards,
jimonlinux








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


Re: why does dead code costs time?

2012-12-05 Thread Bruno Dupuis
I added a patch on the issue tracker. It solves the bug for short
(32700 bytes) functions

ref : http://bugs.python.org/file28217/16619-1.patch

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


Re: mini browser with python

2012-12-05 Thread Dave Angel
On 12/05/2012 04:19 PM, inq1ltd wrote:
 SNIP

 I need to view the code as if
 I were opening it with a browser.

 Now, I set KWrite to read the code just
 as Firefox does. 
 I want to incorporate a mini browser 
 in a module to do the same.

 regards,
 jimonlinux





You still haven't given any constraints.  How about launching Firefox
on  file://mydir/temp/output.html  ?

Are you using a gui already, so this should display inside a window of
your program?  What OS are you using? Which version of Python, and which
gui?

If you haven't picked a GUI, have you looked into Miki's suggestion,  
Qt + webkit ?

-- 

DaveA

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


Re: mini browser with python

2012-12-05 Thread inq1ltd
On Wednesday, December 05, 2012 10:15:58 PM Hans 
Mulder wrote:
 On 5/12/12 20:36:04, inq1ltd wrote:
  Python help.
 
 ?This is not a Python question.
 
  I can connect to and download a web page,
  html code, and save it to a file. If connected
  to the web, I can use KWrite to open the file
  and navigate the page.
  
  I want to view the html file without using a browser
  or KWrite as I do now.
  
  In other words I need a mini, simple browser;
  something I can build that will open, read and
  display a saved html file.
 
 Why don't you use the browser you already know?
 
  I would appreciate some direction.
 
 You're more likely to get good recommendations on
 a forum dedicated to whichever OS you are using.
If this is not a Python question what is
the purpose of the htmllib module, or the sgmllib module,
or the formatter module, or the htmlentitydef module or
the HTMLParser module and others that are available 
for python??

regards,

jimonlinux











 
 
 
 Hope this help,
 
 -- HansM-- 
http://mail.python.org/mailman/listinfo/python-list


mini browser with python

2012-12-05 Thread inq1ltd
Python help.

I can connect to and download a web page,
html code, and save it to a file.  If connected 
to the web, I can change the settings on  KWrite 
to open the file and navigate the page, 
 (just like a browser does).

I want to view the html file without using a browser 
or KWrite as I do now. 

Customer wants a direct connect to a dedicated 
website.  Their clients can be using any 
browser. This gives them a a direct connect to 
a dedicated website for a specific purpose.
  
In other words I need a mini, simple browser; 
something I can build that will open, read and 
display a saved html or the connected url site.

I would appreciate some direction.



jimonlinux

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


Re: problem with usbtmc-communication

2012-12-05 Thread wrw

On Dec 5, 2012, at 3:38 PM, Jean Dubois jeandubois...@gmail.com wrote:

[byte]

 
 I note that in your Octave example you are reading characters rather than 
 lines.  It seems to me that you have two choices here, either do the 
 equivalent in python or dig through the Keithley documentation to find the 
 hex codes that usbtmc is presumably translating your commands into.  If you 
 can find those, and if you are interested, I can send you off-line the 
 handler I wrote a couple of years ago that used a dictionary to translate 
 English commands into hex, then assembled those with a checksum and sent the 
 string out to a Keyspan usb to serial converter.
 
 If you could show me how to do the equivalent in Python I'd
 appreciate that very much
 
 best regards,
 jean
 -- 
 http://mail.python.org/mailman/listinfo/python-list

OK - I've excerpted some of the relevant code (the original was much longer and 
included a lot of error checking).  Please understand that the comments were 
for my own use, this was never intended to see the light of day (as it were).  
Also, the structure grew from a situation where I only had to worry about a 
single controller model - single relay model to one where I had to be able to 
deal with two controllers and two relay models.  This was all python 2.7 of 
course.

cut on dotted line..

 serial_port = /dev/tty.KeySerial1, 2, 3, etc.
 X10_controller = 1132B  or TI103
 Relay_model = UM506  or RBn04
 Relay_address = B2


import serial, string

def checksum256(st):
temp = reduce(lambda x,y:x+y, map(ord,st)) % 256
if temp  9:
hex_temp = '0'+str(temp)
return hex_temp
hex_temp = hex(temp).upper()[2:]
return hex_temp

letr_address_dict = {'A':'\x46', 'B':'\x4E', 'C':'\x42', 'D':'\x4A', 
'E':'\x41', 'F':'\x49', 'G':'\x45', 'H':'\x4D', 'I':'\x47',  'J':'\x4F',  
'K':'\x43',  'L':'\x4B',  'M':'\x40',  'N':'\x48',  'O':'\x44',  'P':'\x4C' }
numb_address_dict = {'1':'\x4C', '2':'\x5C', '3':'\x44', '4':'\x54', 
'5':'\x42', '6':'\x52', '7':'\x4A', '8':'\x5A', '9':'\x4E', '10':'\x5E', 
'11':'\x46', '12':'\x56', '13':'\x40', '14':'\x50', '15':'\x48', '16':'\x58' }
cmd_dict =  {'SoC':'\x63', 'All_Units_Off':'\x41', 
'All_Lights_On':'\x43', 'ON':'\x45', 'OFF':'\x47', 'Dim':'\x49', 
'Bright':'\x4B', 'All_Lights_Off':'\x4D', 'Rep_Cnt1':'\x41', 'Rep_Cnt2':'\x42'}

def relay(port, controller_model, relay_model, relay_address, command):
if controller_model == '1132B':
if relay_model == 'UM506' or relay_model == 'UM7206':
letr = letr_address_dict[relay_address[0]]
numb = numb_address_dict[relay_address[1]]
cmd = cmd_dict[command]
cmd_string = '\x63'+letr+numb+cmd+'\x42' # Start-of-Command + 
address_letter + address_number + command + Rep-count
ser = serial.Serial(port, 9600, timeout=1)   # Set up handle to 
serial port
stat1 = ser.write('\x02')# Write attention to 
PowerLink, stat = number of bytes written, not really an error return.
ack1 = ser.read(2)   # Check to see if 
PowerLink is ready
if ack1 == '\x06\r': # It returns ACKCR 
(\x06\r) if it is
stat2 = ser.write(cmd_string)
ack2 = ser.read(19)
if command == 'ON' and ack2 == 'XN\\1\rXNE1\rXNE1\r' : status = 0
if command == 'OFF' and ack2 == 'XN\\1\rXNG1\rXNG1\r': status = 0
if command == 'ON' and ack2 != 'XN\\1\rXNE1\rXNE1\r' : status = 
1
if command == 'OFF' and ack2 != 'XN\\1\rXNG1\rXNG1\r': status = 
1
elif ack1 =='\x15':   # PowerLink sends NAC 
(hex 15) if it isn't.
print('Received NAK from X10 Control, is there too much X10 
traffic on the line?\n')
else:   print(Something's wrong with X10 control. Ack returned 
was:  + ack1 + \n)
stat3 = ser.close()  # Close serial port
return(status)
 
-
some irrelevant stuff was here, snipped
-
elif controller_model == 'TI103':
if relay_model == 'UM506' or relay_model == 'UM7206':
letr = relay_address[0]
numb = relay_address[1]
if int(relay_address[1]) = 9:   numb = '0'+numb

#   stat1 = ser.write('$28001B02B02 BONBONCC#') # Tell TI103 to send 
On to device B2

ser = serial.Serial(port, 9600, timeout=0.1)   # Set up handle to 
serial port

cmd_string = '$28001'+letr+numb+letr+numb+' 
'+letr+command+letr+command
ck_sum = checksum256(cmd_string)
cmd_string = cmd_string+ck_sum+'#'

stat2 = ser.write(cmd_string)
ack2 = ser.read(10)
if ack2 != '$2800!4B#': print('Problem writing command string, 
controller ACK =', ack2)  # $2800!4B# == success

stat3 = ser.close() 

urlopen in python3

2012-12-05 Thread Olive
In python2, I use this code:

a=urllib.urlopen(something)

In python2, this work if something is a regular file on the system as
well as a remote URL. The 2to3 script convert this
to urllib.request.urlopen. But it does not work anymore if something
is just a file name. 

My aim is to let the user specify a file on the command line and have
something that works, whatever the file  actually is: a regular file,
an http url, etc...

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


Confused compare function :)

2012-12-05 Thread Anatoli Hristov
Hi all,

I'm confused again with a compare update function. The problem is that
my function does not work at all and I don't get it where it comes
from.

in my DB I have total of 754 products. when I run the function is says:
Total updated: 754
Total not found with in the distributor: 747
I just don't get it, can you find my  mistake ?

Thanks in advance

def Change_price():
total = 0
tnf = 0
for row in DB: # DB is mySQL DB, logically I get out 1 SKU and I
compare it with next loop
isku = row[sku]
isku = isku.lower()
iprice = row[price]
iprice = int(iprice)
found = 0
try:
for x in PRICELIST:# here is my next loop in a CSV file
which is allready in a list PRICELIST
try:
dprice = x[6]
dprice = dprice.replace(,,.) # As in the
PRICELIST the prices are with commas I replace the comma as python
request it
dprice = float(dprice)
newprice = round(dprice)*1.10
dsku = x[4]
dsku = dsku.lower()
stock = int(x[7])
if isku == dsku and newprice  int(iprice):# If
found the coresponded SKU and the price is higher than the one in the
CSV I update the price
print dsku, x[6], dprice, newprice
Update_SQL(newprice, isku)# goes to the SQL Update
print isku, newprice
if isku == dsku:# Just a check to see if it works
print Found %s %dsku
found = 1
else:
found = 0
except IndexError:
pass
except ValueError:
pass
except TypeError:
pass
except IndexError:
pass
if found == 1:
print %s This is match % isku
if found == 0:
print %s Not found % isku
tnf = tnf +1
total = total +1
print Total updated: %s % total
printTotal not found with in the distributor: %s % tnf
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: urlopen in python3

2012-12-05 Thread Nick Cash
 In python2, this work if something is a regular file on the system as
 well as a remote URL. The 2to3 script convert this to
 urllib.request.urlopen. But it does not work anymore if something
 is just a file name.
 
 My aim is to let the user specify a file on the command line and have
 something that works, whatever the file  actually is: a regular file,
 an http url, etc...

A file path, such as /etc/passwd, isn't properly a URL, so urllib correctly 
refuses to handle it. You can make it a URL by using the file:// protocol, i.e. 
file:///etc/passwd... which appears to work in both python2 and python3.

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


Re: mini browser with python

2012-12-05 Thread John Gordon
In mailman.532.1354745770.29569.python-l...@python.org inq1ltd 
inq1...@inqvista.com writes:

 In other words I need a mini, simple browser; 
 something I can build that will open, read and 
 display a saved html or the connected url site.

What will be the output of this mini browser?  Plain text?  Rendered
graphics?  An audible screen reader?  Something else?

What web standards does this mini browser need to support?  Full HTML5?
Partial HTML?  CSS?  Javascript?  Flash?

-- 
John Gordon   A is for Amy, who fell down the stairs
gor...@panix.com  B is for Basil, assaulted by bears
-- Edward Gorey, The Gashlycrumb Tinies

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


z-buffer mlab

2012-12-05 Thread Jan Kurzawski
Hi Guys!

I am plotting an object in mlab (isoSurface) and then I am adding 2 
image_plane_widgets. Till now everything is ok but when I am  changing the 
opacity of the object to lower than 1 it is suddenly appearing behind the 
planes? Why so? Is it dependant on the order of adding objects? But i cant add 
image_plane without having a surface

I would be very gratefull for help.
-- 
http://mail.python.org/mailman/listinfo/python-list


Installing packages on Mac OS X 10.7.5

2012-12-05 Thread John Dildy
Hello Everyone!

I have python v2.7.1 and I am trying to install packages on the Mac OS X v10.7.5

I am trying to install:

Distribute

Nose

virtualenv

If anyone can help me that would be great

John Dildy

jdild...@gmail.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Remote server: running a Python script and making *.csv files publicly available

2012-12-05 Thread Michael Torrie
On 12/05/2012 12:14 PM, Jason Hsu wrote:
 I have a Python 2.7 script that produces *.csv files.  I'd like to
 run this Python script on a remote server and make the *.csv files
 publicly available to read.
 
 Can this be done on Heroku?  I've gone through the tutorial, but it
 seems to be geared towards people who want to create a whole web
 site.
 
 If Heroku isn't the solution for me, what are the alternatives?  I
 tried Google App Engine, but it requires Python 2.5 and won't work
 with 2.7.

Can you just write the files in a directory and then have apache
configured to serve this directory up over http? That way Python needn't
worry about the details of http serving.  Other mini web servers would
work too like lighttpd or uhttpd.

Alternatively you could use Python-Twisted to implement a simple web
server that serves up the CSV.

Using a full framework is overkill, in my opinion.  I'm not sure what
Heroku (first I've heard of it) is, but I don't think you need that.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: urlopen in python3

2012-12-05 Thread Olive
Nick Cash nick.c...@npcinternational.com wrote:
  In python2, this work if something is a regular file on the
  system as well as a remote URL. The 2to3 script convert this to
  urllib.request.urlopen. But it does not work anymore if something
  is just a file name.
  
  My aim is to let the user specify a file on the command line and
  have something that works, whatever the file  actually is: a
  regular file, an http url, etc...
 
 A file path, such as /etc/passwd, isn't properly a URL, so urllib
 correctly refuses to handle it. You can make it a URL by using the
 file:// protocol, i.e. file:///etc/passwd... which appears to work
 in both python2 and python3.
 


That's true a file path is not an URL, yet the python2 behaviour was
handy. I do not know in advance if it is a file or an URL, so 
what's the best way to hadle the case? I imagine someling like:

if os.path.exists(something):
something=file://+os.path.abspath(something)
a=urllib.request.urlopen(something)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Installing packages on Mac OS X 10.7.5

2012-12-05 Thread Irmen de Jong
On 6-12-2012 0:12, John Dildy wrote:
 Hello Everyone!
 
 I have python v2.7.1 and I am trying to install packages on the Mac OS X 
 v10.7.5
 
 I am trying to install:
 
 Distribute
 
 Nose
 
 virtualenv
 
 If anyone can help me that would be great
 
 John Dildy
 
 jdild...@gmail.com
 


Avoid changing stuff on the system installed python.
If you don't have virtualenv already, I would suggest to either:

- install virtualenv by means of easy_install (which should be installed 
already)
- do everything else in a virtual env, instead of in the system installed 
python directly

Or install homebrew, then brew install python, and use that. This avoids using 
the
system installed python entirely.


Irmen

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


Re: Confused compare function :)

2012-12-05 Thread Bruno Dupuis
On Wed, Dec 05, 2012 at 11:50:49PM +0100, Anatoli Hristov wrote:
 I'm confused again with a compare update function. The problem is that
 my function does not work at all and I don't get it where it comes
 from.
 
 in my DB I have total of 754 products. when I run the function is says:
 Total updated: 754
 Total not found with in the distributor: 747
 I just don't get it, can you find my  mistake ?
 
 Thanks in advance
 
 def Change_price():
 total = 0
 tnf = 0
 for row in DB: # DB is mySQL DB, logically I get out 1 SKU and I
 compare it with next loop
 isku = row[sku]
 isku = isku.lower()
 iprice = row[price]
 iprice = int(iprice)
 found = 0
 try:
 for x in PRICELIST:# here is my next loop in a CSV file
 which is allready in a list PRICELIST
 try:
 dprice = x[6]
 dprice = dprice.replace(,,.) # As in the
 PRICELIST the prices are with commas I replace the comma as python
 request it
 dprice = float(dprice)
 newprice = round(dprice)*1.10
 dsku = x[4]
 dsku = dsku.lower()
 stock = int(x[7])
 if isku == dsku and newprice  int(iprice):# If
 found the coresponded SKU and the price is higher than the one in the
 CSV I update the price
 print dsku, x[6], dprice, newprice
 Update_SQL(newprice, isku)# goes to the SQL Update
 print isku, newprice
 if isku == dsku:# Just a check to see if it works
 print Found %s %dsku
 found = 1
 else:
 found = 0
 except IndexError:
 pass
 except ValueError:
 pass
 except TypeError:
 pass
 except IndexError:
 pass
 if found == 1:
 print %s This is match % isku
 if found == 0:
 print %s Not found % isku
 tnf = tnf +1
 total = total +1
 print Total updated: %s % total
 printTotal not found with in the distributor: %s % tnf

I tried, I swear I did try, I didn't understand the whole algorithm of the
function. However, in a first sight, I find it way to deeply nested.
def ... for ... try ... for ... if ... if. Can't you split it in several
function, or in methods of a callable class? Somtimes it's finally much
more clear and the errors become obvious. Another advice: never ever

except XXXError:
pass

at least log, or count, or warn, or anything, but don't pass. I bet your
missing products have disapeared into those black holes. 

mmmh, now, i see that you set found to 1 only if newprice int(iprice)...
new_price is a float (newprice = round(dprice)*1.10) that you compare with
an int? is that correct? seems strangee to me.

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


Re: Confused compare function :)

2012-12-05 Thread Steven D'Aprano
On Wed, 05 Dec 2012 23:50:49 +0100, Anatoli Hristov wrote:


 def Change_price():

Misleading function name. What price does it change?


 total = 0
 tnf = 0

tnf? Does that mean something?


 for row in DB:  # DB is mySQL DB, logically I get out 
 # 1 SKU and I compare it with next loop

Use of global variables, yuck. What happens if some day you need two 
databases at the same time?


 isku = row[sku]
 isku = isku.lower()

Hungarian Notation? This is better written as:

sku = row[sku].lower()


 iprice = row[price]
 iprice = int(iprice)

And likewise price = int(row[price]). Or better still, oldprice, or 
price_in_database, or something that actually describes what it is.


 found = 0

found = False


 try:
 for x in PRICELIST:  # here is my next loop in a CSV file
  # which is allready in a list PRICELIST
 try:
 dprice = x[6]

dprice? D-for-database price? But this is the price from the CSV file, 
not from the database. Another misleading name, leading to confusion.


 dprice = dprice.replace(,,.)
 # As in the PRICELIST the prices are with 
 # commas I replace the comma as python request it
 dprice = float(dprice)
 newprice = round(dprice)*1.10
 dsku = x[4]
 dsku = dsku.lower()

And again, what's dsku mean? Database-SKU? But it's the CSV SKU.


 stock = int(x[7])

I don't believe that this is used at all. Get rid of it.


 if isku == dsku and newprice  int(iprice):
 # If found the coresponded SKU and the price is
 # higher than the one in the CSV I update the price

I think your logic is wrong here. You aren't comparing the price in the 
CSV here at all. You compare two prices, neither of which is the price in 
the CSV file:

newprice = round(price in CSV) * 1.10
iprice = price from the database

(which is already an int, no need to call int *again* -- if you're going 
to use Hungarian Notation, pay attention to it!)

So you have THREE prices, not two, and it isn't clear which ones you are 
*supposed* to compare. Either the code is wrong, or the comment is wrong, 
or possibly both.

iprice = price from the database
dprice = price from the CSV
newprice = calculated new price

I'm going to GUESS that you actually want to compare the new price with 
the price in the database.

if newprice  iprice:  # horrible name! no wonder you are confused
# Update the database with the new price


 print dsku, x[6], dprice, newprice
 Update_SQL(newprice, isku)
 # goes to the SQL Update

Really? Gosh, without the comment, how would anyone know that Update_SQL 
updates the SQL? :-P

Seriously, the comment is redundant. Get rid of it.


 print isku, newprice
 if isku == dsku:
 # Just a check to see if it works
 print Found %s %dsku
 found = 1
 else:
 found = 0

found = True or False is better.

But this code cannot do anything but print Found, since above you already 
tested that isku == dsku. So this check is pointless.

The reason is, your code does this:

if isku == dsku and (something else):
# Inside this block, isku MUST equal dsku
blah blah blah
if isku == dsku:
print Found
found = 1
else:
# But this cannot possibly happen
print not found
found = 0


 except IndexError:
 pass
 except ValueError:
 pass
 except TypeError:
 pass

Why are you hiding errors? You should not hide errors unnecessarily, that 
means there are bugs in either the CSV or your code, you should fix the 
bugs.

However, if you really must, then you can replace all of those with:

  except (IndexError, ValueError, TypeError): 
  pass


 except IndexError:
 pass

And hiding more errors?


 if found == 1:
 print %s This is match % isku
 if found == 0:
 print %s Not found % isku
 tnf = tnf +1
 total = total +1

Better to write this as:

if found:
   print %s This is match % isku
else:
   print %s Not found % isku
   tnf = tnf + 1  # What does this mean?
total += 1


 print Total updated: %s % total

That's wrong. total is *not* the number updated. It is the total, updated 
or not updated. This should say:

print Total records inspected: %s % total


 printTotal not found with in the distributor: %s % tnf

Ah-ha! It means Total Not Found! I shouldn't have 

Re: Confused compare function :)

2012-12-05 Thread Steven D'Aprano
On Thu, 06 Dec 2012 01:19:58 +0100, Bruno Dupuis wrote:

 I tried, I swear I did try, I didn't understand the whole algorithm of
 the function. However, in a first sight, I find it way to deeply nested.

Yes!

But basically, the code seems to run a pair of nested for-loops:

for SKU in database:
for SKU in csv file:
if the two SKUs match:
compare their prices and update the database



 def ... for ... try ... for ... if ... if. 

You missed a second try.

 Can't you split it in several
 function, or in methods of a callable class? Somtimes it's finally much
 more clear and the errors become obvious. Another advice: never ever
 
 except XXXError:
 pass
 
 at least log, or count, or warn, or anything, but don't pass. I bet your
 missing products have disapeared into those black holes.

I think that never is too strong, but otherwise I agree with you.


 mmmh, now, i see that you set found to 1 only if newprice
 int(iprice)... new_price is a float (newprice = round(dprice)*1.10)
 that you compare with an int? is that correct? seems strangee to me.


There's nothing wrong with comparing floats to ints.

However, possibly iprice is not intended to be an int. The code is rather 
confused and the names are at best obscure and at worst actively 
misleading. I assumed that prices must be ints, (iprice means integer 
price?) but that might not be the case, since later on another price is 
multiplied by 1.10.

Also I wonder if the code is meant to calculate the new price:

newprice = round(dprice)*1.10  # dprice is the price in the CSV file!

or perhaps it is meant to be:

newprice = int(round(dprice*1.10))


That seems likely.


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


Re: mini browser with python

2012-12-05 Thread Hans Mulder
On 5/12/12 22:44:21, inq1ltd wrote:

 I can connect to and download a web page,
 html code, and save it to a file. If connected
 to the web, I can change the settings on KWrite
 to open the file and navigate the page,
 (just like a browser does).

 I want to view the html file without using a browser

 or KWrite as I do now.

 Customer wants a direct connect to a dedicated
 website. Their clients can be using any
 browser. This gives them a a direct connect to
 a dedicated website for a specific purpose.
 In other words I need a mini, simple browser;
 something I can build that will open, read and
 display a saved html or the connected url site.

How about:

import os.path, webbrowser

webbrowser.open(file:// + os.path.abspath(your_file))

 I would appreciate some direction.

You'd get more useful answers if you'd give some
more context.  For example:

* Which platform?
* Which version of Python?
* Which GUI (if any)?
* What capabilities do you need?  CSS? Javascript?
* Are there any unusual requirement?


Hope this helps,

-- HansM


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


Re: Installing packages on Mac OS X 10.7.5

2012-12-05 Thread Hans Mulder
On 6/12/12 00:56:55, Irmen de Jong wrote:
 On 6-12-2012 0:12, John Dildy wrote:
 I have python v2.7.1 and I am trying to install packages on the Mac OS X 
 v10.7.5
 I am trying to install:
 Distribute
 Nose
 virtualenv
 If anyone can help me that would be great
 
 Avoid changing stuff on the system installed python.

+1

 If you don't have virtualenv already, I would suggest to either:
 
 - install virtualenv by means of easy_install (which should be installed 
 already)
 - do everything else in a virtual env, instead of in the system installed 
 python directly
 
 Or install homebrew, then brew install python, and use that. This avoids 
 using the
 system installed python entirely.

Or simply download Python 2.7.3 from python.org and install that.

The Mac OSX installers from python.org leave the system Python
alone and install a separate copy of Python under /Local/.

Or you could use MacPorts, which will install into /opt/local/.

Or you could get really fancy, and install a Python from python.org
or homebrew or MacPorts, and then user virtualenv to avoid messing up
that Python.

The one thing you want to avoid is messing with the system Python.


Hope this helps,

-- HansM

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


Re: Secretly passing parameter to function

2012-12-05 Thread Modulok
 Hi all !

 I have a problem that is not easy to explained, so I have tried to
 reduce it a lot.

 We are using a framework, that we can not modify.

 in framework.py:
 def do(something):
 '''
 Here we are in a framework that can not be modified ...
 It does a lot of things

 and finally:
 '''
 something()

 in test.py:
 from framework import *

 def step1():
 print Do step1

 def step2():
 print Do step2


 # We ask the framework to do some work.
 do(step1)
 do(step2)
 do(step3)


 We are writing step1, step2, ... and asking the framework to process them.
 Everything is ok, until we want to add a parameter to some steps.
 We want to be able to do that:

 in test.py:
 from framework import *

 def step1(param):
 print Do step1 with param

 def step2():
 print Do step2


 # We ask the framework to do some work.

 do(step1, param = None)
 do(step1, param = [0, 1, 5]) # again
 do(step2)

 Of course it does not work ...
 TypeError: do() takes exactly 1 argument (2 given)

 And we can not modify the framework (in which do is defined.

 One solution would be to use a global variable that can be set before
 each step. But it is not very elegant ...

 One other approach would be to add dynamically an attribute the the
 step1 function, and retrieve it inside the function, but it is perhaps
 overkill.

 Do you have some ideas ?

Olivier,

I would create a partial object using the functools module, but I would also
wrap it in a decorator so I could call my functions as usual. Here's an
example:


# File: framework.py:
def do(something):
print(Framework in action...)
return something()



# File: test.py:
import functools
import framework

def pack(func):
Return a function object to be called later.
def f(*args, **kwargs):
Call the framework passing a partial object to be called.
print(Wrapper in action...)
part = functools.partial(func, *args, **kwargs)
return framework.do(part)   #-- Call the simplified function.
return f#-- Return the function object to-be-called.


# Usage: Just wrap your defs with the decorator '@pack':
@pack
def step1(x, y):
print(x, y)

@pack
def step2(a):
return sum(a)

@pack
def step3():
print(Amazing!)


# Call your functions as usual e.g: step1(3, 5)...


In theory everything should just work. I tested the above example and it seemed
to work just fine with my limited testing.

Good luck!
-Modulok-
-- 
http://mail.python.org/mailman/listinfo/python-list


Dict comprehension help

2012-12-05 Thread Joseph L. Casale
I get a list of dicts as output from a source I need to then extract various 
dicts
out of. I can easily extract the dict of choice based on it containing a key 
with
a certain value using list comp but I was hoping to use dict comp so the output
was not contained within a list.

reduce(lambda x,y: dict(x.items() + y.items()), filter(lambda z: z['key'] == 
value, my_list))

where my_list is a list of dicts. The premise is all dicts in the list have a 
unique
value for z['key'].

Anyone have a pointer as to how I might tackle this without lambdas and only
using dict comp?

Thanks!
jlc
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need porting of German Wotan AI into Python

2012-12-05 Thread alex23
On 6 Dec, 04:38, Mentifex menti...@myuw.net wrote:
 There are no funds available for payment but
 mega kudos will accrue

I'm sure that'll keep my kids well fed.

Not commenting on this link, just including it here for the
interested:

http://www.nothingisreal.com/mentifex_faq.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Confused compare function :)

2012-12-05 Thread Rotwang

On 06/12/2012 00:19, Bruno Dupuis wrote:

[...]

Another advice: never ever

except XXXError:
 pass

at least log, or count, or warn, or anything, but don't pass.


Really? I've used that kind of thing several times in my code. For 
example, there's a point where I have a list of strings and I want to 
create a list of those ints that are represented in string form in my 
list, so I do this:


listofints = []
for k in listofstrings:
try:
listofints.append(int(k))
except ValueError:
pass

Another example: I have a dialog box with an entry field where the user 
can specify a colour by entering a string, and a preview box showing the 
colour. I want the preview to automatically update when the user has 
finished entering a valid colour string, so whenever the entry field is 
modified I call this:


def preview(*args):
try:
previewbox.config(bg = str(entryfield.get()))
except tk.TclError:
pass

Is there a problem with either of the above? If so, what should I do 
instead?



--
I have made a thing that superficially resembles music:

http://soundcloud.com/eroneity/we-berated-our-own-crapiness
--
http://mail.python.org/mailman/listinfo/python-list


Re: Dict comprehension help

2012-12-05 Thread Ian Kelly
On Wed, Dec 5, 2012 at 8:03 PM, Joseph L. Casale
jcas...@activenetwerx.com wrote:
 I get a list of dicts as output from a source I need to then extract various 
 dicts
 out of. I can easily extract the dict of choice based on it containing a key 
 with
 a certain value using list comp but I was hoping to use dict comp so the 
 output
 was not contained within a list.

 reduce(lambda x,y: dict(x.items() + y.items()), filter(lambda z: z['key'] == 
 value, my_list))

 where my_list is a list of dicts. The premise is all dicts in the list have a 
 unique
 value for z['key'].

 Anyone have a pointer as to how I might tackle this without lambdas and only
 using dict comp?

{k: v for d in my_list if d['key'] == value for (k, v) in d.items()}

However, since you say that all dicts have a unique value for
z['key'], you should never need to actually merge two dicts, correct?
In that case, why not just use a plain for loop to search for the
dict?

for d in my_list:
if d['key'] == value:
result = d
break
else:
raise ValueError(Value not found!)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Confused compare function :)

2012-12-05 Thread Steven D'Aprano
On Thu, 06 Dec 2012 03:22:53 +, Rotwang wrote:

 On 06/12/2012 00:19, Bruno Dupuis wrote:
 [...]

 Another advice: never ever

 except XXXError:
  pass

 at least log, or count, or warn, or anything, but don't pass.
 
 Really? I've used that kind of thing several times in my code. For
 example, there's a point where I have a list of strings and I want to
 create a list of those ints that are represented in string form in my
 list, so I do this:
 
 listofints = []
 for k in listofstrings:
   try:
   listofints.append(int(k))
   except ValueError:
   pass
 
 Another example: I have a dialog box with an entry field where the user
 can specify a colour by entering a string, and a preview box showing the
 colour. I want the preview to automatically update when the user has
 finished entering a valid colour string, so whenever the entry field is
 modified I call this:
 
 def preview(*args):
   try:
   previewbox.config(bg = str(entryfield.get()))
   except tk.TclError:
   pass
 
 Is there a problem with either of the above? If so, what should I do
 instead?

They're fine.

Never, ever say that people should never, ever do something.


*cough*


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


RE: Dict comprehension help

2012-12-05 Thread Joseph L. Casale
 {k: v for d in my_list if d['key'] == value for (k, v) in d.items()}

Ugh, had part of that backwards:) Nice!

 However, since you say that all dicts have a unique value for
 z['key'], you should never need to actually merge two dicts, correct?
 In that case, why not just use a plain for loop to search for the
 dict?

The reason is that I need several of these in an init function and they
stack up nicely where as the loops get unruly in length, but I don't
disagree.
-- 
http://mail.python.org/mailman/listinfo/python-list


question about importing a package

2012-12-05 Thread Matt
I have a directory structure that looks like this:

sample.py
sub_one/
  __init__.py # defines only the list__all__ = ['foo', 'bar']
  foo.py   # defines the function in_foo()  
  bar.py   # defines the function in_bar()

In sample.py, I have this command at the top:

from sub_one import *

I can't refer to in_foo() and in_bar() without prefacing them with the module 
names. I.e. foo.in_foo() and bar.in_bar() work, but I want to import them in 
the __main__ namespace of sample.py and refer to them as just in_foo() and 
in_bar(). I know this is frowned upon, but for my purposes it is the best 
choice. I have about 30 modules in my package (foos and bars) and I don't want 
30 lines at the top of each file that uses this package. What am I doing wrong?

Thanks,

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


Re: How does one make argparse print usage when no options are provided on the command line?

2012-12-05 Thread Terry Reedy

On 12/5/2012 7:48 PM, rh wrote:

On Wed, 5 Dec 2012 18:42:37 +0100
Bruno Dupuis python.ml.bruno.dup...@lisael.org wrote:


On Wed, Dec 05, 2012 at 08:48:30AM -0800, rh wrote:

I have argparse working with one exception. I wanted the program to
print out usage when no command line options are given. But I only
came across other examples where people didn't use argparse but
instead printed out a separate usage statement. So they used
argparse for everything but the case where no command line args are
given.



this is quite raw, but i'd add

import sys
if len(sys.argv) == 1:
 sys.argv.append('-h')


This works too. I guess I like the print_usage() method better.

Being new to python I have noticed that I had copied a bit of code that did

if len(sys.argv[1:]) == 0:


This needlessly creates and tosses a new object.


You did this:
if len(sys.argv) == 1:


This does not.


The other reply did this:
if len(sys.argv) = 1:


This allows for the possibility that len(sys.argv) == 0. However, that 
can (according to the doc) only happen when starting the interpreter 
interactively without a script. Since that does not apply to code within 
a .py file, I prefer == 1.


argv[0] is the script name (it is operating system dependent whether 
this is a full pathname or not). If the command was executed using the 
-c command line option to the interpreter, argv[0] is set to the string 
'-c'. If no script name was passed to the Python interpreter, argv[0] is 
the empty string.


--
Terry Jan Reedy

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


Re: question about importing a package

2012-12-05 Thread Chris Angelico
On Thu, Dec 6, 2012 at 3:58 PM, Matt m...@woodridgeadvisors.com wrote:
 I have about 30 modules in my package (foos and bars) and I don't want 30 
 lines at the top of each file that uses this package. What am I doing wrong?

Not necessarily wrong, but definitely something to query: WHY do you
have thirty modules in your package? How big are your source files -
could you simply merge them into a single module?

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


Re: why does dead code costs time?

2012-12-05 Thread Tim Roberts
Bruno Dupuis python.ml.bruno.dup...@lisael.org wrote:

On Wed, Dec 05, 2012 at 05:40:51PM +0100, Bruno Dupuis wrote:
 
 Good point! I didn't even noticed that. It's weird... Maybe the
 difference comes from a peehole optim on f which is not possible on g as
 g is to complex.

Neil, you were right, thanks. I patched peehole.c to remove this optim, and
now the figures are the same. I investigate to find out why the latter
function is not optimized the same way (and if it can be, I'll propose a
patch for that)

At the risk of being labeled a prude, please be careful about spelling (and
pronouncing) the whole word peephole.  The word as you have spelled it
here (twice) is a vulgarity.

Now, I'm all in favor of the occasional vulgarity, but if this is a
misunderstanding, you could find yourself as the butt of some awkward jokes
at some future optimization conference...
-- 
Tim Roberts, t...@probo.com
Providenza  Boekelheide, Inc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: question about importing a package

2012-12-05 Thread alex23
On 6 Dec, 14:58, Matt m...@woodridgeadvisors.com wrote:
 I have a directory structure that looks like this:

 sample.py
 sub_one/
       __init__.py     # defines only the list    __all__ = ['foo', 'bar']
       foo.py           # defines the function in_foo()
       bar.py           # defines the function in_bar()

 In sample.py, I have this command at the top:

 from sub_one import *

 What am I doing wrong?

The statement `from sub_one import *` imports from sub_one/
__init__.py, which only imports the two modules into its namespace,
not their contents. What you need to do is bring into __init__.py
everything you want the star-import to pull into your code:

  __init__.py:
from foo import *
from bar import *

  foo.py:
__all__ = [ 'in_foo' ]
def in_foo():
  ...

  bar.py:
__all__ = [ 'in_bar' ]
def in_bar():
  ...

If you structure is like this, you can restrict which items can be
imported within the defining file. If it doesn't make sense to do it
there, remove __all__ and just import directly in the __init__.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: question about importing a package

2012-12-05 Thread Steven D'Aprano
On Wed, 05 Dec 2012 20:58:46 -0800, Matt wrote:

 I have a directory structure that looks like this:
 
 sample.py
 sub_one/
 __init__.py  # defines only the list __all__ = ['foo', 'bar'] 
 foo.py   # defines the function in_foo() 
 bar.py   # defines the function in_bar()
 
 In sample.py, I have this command at the top:
 
 from sub_one import *
 
 I can't refer to in_foo() and in_bar() without prefacing them with the
 module names. I.e. foo.in_foo() and bar.in_bar() work, but I want to
 import them in the __main__ namespace of sample.py and refer to them as
 just in_foo() and in_bar().

Module `sub_one` has two public names, foo and bar, exactly as you 
say. So when you import * from it, you only get two names. Now, you could 
do any of these inside sample.py:

# 1
from sub_one.foo import in_foo
from sub_one.bar import in_bar


# 2
from sub_one import *
in_foo = foo.in_foo
in_bar = bar.in_foo


Or you could turn to sub_one.__init__ and do this:

# 3
__all__ = ['in_foo', 'in_bar']
from foo import in_foo
from bar import in_bar


or any combination of the above.



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


Some help in refining this regex for CSV files

2012-12-05 Thread Oltmans
Hi guys,

I've to deal with CSVs that look like following

CSV (with one header and 3 legit rows where each legit row has 3 columns)

Some info
Date: 12/6/2012
Author: Some guy
Total records: 100

header1, header2, header3
one, two, three
one, Python is great, so are other languages, isn't ?, three
one, two, 'some languages, are realyl beautiful\r\n, I really cannot deny \n 
this \t\t\t fact. \t\t\t\tthis fact alone is amazing'


So inside this CSV, there will always be bad lines like the top 4 (they could 
end up in the beginning, in the middle and even in the last). So above sample, 
csv has 3 legit lines and a header. I want to read those three lines and here 
is a regex that I came up with (which clearly isn't working)

#print line
pattern = r([^\t]+\t|,+)
matches = re.match(pattern, line) 

Do you've any better ideas guys? I will really appreciate all help.
-- 
http://mail.python.org/mailman/listinfo/python-list


[issue16581] define PEP editor in PEP 1

2012-12-05 Thread Chris Jerdonek

Chris Jerdonek added the comment:

Are there any comments on the patch from the PEP 1 authors?  PEP 1 says that I 
should assign this to one of the PEP authors.  Any takers, or is this something 
I can commit on the authors' behalf?

--

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



[issue16123] IDLE - deprecate running without a subprocess

2012-12-05 Thread Andrew Svetlov

Andrew Svetlov added the comment:

I think that usage is not officially supported, so we cannot grant this way 
will work forever.

--

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



[issue15346] Tkinter extention modules have no documentation

2012-12-05 Thread Andrew Svetlov

Andrew Svetlov added the comment:

Patch is welcome!

--

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



[issue16613] ChainMap.new_child could use improvement

2012-12-05 Thread Vinay Sajip

New submission from Vinay Sajip:

ChainMap.new_child could IMO be improved through allowing an optional dict to 
be passed, which is used to create the child. The use case is that you 
sometimes need to temporarily push a new non-empty mapping in front of an 
existing chain. This could be achieved by changing new_child to the following, 
which is backwards-compatible:

def new_child(self, d=None):
'New ChainMap with a new dict followed by all previous maps.'
return self.__class__(d or {}, *self.maps)

--
components: Library (Lib)
messages: 176974
nosy: rhettinger, vinay.sajip
priority: normal
severity: normal
status: open
title: ChainMap.new_child could use improvement
type: enhancement
versions: Python 3.4

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



[issue16612] Integrate Argument Clinic specialized preprocessor into CPython trunk

2012-12-05 Thread Andrew Svetlov

Changes by Andrew Svetlov andrew.svet...@gmail.com:


--
nosy: +asvetlov

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



[issue8865] select.poll is not thread safe

2012-12-05 Thread Andrew Svetlov

Changes by Andrew Svetlov andrew.svet...@gmail.com:


--
nosy: +asvetlov

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



[issue12268] file readline, readlines readall methods can lose data on EINTR

2012-12-05 Thread Andrew Svetlov

Changes by Andrew Svetlov andrew.svet...@gmail.com:


--
nosy: +asvetlov

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



[issue16602] weakref can return an object with 0 refcount

2012-12-05 Thread Andrew Svetlov

Changes by Andrew Svetlov andrew.svet...@gmail.com:


--
nosy: +asvetlov

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



[issue14913] tokenize the source to manage Pdb breakpoints

2012-12-05 Thread Andrew Svetlov

Changes by Andrew Svetlov andrew.svet...@gmail.com:


--
nosy: +asvetlov

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



[issue14728] trace function not set, causing some Pdb commands to fail

2012-12-05 Thread Andrew Svetlov

Changes by Andrew Svetlov andrew.svet...@gmail.com:


--
nosy: +asvetlov

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



[issue14743] on terminating, Pdb debugs itself

2012-12-05 Thread Andrew Svetlov

Changes by Andrew Svetlov andrew.svet...@gmail.com:


--
nosy: +asvetlov

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



[issue16180] cannot quit pdb when there is a syntax error in the debuggee (must kill it)

2012-12-05 Thread Andrew Svetlov

Changes by Andrew Svetlov andrew.svet...@gmail.com:


--
nosy: +asvetlov

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



[issue13044] pdb throws AttributeError at end of debugging session

2012-12-05 Thread Andrew Svetlov

Changes by Andrew Svetlov andrew.svet...@gmail.com:


--
nosy: +asvetlov

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



  1   2   >