Re: Comparisons and sorting of a numeric class....

2015-01-14 Thread Ian Kelly
On Tue, Jan 13, 2015 at 5:12 PM, Andrew Robinson
 wrote:
> So -- even a cursory thought shows that the information could be encoded in
> a very few lines even without an instance of a subclass:
>
> class CAllFalse():
> @classmethod
> def __nonzero__(Kls): return False
>
> class CPartFalse():
> @classmethod
> def __nonzero__(Kls): return False

This doesn't actually work.

>>> class CAllFalse(object):
...   @classmethod
...   def __nonzero__(cls): return False
...
>>> bool(CAllFalse)
True

When Python evaluates the "truthiness" of an object, it looks up the
__nonzero__ method on the object's class, not on the object itself
(same goes for all the other special dunder methods). Since CAllFalse
is itself a class, its own class (or rather, metaclass) is type [1].
Thus there's no point in using @classmethod on one of these special
methods. If you really want to proceed with this, you would have to
override the metaclass and define its __nonzero__ to return False,
like so:

>>> class CAllFalseMeta(type):
... def __nonzero__(self): return False
...
>>> class CAllFalse(object):
... __metaclass__ = CAllFalseMeta
...
>>> bool(CAllFalse)
False

In this code CAllFalse is an instance of CAllFalseMeta, while also
being a class itself.  But I'm not sure what the point of this is,
when you could just make CAllFalse a normal class and create an
instance of it.

[1] Actually, its metaclass is only type if you inherit from object,
which you didn't do, although in Python 2.x it must be done
explicitly. If you don't inherit from object, you get what is known as
a "classic class" instead of a new-style class. There is no reason to
use the old-style classes unless you really know what you are doing.

> class cmp():
> lex=( CAllFalse,  CPartFalse, True )
> @staticmethod
> def __cmp__( left, right ):
> if type(left) == type(right): # advanced statistical compare
> return cmp.lex.index( right ).__cmp__( cmp.lex.index( left ) )
> else: # legacy bool compare...
>return right.__nonzero__().__cmp__( left.__nonzero__() )

Magic methods are generally meant to be used by the interpreter, not
called directly. The Pythonic way to write that last line would have
been:

return cmp(bool(right), bool(left))

(Assuming of course that you rename the class to something that
doesn't shadow the cmp builtin function). This also would have
revealed sooner that your __nonzero__ method isn't actually invoked by
the interpreter.

> A full implementation, that distinguishes between legacy and probabilistic
> interpretations for the math operators could be done in several ways;
> Here's one that I think is reasonably clean.
>
> class RBool(tuple):
> """
> Rich compare bool.
> A tuple which contains a bool, and a relative certainty value.
> Whenever a RBool is compared against a bool or single valued object
> tuple,
> it will default to legacy mode and compare only against the bool
> However, whenever compared against another RBool, or multi-element
> tuple,
> it will do a full rich comparison.
> """
> def __new__(cls, data):return super(RBool,cls).__new__(cls,data)
> def _legacy(self,other):
> 'Resize local data for proper legacy compares as needed.'
> try:
> if len(other)==1: return (self[0],)  # legacy compare in tuples.
> except TypeError: return self[0] # non iterables are a legacy
> compare.
> return tuple(self) # A full rich non-recursive compare is warranted
> def __eq__(self,other):return self._legacy(other) == other
> def __ne__(self,other):return self._legacy(other) != other
> def __lt__(self,other):return self._legacy(other) <  other
> def __le__(self,other):return self._legacy(other) <= other
> def __gt__(self,other):return self._legacy(other) >  other
> def __ge__(self,other):return self._legacy(other) >= other
> # def __cmp__( #TODO:
> # def __nonzero__( #TODO:
>
> AllTrue   = True
> PartTrue  = RBool((False,3))
> Uncertain = RBool((False,2))
> PartFalse = RBool((False,1))
> AllFalse  = RBool((False,0))

This seems fine, but I don't get why you're messing around with
subclassing tuples. Do you really want to have boolean values that are
iterable, indexable, etc. for no reason? Just create a normal class
(or maybe subclass int, to match the normal bool class) and let your
instances contain a tuple instead of being one.
-- 
https://mail.python.org/mailman/listinfo/python-list


Is anyone doing Python 3 bindings for the IUP GUI library?

2015-01-14 Thread Mark Summerfield
Just wondering if anyone is doing Python 3 bindings for the IUP GUI library?
The library is pure C and GUI only (so not a giant framework), and uses native 
controls. It comes with Lua bindings and I believe there are third-party Ruby 
bindings.
http://webserver2.tecgraf.puc-rio.br/iup/
-- 
https://mail.python.org/mailman/listinfo/python-list


"cannot set color attribute.." with Mayavi

2015-01-14 Thread sperelat
Hello.
I use mayavi module to plot  some function on a spherical surface.
The code read data from files and make 3d plot.

This code  worked well when I ran it with ipython interpreter on one machine.

-code
from numpy import *
from mayavi.mlab import *

theta=load('theta_q.npy')
phi=load('phi_q.npy')

spin_q=load("spin_qfunction.npy")





r=3


gap=9

x = r*sin(theta)*cos(phi)   
y = r*sin(theta)*sin(phi)
z = r*cos(theta) 


mesh(x, y, z,scalars=spin_q,colormap='jet')
-

When I tried to run it on another machine, it makes error like below.

"Cannot set the undefined 'color' attribute of a 'Light' object.


I actually don't understand what the problem is.

I just guess  that there was some installation problem.

The machine that works well with the code has Windows  XP as its OS,

and another machine has Windows 7.

PythonXY does not seem to be installed properly on this Windows 7 machine.

Does anyone have idea on  the meaning and the origin   of the error  message ?



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


Re: Performance in exec environnements

2015-01-14 Thread Jean-Baptiste Braun
2015-01-13 22:48 GMT+01:00 Steven D'Aprano <
[email protected]>:

> So you have been comparing:
>
> 2
>
> versus
>
> exec('1+1')
>
>
> The first case just fetches a reference to a pre-existing int object, and
> then deletes the reference. That's fast.
>
> The second case:
>
> - creates a new string '1+1'
> - does a global lookup to find the built-in exec function
> - passes the string to the function
> - the function then parses that string and compiles it to byte-code
> - runs the keyhole optimizer over it
> - and finally executes the byte code for "2", same as above.
>
> Only the last step is the same as your earlier test case.
>
What I don't understand is the ratio between test 2 / 4 and test 1 / 3.

Let 0.0229 sec be the execution time to read a bytecode (1st test).
Executing two times that bytecode takes 0.042 sec (test 3), which looks
coherent.

Let 11.6 sec be the execution time to call the global exec, parse, do some
stuff and read a bytecode (test 2). I'm trying to understand why does doing
the same thing and reading one more bytecode is much longer (15.7 sec) in
comparison.


> Bigger savings come from avoiding exec. Instead, try to use factory
> functions, closures, etc. If you give an example of what you are trying to
> generate with exec, we may be able to offer an alternative.
>
I think I'm going to compile before exec'ing.

What I'm trying to do is to map a transformation description in a markup
langage (XSLT) in python to improve execution time. Here is a
simplification of what it looks like :

XSLT :

 
  
   Mr
  
  
   Mrs
  
 


Generated python :
print('')
if gender == 'M':
print('Mr')
else:
print('Mrs')
print('')
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: List of "python -m" tools

2015-01-14 Thread Albert-Jan Roskam





- Original Message -
> From: Irmen de Jong 
> To: [email protected]
> Cc: 
> Sent: Wednesday, January 14, 2015 12:01 AM
> Subject: Re: List of "python -m" tools
> 
> On 12-1-2015 5:17, Miki Tebeka wrote:
>>  Greetings,
>> 
>>  I've compiled a list of "python -m" tools at 
> pythonwise.blogspot.com/2015/01/python-m.html.
>> 
>>  Did I miss something? What are your favorite "python -m" tools?
>> 
>>  Thanks,
>>  --
>>  Miki
>> 
> 
> 
> python -m calendar
> python -m test.pystone
> python -m antigravity
> 


cool, thanks.

You say "Python 2.7.9 and 3.x comes with an easy way to install pip. Run python 
-m ensurepip and pypi is at your service." . 

But here https://docs.python.org/3/library/ensurepip.html it says that "This 
module does not access the internet. All of the components needed to bootstrap 
pip are included as internal parts of the package." So pypi is not involved, 
right?

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


Re: UnicodeEncodeError: 'ascii' codec can't encode character u'\ua000' in position 0: ordinal not in range(128)

2015-01-14 Thread Dave Angel

On 01/13/2015 10:26 PM, Peng Yu wrote:

Hi,



First, you should always specify your Python version and OS version when 
asking questions here.  Even if you've  been asking questions, many of 
us cannot keep track of everyone's specifics, and need to refer to a 
standard place, the head of the current thread.


I'll assume you're using Python 2.7, on Linux or equivalent.


I am trying to understand what does encode() do. What are the hex
representations of "u" in main.py? Why there is UnicodeEncodeError
when main.py is piped to xxd? Why there is no such error when it is
not piped? Thanks.

~$ cat main.py
#!/usr/bin/env python

u = unichr(40960) + u'abcd' + unichr(1972)
print u


The unicode characters in 'u' must be decoded to a byte stream before 
sent to the standard out device.  How they're decoded depends on the 
device, and what Python knows (or thinks it knows) about it.



~$ cat main_encode.py
#!/usr/bin/env python

u = unichr(40960) + u'abcd' + unichr(1972)
print u.encode('utf-8')


Here, print is trying to send bytes to a byte-device, and doesn't try to 
second guess anything.



$ ./main.py
ꀀabcd޴
~$ cat main.sh
#!/usr/bin/env bash

set -v
./main.py | xxd
./main_encode.py | xxd

~$ ./main.sh
./main.py | xxd
Traceback (most recent call last):
   File "./main.py", line 4, in 
 print u
UnicodeEncodeError: 'ascii' codec can't encode character u'\ua000' in
position 0: ordinal not in range(128)
./main_encode.py | xxd
000: ea80 8061 6263 64de b40a ...abcd...



I'm guessing (since i already guessed you're running on Linux) that in 
the main_encode case, you're printing to a terminal window that Python 
already knows is utf-8.


But in the pipe case, it cannot tell what's on the other side.  So it 
guesses ASCII, and runs into the conversion problem.


(Everything's different in Python 3.x, though in general the problem 
still exists.  If the interpreter cannot tell what encoding is needed, 
it has to guess.)


There are ways to tell Python 2.7 what encoding a given file object 
should have, so you could tell Python to use utf-8 for sys.stdout.  I 
don't know if that's the best answer, but here's what my notes say:


import sys, codecs
sys.stdout = codecs.getwriter('utf8')(sys.stdout)

Once you've done that, print output will go through the specified codec 
on the way to the redirected pipe.



--
DaveA

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


Re: Threading in Python, Please check the script

2015-01-14 Thread Dave Angel

On 01/14/2015 01:22 AM, Robert Clove wrote:

Hi All,



In any new thread, you should specify what versions of Python and OS 
you're using.  I'll assume Python 2.7 and Linux for this message.



I have made a script in which i have started two thread named thread 1 and
thread 2.
In thread 1 one function will run named func1 and in thread 2 function 2
will run named func 2.
Thread 1 will execute a command and wait for 60 seconds.
Thread 2 will run only till thread 1 is running .
Again after that the same process continues in while after a break of 80
Seconds.

I am a beginner in python.
Please suggest what all i have done wrong and how to correct it.


#!/usr/bin/python

import threading
import time
import subprocess
import datetime
import os
import thread

thread.start_new_thread( print_time, (None, None))
thread.start_new_thread( print_time1, (None, None))


In these two lines you're referencing a function that hasn't been 
defined yet.  This top-level code should be moved to the end of the 
file, after the if __name__ = "__main__": line


Or just drop it, since you've got a second set of code also trying to 
create new threads.



command= "strace -o /root/Desktop/a.txt -c ./server"
final_dir = "/root/Desktop"
exitflag = 0
# Define a function for the thread
def print_time(*args):
 os.chdir(final_dir)
 print "IN first thread"
 proc = subprocess.Popen(command,shell=True,stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
 proc.wait(70)
 exitflag=1


You just set a local variable, not the global one.  So it won't be 
visible in the other thread.  If you must rebind a top-level variable 
from a function, you need to use the 'global' declaration in your function.





def print_time1(*args):
 print "In second thread"
 global exitflag
 while exitflag:
 thread.exit()
 #proc =
subprocess.Popen(command1,shell=True,stdout=subprocess.PIPE,
sterr=subprocess.PIPE)



# Create two threads as follows
try:
 while (1):
 t1=threading.Thread(target=print_time)
 t1.start()
 t2=threading.Thread(target=print_time1)
 t2=start()
 time.sleep(80)
 z = t1.isAlive()
 z1 = t2.isAlive()
 if z:
 z.exit()
 if z1:
 z1.exit()
threading.Thread(target=print_time1).start()
threading.Thread(target=print_time1).start()


What are you trying to do in those two lines?  If nothing else, they'll 
give an indentation error.  But if you fix that, you'll still have the 
potential problem of creating more and more threads as you loop around.



 print "In try"
except:


Bare excepts are "evil."  Your user can't tell what went wrong, from a 
syntax error to the user hitting control-C.  Even if you can't handle a 
particular kind of exception, at least have the courtesy of telling the 
user what went wrong.



print "Error: unable to start thread"





I'm sure there are other things, but these popped out at me.

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


Re: Using a ChangeLog as a canonical source of package metadata

2015-01-14 Thread Ben Finney
Ben Finney  writes:

> The idea is to parse from the Changelog the version metadata, and
> record it in Setuptools metadata. Then the ‘pkg_resources’ module of
> Setuptools allows programmatic access to that metadata.

One tricky aspect is: at what specific point should the Changelog be
parsed and the version metadata recorded in Setuptools package metadata?

At first I thought it should be done immediately on starting ‘setup.py’,
in order to have values to supply to the ‘setup()’ call. So I imported
the ‘version’ module which itself imports ‘docutils’ to have the reST
parsing available; then ‘setup.py’ continues by feeding the Changelog to
a function which parses it and emits values which are used to supply
parameters to ‘setup()’.

As was revealed in a recent version of the code base, though, this
causes a circular dependency. Docutils is a third-party library, which
needs to be declared as a dependency and satsified before it can be
imported. But that dependency can't be declared until ‘setup()’ has run
to specify what the dependencies are!

So in a later release I've had to break that circle by introducing an
initial “unknown” state for the version information, in order to allow
‘setup()’ to run and get the dependencies installed. Then when the
‘setup.py egg_info’ command is run, the Changelog is parsed and the
version info metadata file is injected into the Setuptools metadata for
the distribution.

Rather more complicated than I would like, because of the need to have
‘setup()’ as a top-level call in ‘setup.py’. This would be more
straightforward if we could assume the commonly-deployed existence of a
*declarative* build system (such as Make); but in Python we're stuck
with ‘setup.py’ and its limitations for now. Fortunately, the hard work
of many people have made those much better in recent years.


I'm interested to know what people writing Python distributions think of
this approach. Again, the example I'm discussing is in ‘python-daemon’'s
code base, at https://alioth.debian.org/projects/python-daemon/>.

Eventually I might propose this to the Distutils folks as a possible
improvement, but I'd like to refine it more in the face of actual usage.

-- 
 \  “… a Microsoft Certified System Engineer is to information |
  `\ technology as a McDonalds Certified Food Specialist is to the |
_o__)   culinary arts.” —Michael Bacarella |
Ben Finney

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


Re: What does "pip install" do?

2015-01-14 Thread Ben Finney
Fabien  writes:

> On 12.01.2015 23:46, Chris Angelico wrote:
> > As far as I know, it's equivalent to three steps:
> >
> > 1) Download the appropriate version of a package (the latest, if you
> > didn't say otherwise)
> > 2) Extract that package
> > 3) Run 'python setup.py'.
> >
> > What setup.py does is up to the package, but for a pure-Python
> > package it should simply create something in site-packages. It might
> > do a lot more, though - compiling C or Fortran code, for instance.
>
> Thanks Chris. I should then rephrase my question to "what does python
> setup.pt do?" ;-).

You're re-phrasing that as a question which has already been answered,
though. What ‘setup.py’ actually does is up to each package. Some are
quite small and simple; others are large complex applications.

> My point was also that I think that this information (mostly: what
> will change on my system or my virtual env when I install a python
> package?) could/should be provided in the mentioned resources:
>
> "Python Packaging Authority" website
> (https://pip.pypa.io/en/latest/user_guide.html),
> "python-packaging-user-guide"
> (http://python-packaging-user-guide.readthedocs.org/en/latest/installing.html)

Why, though? The point of an end-user program is that an end-user should
not need to be bothered by what it does internally, only what the
observable behaviour is. In the case of ‘pip install’, the observable
behaviour is that a package is installed and ready for use.

Your questions drive ever toward “What is the *implementation* of this?”
which is not material suitable for a user guide — especially when the
implementation details are in much more appropriate documents
https://python-packaging-user-guide.readthedocs.org/en/latest/distributing.html>
https://python-packaging-user-guide.readthedocs.org/en/latest/additional.html>.

If you want to learn about the internals and the implementation details,
you are expressly moving beyond the scope of the user guide. There's
nothing wrong with doing that, but you need to recognise the shift and
look for the appropriate documentation.

-- 
 \  “The most common way people give up their power is by thinking |
  `\   they don't have any.” —Alice Walker |
_o__)  |
Ben Finney

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


Re: Performance in exec environnements

2015-01-14 Thread Chris Angelico
On Wed, Jan 14, 2015 at 8:02 PM, Jean-Baptiste Braun
 wrote:
> What I'm trying to do is to map a transformation description in a markup
> langage (XSLT) in python to improve execution time. Here is a simplification
> of what it looks like :
>
> XSLT :
> 
>  
>   
>Mr
>   
>   
>Mrs
>   
>  
> 
>
> Generated python :
> print('')
> if gender == 'M':
> print('Mr')
> else:
> print('Mrs')
> print('')

Would it be possible to do a one-off transformation of the entire XSLT
file into a Python module with a single function in it, and then every
time you need that XSLT, you import that module and call the function?
That would potentially be a lot quicker than exec(), *and* would be
much clearer - there'd be an actual file on the disk with your
generated Python code, and anyone could read it and understand what
it's doing.

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


Re: Threading in Python, Please check the script

2015-01-14 Thread Robert Clove
Can u provide me the pseudo script.


On Wed, Jan 14, 2015 at 4:10 PM, Dave Angel  wrote:

> On 01/14/2015 01:22 AM, Robert Clove wrote:
>
>> Hi All,
>>
>>
> In any new thread, you should specify what versions of Python and OS
> you're using.  I'll assume Python 2.7 and Linux for this message.
>
>  I have made a script in which i have started two thread named thread 1 and
>> thread 2.
>> In thread 1 one function will run named func1 and in thread 2 function 2
>> will run named func 2.
>> Thread 1 will execute a command and wait for 60 seconds.
>> Thread 2 will run only till thread 1 is running .
>> Again after that the same process continues in while after a break of 80
>> Seconds.
>>
>> I am a beginner in python.
>> Please suggest what all i have done wrong and how to correct it.
>>
>>
>> #!/usr/bin/python
>>
>> import threading
>> import time
>> import subprocess
>> import datetime
>> import os
>> import thread
>>
>> thread.start_new_thread( print_time, (None, None))
>> thread.start_new_thread( print_time1, (None, None))
>>
>
> In these two lines you're referencing a function that hasn't been defined
> yet.  This top-level code should be moved to the end of the file, after the
> if __name__ = "__main__": line
>
> Or just drop it, since you've got a second set of code also trying to
> create new threads.
>
>  command= "strace -o /root/Desktop/a.txt -c ./server"
>> final_dir = "/root/Desktop"
>> exitflag = 0
>> # Define a function for the thread
>> def print_time(*args):
>>  os.chdir(final_dir)
>>  print "IN first thread"
>>  proc = subprocess.Popen(command,shell=True,stdout=subprocess.PIPE,
>> stderr=subprocess.PIPE)
>>  proc.wait(70)
>>  exitflag=1
>>
>
> You just set a local variable, not the global one.  So it won't be visible
> in the other thread.  If you must rebind a top-level variable from a
> function, you need to use the 'global' declaration in your function.
>
>
>
>> def print_time1(*args):
>>  print "In second thread"
>>  global exitflag
>>  while exitflag:
>>  thread.exit()
>>  #proc =
>> subprocess.Popen(command1,shell=True,stdout=subprocess.PIPE,
>> sterr=subprocess.PIPE)
>>
>>
>>
>> # Create two threads as follows
>> try:
>>  while (1):
>>  t1=threading.Thread(target=print_time)
>>  t1.start()
>>  t2=threading.Thread(target=print_time1)
>>  t2=start()
>>  time.sleep(80)
>>  z = t1.isAlive()
>>  z1 = t2.isAlive()
>>  if z:
>>  z.exit()
>>  if z1:
>>  z1.exit()
>> threading.Thread(target=print_time1).start()
>> threading.Thread(target=print_time1).start()
>>
>
> What are you trying to do in those two lines?  If nothing else, they'll
> give an indentation error.  But if you fix that, you'll still have the
> potential problem of creating more and more threads as you loop around.
>
>   print "In try"
>> except:
>>
>
> Bare excepts are "evil."  Your user can't tell what went wrong, from a
> syntax error to the user hitting control-C.  Even if you can't handle a
> particular kind of exception, at least have the courtesy of telling the
> user what went wrong.
>
>  print "Error: unable to start thread"
>>
>>
>>
>>
> I'm sure there are other things, but these popped out at me.
>
> --
> DaveA
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Threading in Python, Please check the script

2015-01-14 Thread Dave Angel

On 01/14/2015 07:11 AM, Robert Clove wrote:

Can u provide me the pseudo script.


You say you're a beginner.  If so, you shouldn't be trying to use 
threads, which are tricky.  I've been programming for 46 years, and I 
seldom have had to write multi-threading code.


But this looks like a school assignment.  And it looks like the code has 
little to do with the assignment.  So let's break down the assignment a bit.


Your assignment says the two threads will be called thread1 and thread2. 
 So why do you call them t1 and t2?


Your assignment says the two functions will be called func1 and func2. 
So why do you call them print_time and print_time1 ?


The assignment doesn't say anything and about running an external 
process.  Since that's also complex and error prone, perhaps you should 
keep it simple till everything else works.


Some general principles:

1) try not to do the same thing in several places.  You create threads 
in 3 places, and two of them are wrong or at least non-optimal.  Keep 
just the one where you do it right.


2) Use useful names for the various variables.  Except of course where 
the assignment specifies the name to be used.  Then use that name.


3) Learn one new concept at a time.  If you're concentrating on threads 
here, don't try to learn subprocess at the same time, unless that's what 
the assignment calls for.


4) Avoid chdir, especially in multithreaded programs.  Use an absolute 
path if you must when dealing with files.  Except for trivial programs, 
changing the working directory is likely to bite you in the foot 
somewhere else in the program.



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


Re: Python 3 regex?

2015-01-14 Thread Thomas 'PointedEars' Lahn
[email protected] wrote:

> Le mardi 13 janvier 2015 03:53:43 UTC+1, Rick Johnson a écrit :
>> [...]
>> you should find Python's "text processing Nirvana"
>> [...]
> 
> I recommend, you write a "small" application

I recommend you get a real name and do not post using the troll and spam-
infested Google Groups, but a newsreader instead.

> sorting strings composed of latin characters, a sort based on
> diacritical characters

I do not think you need regular expressions for that: you can use Unicode 
collations.

> (and eventually, taking into account linguistic specific aspects).

BTDT.  For a translator application, I used Python to sort a dictionary of 
the Latin phonetic transcription of Golic Vulcan whose alphabet is “S T P K 
R L A Sh O U D V Kh E H G Ch I N Zh M Y F Z Th W B” [1].  re helped a lot 
with that because inversely sorting the list by character length and turning 
it into an alternation allowed me to easily find the characters in words, 
and assign numbers to the letters so that I could sort the words according 
to this alphabet.  If anyone is interested, I can post the relevant code.

> And, why not? compare Py3.2 and Py3.3+ !

What are you getting at?

[1] 

-- 
PointedEars

Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.
-- 
https://mail.python.org/mailman/listinfo/python-list


How to terminate the function that runs every n seconds

2015-01-14 Thread Ganesh Pal
Iam using Linux and  Python 2.7  and playing with the threading.Timer module.

I had the below question on the same.

(a) How to I prevent the execution  the  "EXECUTED SLEEP" after 4
seconds ? , current this is running in an infinite loop

node-1# cat  file_01.py

import threading
import time
def printit():
threading.Timer(2, printit).start()
print "EXECUTED SLEEP"
printit()
print "hi"
time.sleep(4)
print "hi"
time.sleep(5)

Output:
node-1# python  file_01.py
EXECUTED SLEEP
hi
EXECUTED SLEEP
EXECUTED SLEEP
EXECUTED SLEEP



Regards,
Ganesh
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to terminate the function that runs every n seconds

2015-01-14 Thread Ganesh Pal
Corrected Typos .

>a) How to I prevent the execution  of   Print "EXECUTED SLEEP" after 4
> seconds ? , current this is running in an infinite loop


On Wed, Jan 14, 2015 at 6:37 PM, Ganesh Pal  wrote:
> Iam using Linux and  Python 2.7  and playing with the threading.Timer module.
>
> I had the below question on the same.
>
> (a) How to I prevent the execution  the  "EXECUTED SLEEP" after 4
> seconds ? , current this is running in an infinite loop
>
> node-1# cat  file_01.py
>
> import threading
> import time
> def printit():
> threading.Timer(2, printit).start()
> print "EXECUTED SLEEP"
> printit()
> print "hi"
> time.sleep(4)
> print "hi"
> time.sleep(5)
>
> Output:
> node-1# python  file_01.py
> EXECUTED SLEEP
> hi
> EXECUTED SLEEP
> EXECUTED SLEEP
> EXECUTED SLEEP
>
>
>
> Regards,
> Ganesh
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to terminate the function that runs every n seconds

2015-01-14 Thread Ganesh Pal
I quick modified the code and it now looks like this ,is this ok for
termination ? I played with t.canel() it didn't work

import threading
import time
def printit():
print "EXECUTED SLEEP"

t = threading.Timer(4, printit)
t.start()
printit()
print "hello"

output :

Throttling-1# python  file_02.py
EXECUTED SLEEP
hello
EXECUTED SLEEP



On Wed, Jan 14, 2015 at 6:39 PM, Ganesh Pal  wrote:
> Corrected Typos .
>
>>a) How to I prevent the execution  of   Print "EXECUTED SLEEP" after 4
>> seconds ? , current this is running in an infinite loop
>
>
> On Wed, Jan 14, 2015 at 6:37 PM, Ganesh Pal  wrote:
>> Iam using Linux and  Python 2.7  and playing with the threading.Timer module.
>>
>> I had the below question on the same.
>>
>> (a) How to I prevent the execution  the  "EXECUTED SLEEP" after 4
>> seconds ? , current this is running in an infinite loop
>>
>> node-1# cat  file_01.py
>>
>> import threading
>> import time
>> def printit():
>> threading.Timer(2, printit).start()
>> print "EXECUTED SLEEP"
>> printit()
>> print "hi"
>> time.sleep(4)
>> print "hi"
>> time.sleep(5)
>>
>> Output:
>> node-1# python  file_01.py
>> EXECUTED SLEEP
>> hi
>> EXECUTED SLEEP
>> EXECUTED SLEEP
>> EXECUTED SLEEP
>>
>>
>>
>> Regards,
>> Ganesh
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Performance in exec environnements

2015-01-14 Thread Jean-Baptiste Braun
2015-01-14 12:14 GMT+01:00 Chris Angelico :

> Would it be possible to do a one-off transformation of the entire XSLT
> file into a Python module with a single function in it, and then every
> time you need that XSLT, you import that module and call the function?
> That would potentially be a lot quicker than exec(), *and* would be
> much clearer - there'd be an actual file on the disk with your
> generated Python code, and anyone could read it and understand what
> it's doing.
>
I've done some tests executing compiled generated python and it doesn't
seem to be worth over processing xslt directly. What you propose could be a
solution. In fact I'm not sure yet how it will be designed.

Thank you for feedback anyway.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Performance in exec environnements

2015-01-14 Thread Chris Angelico
On Thu, Jan 15, 2015 at 12:38 AM, Jean-Baptiste Braun
 wrote:
> 2015-01-14 12:14 GMT+01:00 Chris Angelico :
>>
>> Would it be possible to do a one-off transformation of the entire XSLT
>> file into a Python module with a single function in it, and then every
>> time you need that XSLT, you import that module and call the function?
>> That would potentially be a lot quicker than exec(), *and* would be
>> much clearer - there'd be an actual file on the disk with your
>> generated Python code, and anyone could read it and understand what
>> it's doing.
>
> I've done some tests executing compiled generated python and it doesn't seem
> to be worth over processing xslt directly. What you propose could be a
> solution. In fact I'm not sure yet how it will be designed.
>
> Thank you for feedback anyway.

I don't know how often you need to re-process the same XSLT file, but
if you often run the same file (eg with different input data), then
you could create a .py file and import it, and then call it. Something
like this:

# Python code generated from some_file.xslt - DO NOT MODIFY
def process(gender):
print('')
if gender == 'M':
print('Mr')
else:
print('Mrs')
print('')


And then you'd use it thus:

import some_file
some_file.process(gender='M')

Although rather than using print(), it might be better to use yield:

# Python code generated from some_file.xslt - DO NOT MODIFY
def process(gender):
yield ''
if gender == 'M':
yield 'Mr'
else:
yield 'Mrs'
yield''

And then usage would be:

import some_file
print("\n".join(some_file.process(gender='M')))

which gives you the flexibility of sending it somewhere other than
stdout, without monkey-patching the print function to do something
else.

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


Re: what would be the regular expression for null byte present in a string

2015-01-14 Thread Thomas 'PointedEars' Lahn
Shambhu Rajak wrote:

> I have a string that I get as an output of a command as:
> 
'\x01\x00\x00\x00\x00\x00\x00\x00\x0c\x00\x00\x0010232ae8944a\x02\x00\x00\x00\x00\x00\x00\x00\n'
> 
> I want to fetch '10232ae8944a' from the above string.
> 
> I want to find a re pattern that could replace all the \x01..\x0z to be
> replace by empty string '',  so that I can get the desired portion of
> string

You need a character class with that range.

> Can anyone help me with a working regex for it.

Yes.

> 
> 
> PLEASE NOTE: The information contained in this electronic mail message is
> intended only for the use of the designated recipient(s) named above. […]

Please disable this nonsense, do not post multi-part messages, or if this is 
not possible with your e-mail client or provider, use another one, 
respectively.

-- 
PointedEars

Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: what would be the regular expression for null byte present in a string

2015-01-14 Thread Thomas 'PointedEars' Lahn
Peter Otten wrote:

> Shambhu Rajak wrote:
>> I have a string that I get as an output of a command as:
>> 
> 
'\x01\x00\x00\x00\x00\x00\x00\x00\x0c\x00\x00\x0010232ae8944a\x02\x00\x00\x00\x00\x00\x00\x00\n'
>> 
>> I want to fetch '10232ae8944a' from the above string.
>> 
>> I want to find a re pattern that could replace all the \x01..\x0z to be
>> replace by empty string '',  so that I can get the desired portion of
>> string
>> 
>> Can anyone help me with a working regex for it.
> 
> I think you want the str.tranlate() method rather than a regex.

Another possibility, but given the length of the list probably not the most 
efficient one.  Aside from re and this one, here is another (tested with 
Python 3.4.2):

  filtered = ''.join(filter(lambda ch: ord(ch) > 0x0f, s))

-- 
PointedEars

Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: what would be the regular expression for null byte present in a string

2015-01-14 Thread Thomas 'PointedEars' Lahn
Thomas 'PointedEars' Lahn wrote:

> Peter Otten wrote:
>> Shambhu Rajak wrote:
>>> I want to find a re pattern that could replace all the \x01..\x0z to be
>>> replace by empty string '',  so that I can get the desired portion of
>>> string
>>> 
>>> Can anyone help me with a working regex for it.
>> 
>> I think you want the str.tranlate() method rather than a regex.
> 
> Another possibility, but given the length of the list probably not the
> most efficient one.  Aside from re and this one, here is another (tested
> with Python 3.4.2):
> 
>   filtered = ''.join(filter(lambda ch: ord(ch) > 0x0f, s))

  filtered = ''.join(filter(lambda ch: ch > "\x0f", s))

-- 
PointedEars

Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: what would be the regular expression for null byte present in a string

2015-01-14 Thread Wolfgang Maier

On 01/13/2015 02:40 PM, Shambhu Rajak wrote:

I have a string that I get as an output of a command as:

'\x01\x00\x00\x00\x00\x00\x00\x00\x0c\x00\x00\x0010232ae8944a\x02\x00\x00\x00\x00\x00\x00\x00\n'

I want to fetch ‘*10232ae8944a*’ from the above string.

I want to find a re pattern that could replace all the \x01..\x0z to be
replace by empty string ‘’,  so that I can get the desired portion of string

Can anyone help me with a working regex for it.



Sorry, no regex either, but depending on what exact behavior you need 
str.isprintable() or even str.isalnum() could be for you:


e.g.

s_in = 
'\x01\x00\x00\x00\x00\x00\x00\x00\x0c\x00\x00\x0010232ae8944a\x02\x00\x00\x00\x00\x00\x00\x00\n'

s_out = ''.join(c for c in s_in if c.isprintable())

Wolfgang

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


Re: what would be the regular expression for null byte present in a string

2015-01-14 Thread Wolfgang Maier

On 01/13/2015 02:40 PM, Shambhu Rajak wrote:

I have a string that I get as an output of a command as:

'\x01\x00\x00\x00\x00\x00\x00\x00\x0c\x00\x00\x00*10232ae8944a*\x02\x00\x00\x00\x00\x00\x00\x00\n'

I want to fetch ‘*10232ae8944a*’ from the above string.

I want to find a re pattern that could replace all the \x01..\x0z to be
replace by empty string ‘’,  so that I can get the desired portion of string

Can anyone help me with a working regex for it.

Thanks,

Shambhu



If the characters you need to keep always form a contiguous stretch, you 
can also use:


exclude = ''.join(chr(n) for n in range(32))
s_in.strip(exclude)

Wolfgang

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


Re: How to terminate the function that runs every n seconds

2015-01-14 Thread Ganesh Pal
This is bit urgent and I all stuck form last few hours   :(

On Wed, Jan 14, 2015 at 6:37 PM, Ganesh Pal  wrote:
> Iam using Linux and  Python 2.7  and playing with the threading.Timer module.
>
> I had the below question on the same.
>
> (a) How to I prevent the execution  the  "EXECUTED SLEEP" after 4
> seconds ? , current this is running in an infinite loop
>
> node-1# cat  file_01.py
>
> import threading
> import time
> def printit():
> threading.Timer(2, printit).start()
> print "EXECUTED SLEEP"
> printit()
> print "hi"
> time.sleep(4)
> print "hi"
> time.sleep(5)
>
> Output:
> node-1# python  file_01.py
> EXECUTED SLEEP
> hi
> EXECUTED SLEEP
> EXECUTED SLEEP
> EXECUTED SLEEP
>
>
>
> Regards,
> Ganesh
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python 3 regex?

2015-01-14 Thread alister
On Wed, 14 Jan 2015 14:02:27 +0100, Thomas 'PointedEars' Lahn wrote:

> [email protected] wrote:
> 
>> Le mardi 13 janvier 2015 03:53:43 UTC+1, Rick Johnson a écrit :
>>> [...]
>>> you should find Python's "text processing Nirvana"
>>> [...]
>> 
>> I recommend, you write a "small" application
> 
> I recommend you get a real name and do not post using the troll and
> spam- infested Google Groups, but a newsreader instead.
> 
>> sorting strings composed of latin characters, a sort based on
>> diacritical characters
> 
> I do not think you need regular expressions for that: you can use
> Unicode collations.
> 
>> (and eventually, taking into account linguistic specific aspects).
> 
> BTDT.  For a translator application, I used Python to sort a dictionary
> of the Latin phonetic transcription of Golic Vulcan whose alphabet is “S
> T P K R L A Sh O U D V Kh E H G Ch I N Zh M Y F Z Th W B” [1].  re
> helped a lot with that because inversely sorting the list by character
> length and turning it into an alternation allowed me to easily find the
> characters in words, and assign numbers to the letters so that I could
> sort the words according to this alphabet.  If anyone is interested, I
> can post the relevant code.
> 
>> And, why not? compare Py3.2 and Py3.3+ !
> 
> What are you getting at?
> 
> [1] 

Do not wast you/our time with JMF
he is a resident troll but unlike some of our resident trolls I don't 
think he has ever contributed anything useful 



-- 
An actor's a guy who if you ain't talkin' about him, ain't listening.
-- Marlon Brando
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to terminate the function that runs every n seconds

2015-01-14 Thread Mark Lawrence

On 14/01/2015 15:03, Ganesh Pal wrote:

This is bit urgent and I all stuck form last few hours   :(



I'm not inclined to help a person who throws four posts at us in two 
hours and top posts, sorry.


--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


Re: How to terminate the function that runs every n seconds

2015-01-14 Thread Dave Angel

On 01/14/2015 08:09 AM, Ganesh Pal wrote:

Corrected Typos .


a) How to I prevent the execution  of   Print "EXECUTED SLEEP" after 4
seconds ? , current this is running in an infinite loop





Please on't top-post.  If you want to comment on a message, insert your 
comments after the portion of the message you'd like to comment on.



On Wed, Jan 14, 2015 at 6:37 PM, Ganesh Pal  wrote:

Iam using Linux and  Python 2.7  and playing with the threading.Timer module.

I had the below question on the same.

(a) How to I prevent the execution  the  "EXECUTED SLEEP" after 4
seconds ? , current this is running in an infinite loop

node-1# cat  file_01.py

import threading
import time
def printit():
 threading.Timer(2, printit).start()
 print "EXECUTED SLEEP"
printit()
print "hi"
time.sleep(4)
print "hi"
time.sleep(5)

Output:
node-1# python  file_01.py
EXECUTED SLEEP
hi
EXECUTED SLEEP
EXECUTED SLEEP
EXECUTED SLEEP



Do you want to fix the symptom, fix the problem, or finish a school 
assignment?  To do the first, make a global variable that contains the 
time you want to stop making new threads, and conditionally test it 
before calling threading.Timer


Something like
   quit_time = timer.time() + 4

If you want to fix the problem, consider that creating a new thread for 
each iteration is overkill, and consider how you might better solve it. 
 For example a single thread that loops through a range, and prints 
once each time could easily do something a fixed number of times.


if you want to get the homework done, better reread the assignment.  For 
example, if you just want to print the string a fixed number of times 
with a delay each time, you don't need threads at all.  But the 
assignment may have specified that you're stuck with using threads for 
the purpose, and it may have even required that you use Timer somewhere.


Note that neither Timer nor sleep makes any promises about how 
accurately it matches the requested time.


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


Re: How to terminate the function that runs every n seconds

2015-01-14 Thread Mark Lawrence

On 14/01/2015 16:33, Dave Angel wrote:


Note that neither Timer nor sleep makes any promises about how
accurately it matches the requested time.



Reminds me of working on Telematics S200/300/4000/5000 telecomms kit in 
the early 90s where the timers were mains based, so a one hour timer 
would go off at about one hour, 30 seconds.  Programming becomes such 
fun working around such things :)


--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


Calling a derived class's constructor from a parent method

2015-01-14 Thread jason
If I have a class hierarchy like so:

 
class A(object):
  def __init__(self, s):
self.s = s
  def foo(self, s):
return A(s)

class B(A):
  def __init__(self, s):
A.__init__(self, s)

If I make a B:

b = B(0)

I'd like b.foo(1) to return an instance of B. Is there a way to do that besides 
implementing a construct(self, s) for each part of the hierarchy? I.e. is there 
a way for the base class to look at self and find out what type to create?

I'm using Python 2.7.5, but I'm curious what the 3.x answer is too.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Calling a derived class's constructor from a parent method

2015-01-14 Thread Ian Kelly
On Wed, Jan 14, 2015 at 9:45 AM, jason  wrote:
> class A(object):
>   def __init__(self, s):
> self.s = s
>   def foo(self, s):
> return A(s)

Instead of explicitly naming the return class here, do this:

return self.__class__(s)

Alternatively, since you never use self elsewhere in the method, it
may be cleaner to use a classmethod here:

@classmethod
def foo(cls, s):
return cls(s)

> I'm using Python 2.7.5, but I'm curious what the 3.x answer is too.

The answer is the same in 3.x.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Calling a derived class's constructor from a parent method

2015-01-14 Thread Chris Angelico
On Thu, Jan 15, 2015 at 3:45 AM, jason  wrote:
> If I have a class hierarchy like so:
>
>
> class A(object):
>   def __init__(self, s):
> self.s = s
>   def foo(self, s):
> return A(s)
>
> class B(A):
>   def __init__(self, s):
> A.__init__(self, s)
>
> If I make a B:
>
> b = B(0)
>
> I'd like b.foo(1) to return an instance of B. Is there a way to do that 
> besides implementing a construct(self, s) for each part of the hierarchy? 
> I.e. is there a way for the base class to look at self and find out what type 
> to create?
>
> I'm using Python 2.7.5, but I'm curious what the 3.x answer is too.

Assuming you require that every subclass be able to be constructed
with that single parameter, it's not too hard. You get a 'self'
parameter; its type is the type you want to construct.

def foo(self, s):
return type(self)(s)

That should work just fine on either 2.7 or 3.x.

I'm not sure what this function would do in context, but this basic
technique should work.

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


Re: Calling a derived class's constructor from a parent method

2015-01-14 Thread Mark Lawrence

On 14/01/2015 16:45, jason wrote:

If I have a class hierarchy like so:


class A(object):
   def __init__(self, s):
 self.s = s
   def foo(self, s):
 return A(s)

class B(A):
   def __init__(self, s):
 A.__init__(self, s)

If I make a B:

b = B(0)

I'd like b.foo(1) to return an instance of B. Is there a way to do that besides 
implementing a construct(self, s) for each part of the hierarchy? I.e. is there 
a way for the base class to look at self and find out what type to create?

I'm using Python 2.7.5, but I'm curious what the 3.x answer is too.



I'm confused, can you please explain what you're trying to achieve 
rather than how you're trying to achieve it and I'm sure that others 
will give better answers than I can :)


--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


Re: Performance in exec environnements

2015-01-14 Thread Ian Kelly
On Wed, Jan 14, 2015 at 2:02 AM, Jean-Baptiste Braun
 wrote:
> What I don't understand is the ratio between test 2 / 4 and test 1 / 3.
>
> Let 0.0229 sec be the execution time to read a bytecode (1st test).
> Executing two times that bytecode takes 0.042 sec (test 3), which looks
> coherent.
>
> Let 11.6 sec be the execution time to call the global exec, parse, do some
> stuff and read a bytecode (test 2). I'm trying to understand why does doing
> the same thing and reading one more bytecode is much longer (15.7 sec) in
> comparison.

You've also given it twice as much code to parse and compile.

C:\Users\Ian>python -m timeit "compile('1 + 1', '', 'exec')"
10 loops, best of 3: 16.8 usec per loop

C:\Users\Ian>python -m timeit "compile('1 + 1; 1 + 1', '', 'exec')"
1 loops, best of 3: 22.4 usec per loop
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [ANN] EasyGUI_Qt version 0.9

2015-01-14 Thread stephen . boulet
On Tuesday, January 13, 2015 at 8:30:13 PM UTC, André Roberge wrote:
> On Tuesday, 13 January 2015 08:23:30 UTC-4, [email protected]  wrote:
> > I found a solution that I'm happy with.
> > 
> > from datetime import datetime
> > from easygui_qt import *
> > 
> > datestring = get_date()
> > mydate = datetime.strptime(datestring, '%b %d %Y')
> 
> I'm thinking of having the new version return a datetime object automatically.
> 
> André

That's probably a cleaner solution.

> > On Saturday, January 10, 2015 at 1:02:30 AM UTC, André Roberge wrote:
> > > On Friday, 9 January 2015 19:09:15 UTC-4, [email protected]  wrote:
> > > > On Wednesday, December 31, 2014 at 4:24:50 PM UTC-6, André Roberge 
> > > > wrote:
> > > > > EasyGUI_Qt version 0.9 has been released.  This is the first 
> > > > > announcement about EasyGUI_Qt on this list.
> > > > > 
> > > > > Like the original EasyGUI (which used Tkinter), 
> > > > > EasyGUI_Qt seeks to provide simple GUI widgets
> > > > > that can be called in a procedural program. 
> > > > > 
> > > > > EasyGUI_Qt is NOT event-driven: all GUI interactions are invoked by 
> > > > > simple function calls.
> > > > > 
> > > > > The archetype is get_string(message)
> > > > > which pops a box whose purpose is exactly the same as Python's 
> > > > > input(prompt),
> > > > > that is, present the user with a question/prompt, have the user enter 
> > > > > an
> > > > > answer, and return the provided answer as a string.  Thus
> > > > > easygui_qt.get_string() can be used as a drop-in replacement for
> > > > > input().
> > > > > 
> > > > > Similarly, instead of using a print() function to display a message,
> > > > > show_message() is used which pops a message window.
> > > > > 
> > > > > EasyGUI_Qt requires PyQt4 and is really targeted for Python 3.3+ - 
> > > > > although it can work (possibly with some unicode problems ...) using 
> > > > > Python 2.7.
> > > > > 
> > > > > More information can be found at 
> > > > > http://easygui-qt.readthedocs.org/en/latest/index.html
> > > > > 
> > > > > Feedback is most welcome, including reporting bugs to 
> > > > > https://github.com/aroberge/easygui_qt/issues
> > > > > 
> > > > > Happy 2015 everyone,
> > > > > 
> > > > > André Roberge
> > > > 
> > > > Very nice, thanks.
> > > > 
> > > > One issue is the format returned for the calendar selection. For today, 
> > > > the string returned is "Fri Jan 9 2015". My script needs to convert the 
> > > > date to a datetime.date, and having the month returned as a string 
> > > > instead of an integer makes this harder.
> > > 
> > > Would today's date be represented as the string "09.01.2015" useful to 
> > > you? (I found out how to do this.)  If so, I could perhaps add an 
> > > argument like numeric_format = True.
> > > 
> > > André
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to terminate the function that runs every n seconds

2015-01-14 Thread Ganesh Pal
> Do you want to fix the symptom, fix the problem, or finish a school
> assignment?  To do the first, make a global variable that contains the time
> you want to stop making new threads, and conditionally test it before
> calling threading.Timer
>

 I firstly apologise for multiple posts and thanks for the reply  . I
was actually in hurry to complete my assignment  and there was no way
to figure out what was going wrong !!

 My assignment  was to write a subroutine that run itself every n
minutes  and terminate before the main thread completes.

  I thought threading.Timer helped me achieve this  but timer.cancel()
didn't help me terminate the timer .

> Something like
>quit_time = timer.time() + 4
>

Thanks for the above  hint, I will try this and get back to you

> Note that neither Timer nor sleep makes any promises about how accurately it
> matches the requested time.

Sure will keep this in mind

Regards ,
Ganesh
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to terminate the function that runs every n seconds

2015-01-14 Thread Grant Edwards
On 2015-01-14, Mark Lawrence  wrote:
> On 14/01/2015 16:33, Dave Angel wrote:
>
>> Note that neither Timer nor sleep makes any promises about how
>> accurately it matches the requested time.
>
> Reminds me of working on Telematics S200/300/4000/5000 telecomms kit in 
> the early 90s where the timers were mains based, so a one hour timer 
> would go off at about one hour, 30 seconds.

I don't understand.  Power line frequencies are _very_ accurate and
have been relied upon for timekeeping since the 1930s.  We're talking
a few hundred PPM over a 24 hour period.  A 30 second error over a one
hour period seems _really_ high.

-- 
Grant Edwards   grant.b.edwardsYow! All right, you
  at   degenerates!  I want
  gmail.comthis place evacuated in
   20 seconds!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to terminate the function that runs every n seconds

2015-01-14 Thread Mark Lawrence

On 14/01/2015 17:37, Grant Edwards wrote:

On 2015-01-14, Mark Lawrence  wrote:

On 14/01/2015 16:33, Dave Angel wrote:


Note that neither Timer nor sleep makes any promises about how
accurately it matches the requested time.


Reminds me of working on Telematics S200/300/4000/5000 telecomms kit in
the early 90s where the timers were mains based, so a one hour timer
would go off at about one hour, 30 seconds.


I don't understand.  Power line frequencies are _very_ accurate and
have been relied upon for timekeeping since the 1930s.  We're talking
a few hundred PPM over a 24 hour period.  A 30 second error over a one
hour period seems _really_ high.



From 
http://www2.nationalgrid.com/uk/services/balancing-services/frequency-response/ 
"National Grid has a licence obligation to control frequency within the 
limits specified in the 'Electricity Supply Regulations', i.e. ±1% of 
nominal system frequency (50.00Hz) save in abnormal or exceptional 
circumstances.".  I wouldn't describe ±1% as very accurate and certainly 
not within a few hundred ppm.  I'm assuming that this kind of limitation 
applies around the world, so could you be getting confused with some 
other more accurate frequency control?


--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


Re: How to terminate the function that runs every n seconds

2015-01-14 Thread Grant Edwards
On 2015-01-14, Mark Lawrence  wrote:
> On 14/01/2015 17:37, Grant Edwards wrote:
>> On 2015-01-14, Mark Lawrence  wrote:
>>
>>> Reminds me of working on Telematics S200/300/4000/5000 telecomms kit in
>>> the early 90s where the timers were mains based, so a one hour timer
>>> would go off at about one hour, 30 seconds.
>>
>> I don't understand.  Power line frequencies are _very_ accurate and
>> have been relied upon for timekeeping since the 1930s.  We're talking
>> a few hundred PPM over a 24 hour period.  A 30 second error over a one
>> hour period seems _really_ high.
>
> http://www2.nationalgrid.com/uk/services/balancing-services/frequency-response/
>  
> "National Grid has a licence obligation to control frequency within the 
> limits specified in the 'Electricity Supply Regulations', i.e. ±1% of 
> nominal system frequency (50.00Hz) save in abnormal or exceptional 
> circumstances.".  I wouldn't describe ±1% as very accurate and certainly 
> not within a few hundred ppm.

Sorry, I should have guessed from the use of the word "mains" that you
were in the UK -- which seems to have much laxer power-line frequency
regulation than the US.

> I'm assuming that this kind of limitation applies around the world,
> so could you be getting confused with some other more accurate
> frequency control?

Power line frequency control in the US is much tighter than the UK.
According to real-world data I've seen, powerline-based timings are
typically accurate to a few seconds per month.  In the US, they
implement freqency corrections every hour to keep long term,
cumulative time errors under a certain limit (the limit ranges from 2
to 10 seconds depending on region).

-- 
Grant Edwards   grant.b.edwardsYow! I love ROCK 'N ROLL!
  at   I memorized the all WORDS
  gmail.comto "WIPE-OUT" in 1965!!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Calling a derived class's constructor from a parent method

2015-01-14 Thread jason
On Wednesday, January 14, 2015 at 12:05:55 PM UTC-5, Mark Lawrence wrote:
 
> I'm confused, can you please explain what you're trying to achieve 
> rather than how you're trying to achieve it and I'm sure that others 
> will give better answers than I can :)
> 

Good call. Coming up with a minimal example make things pretty far out of 
context.

The base class represents a generic function (in formal logic) with some name 
and a list of parameters. The derived class is some sort of specialization of 
that generic class (like associative functions, etc).

It is desirable to do certain things to these functions, like perform 
substitution or canonization, that can be implemented in the basic class but 
which I'd like to return as a copy rather than modify the class. Thus, the base 
class needs to know which derived class (if any) to create.

The burden is indeed on me to keep the signature of the init() for each of 
these the same. 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Calling a derived class's constructor from a parent method

2015-01-14 Thread alister
On Wed, 14 Jan 2015 17:05:27 +, Mark Lawrence wrote:

> On 14/01/2015 16:45, jason wrote:
>> If I have a class hierarchy like so:
>>
>>
>> class A(object):
>>def __init__(self, s):
>>  self.s = s
>>def foo(self, s):
>>  return A(s)
>>
>> class B(A):
>>def __init__(self, s):
>>  A.__init__(self, s)
>>
>> If I make a B:
>>
>> b = B(0)
>>
>> I'd like b.foo(1) to return an instance of B. Is there a way to do that
>> besides implementing a construct(self, s) for each part of the
>> hierarchy? I.e. is there a way for the base class to look at self and
>> find out what type to create?
>>
>> I'm using Python 2.7.5, but I'm curious what the 3.x answer is too.
>>
>>
> I'm confused, can you please explain what you're trying to achieve
> rather than how you're trying to achieve it and I'm sure that others
> will give better answers than I can :)

why not just make another b?
b2=B(1)



-- 
The computer should be doing the hard work.  That's what it's paid to do,
after all.
 -- Larry Wall in <[email protected]>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Calling a derived class's constructor from a parent method

2015-01-14 Thread Dave Angel

On 01/14/2015 01:10 PM, jason wrote:

On Wednesday, January 14, 2015 at 12:05:55 PM UTC-5, Mark Lawrence wrote:


I'm confused, can you please explain what you're trying to achieve
rather than how you're trying to achieve it and I'm sure that others
will give better answers than I can :)



Good call. Coming up with a minimal example make things pretty far out of 
context.

The base class represents a generic function (in formal logic) with some name 
and a list of parameters. The derived class is some sort of specialization of 
that generic class (like associative functions, etc).

It is desirable to do certain things to these functions, like perform 
substitution or canonization, that can be implemented in the basic class but 
which I'd like to return as a copy rather than modify the class. Thus, the base 
class needs to know which derived class (if any) to create.

The burden is indeed on me to keep the signature of the init() for each of 
these the same.



If there are additional parameters possible, you might be able to use 
*args, **vargs to pass them through the call.


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


Re: Threading in Python, Please check the script

2015-01-14 Thread sohcahtoa82
On Tuesday, January 13, 2015 at 10:22:32 PM UTC-8, Robert Clove wrote:
> Hi All,
> 
> I have made a script in which i have started two thread named thread 1 and 
> thread 2.
> In thread 1 one function will run named func1 and in thread 2 function 2 will 
> run named func 2.
> Thread 1 will execute a command and wait for 60 seconds. 
> Thread 2 will run only till thread 1 is running .
> Again after that the same process continues in while after a break of 80 
> Seconds.
> 
> I am a beginner in python.
> Please suggest what all i have done wrong and how to correct it.
> 
> 
> #!/usr/bin/python
> 
> import threading
> import time
> import subprocess
> import datetime
> import os
> import thread
> 
> thread.start_new_thread( print_time, (None, None))
> thread.start_new_thread( print_time1, (None, None))
> command= "strace -o /root/Desktop/a.txt -c ./server"
> final_dir = "/root/Desktop"
> exitflag = 0
> # Define a function for the thread
> def print_time(*args):
>     os.chdir(final_dir)
>     print "IN first thread"
>     proc = subprocess.Popen(command,shell=True,stdout=subprocess.PIPE, 
> stderr=subprocess.PIPE)
>     proc.wait(70)
>     exitflag=1
>    
> def print_time1(*args):
>     print "In second thread"
>     global exitflag
>     while exitflag:
>         thread.exit()
>     #proc = subprocess.Popen(command1,shell=True,stdout=subprocess.PIPE, 
> sterr=subprocess.PIPE)
> 
> 
> 
> # Create two threads as follows
> try:
>     while (1):
>         t1=threading.Thread(target=print_time)
>         t1.start()
>         t2=threading.Thread(target=print_time1)
>         t2=start()
>         time.sleep(80)
>         z = t1.isAlive()
>         z1 = t2.isAlive()
>         if z:
>             z.exit()
>         if z1:
>             z1.exit()
>            threading.Thread(target=print_time1).start()
>            threading.Thread(target=print_time1).start()
>         print "In try"
> except:
>    print "Error: unable to start thread"
> 
>  

In addition to what others have said, it looks like you're designing your 
script to run as root.  This is a Very Bad Idea(tm) and has the potential to 
destroy your system if you make a minor mistake and end up blowing away vital 
system files.

If you want to be able to run the script anywhere but have the results saved in 
your home directory, then look into calling os.path.expanduser('~/Desktop') to 
write to the Desktop directory in your home directory.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Compiling multiple python scripts into an exe file

2015-01-14 Thread Dan Stromberg
On Tue, Jan 13, 2015 at 10:53 PM, dieter  wrote:
> no nein  writes:
>
>> Basically, is it possible to compile multiple unrelated python scripts into 
>> a single exe file, so when execute it several python programs are run at 
>> once.
>
> These are two distinct problems:
>
>   *  for doing things in paralell, look at "threading" or "multiprocessing"
Or subprocess.

>   *  for generating exe files, look at "freeze".
Or nuitka.
-- 
https://mail.python.org/mailman/listinfo/python-list


multiprocessing queue hangs up on the Amazon cloud

2015-01-14 Thread jgrant
Hello!

I searched and found posts that were similar to mine, but either I couldn't 
understand the answer or the problem was different enough that the answers 
weren't helpful - please excuse me if this seems to repeat a problem already 
answered.

I am trying to run a series of scripts on the Amazon cloud, multiprocessing on 
the 32 cores of our AWS instance.  The scripts run well, and the queuing seems 
to work BUT, although the processes run to completion, the script below that 
runs the queue never ends.  I have tried Queue and JoinableQueue and the same 
thing happens for both.

I've simplified the example below a bit, just having it print, but the problem 
is the same.  Can anyone see what I have done wrong here?

Thanks!

Jessica



#!/usr/bin/env python
import os

from multiprocessing import Lock, Process, Queue, current_process, 
freeze_support,JoinableQueue
from Pipeline import Pipeline
import time

def worker(done_queue,work_queue):
try:
for f in iter(work_queue.get, 'STOP'):
###
# Normally I call a complicated series of scripts here 
but to test, just print  
print f
### 
done_queue.put("%s - %s got %s." % 
(current_process().name, f, f))
log("%s - %s got %s." % (current_process().name, f, 
f),f)
work_queue.task_done()
except Exception, e:
done_queue.put("%s failed on %s with: %s" % 
(current_process().name, f, e.message))
log("%s failed on %s with: %s" % (current_process().name, f, 
e.message),f)
work_queue.task_done()
done_queue.put('STOP')

return


def log(string,f):
outfile = open('queue_taxon_logfile' + str(f),'a')
outfile.write(str(string) + '\n')
outfile.close()

def main():

workers = 32 #change if fewer processors are available
work_queue = JoinableQueue(810) #change if more than 810 taxa
done_queue = JoinableQueue(810)
processes = []

print "1 it  gets here"
for f in range(64):
work_queue.put(f)   
work_queue.put('STOP')

print "2 it  gets here"

for w in xrange(workers):
p = Process(target=worker, args=(done_queue,work_queue))
p.start()
processes.append(p)

print "3  it  gets here"
for p in processes:
print p # it only prints once - 

p.join()

print "it never gets here"
for status in iter(done_queue.get, 'STOP'):
print status


if __name__ == '__main__':
main()

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


Re: multiprocessing queue hangs up on the Amazon cloud

2015-01-14 Thread Chris Angelico
On Thu, Jan 15, 2015 at 8:55 AM,   wrote:
> I am trying to run a series of scripts on the Amazon cloud, multiprocessing 
> on the 32 cores of our AWS instance.  The scripts run well, and the queuing 
> seems to work BUT, although the processes run to completion, the script below 
> that runs the queue never ends.  I have tried Queue and JoinableQueue and the 
> same thing happens for both.
>

Hi! I'm not absolutely sure that this is a problem, but I'm seeing
something in the code that you might have to walk me through.

> def worker(done_queue,work_queue):
> try:
> for f in iter(work_queue.get, 'STOP'):

This repeatedly asks the queue for something, until it finds the word STOP.

By the way:

> except Exception, e:

This is an old form of exception syntax. You may want to use "except
Exception as e:" instead, assuming you don't need to support ancient
Python versions. Carrying on!

> work_queue = JoinableQueue(810) #change if more than 810 taxa
> work_queue.put('STOP')

There's a single work queue, with a single word STOP on it.

> for w in xrange(workers):
> p = Process(target=worker, args=(done_queue,work_queue))
> p.start()
> processes.append(p)
>
> print "3  it  gets here"
> for p in processes:
> print p # it only prints once - 
> 
> p.join()

And then you seek to run multiple workers. If my reading is correct,
one of them (whichever one happens to get there first) will read the
STOP marker and finish; the others will all be blocked, waiting for
more work (which will never arrive). In theory, you could have the
first process be the one to stop, in which case you'd successfully
join() it and then go on to wait for the second, but if any other
process gets it, you'll be stuck waiting for the first. Or have I
misunderstood something in your logic?

If this is indeed what's happening, the simplest solution might be to
add as many STOPs as you have workers. Alternatively, if you can
guarantee that the work is all on the queue before the first process
starts, you could simply use the empty queue as the sentinel; which I
would recommend doing for the done_queue, as there's only one reader
for that.

But it's entirely possible I've missed some tiny fact that makes my
entire analysis wrong, in which case I apologize for the noise!

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


Re: multiprocessing queue hangs up on the Amazon cloud

2015-01-14 Thread Chris Kaynor
On Wed, Jan 14, 2015 at 2:16 PM, Chris Angelico  wrote:

> And then you seek to run multiple workers. If my reading is correct,
> one of them (whichever one happens to get there first) will read the
> STOP marker and finish; the others will all be blocked, waiting for
> more work (which will never arrive). In theory, you could have the
> first process be the one to stop, in which case you'd successfully
> join() it and then go on to wait for the second, but if any other
> process gets it, you'll be stuck waiting for the first. Or have I
> misunderstood something in your logic?
>
> If this is indeed what's happening, the simplest solution might be to
> add as many STOPs as you have workers. Alternatively, if you can
> guarantee that the work is all on the queue before the first process
> starts, you could simply use the empty queue as the sentinel; which I
> would recommend doing for the done_queue, as there's only one reader
> for that.
>
> But it's entirely possible I've missed some tiny fact that makes my
> entire analysis wrong, in which case I apologize for the noise!
>

My reading of the code is the same as Chris Angelico's, however I would
like to add some more:

Another solution to the STOP issue would be to have each worker re-add the
STOP to the work queue once it is complete. This is a good solution if you
cannot put all the work on the queues before starting the workers or the
number of workers is not known when creating the work queue. This can be
done by merely adding the line "work_queue.put('STOP')" to the end of the
worker function. Note that this solution will not work correctly if workers
may produce more work (but, then again, neither will the STOP entry).

I'd also like to point out a flaw in the results processing, at the end of
the worker process you have "done_queue.put('STOP')", however one thread
may run that before another has added its final work result to the
done_queue. That will cause the following two lines:
On Thu, Jan 15, 2015 at 8:55 AM,   wrote:

> for status in iter(done_queue.get, 'STOP'):
> print status

to exit early, and not print the status of some of the work items.

For this, Chris A's other option is the best: due to the join loop just
above, you know all items have been entered into the done_queue, and thus,
there is no need for the STOP entry: just run until the queue has been
fully processed.

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


Re: Python 3 regex?

2015-01-14 Thread Steven D'Aprano
Thomas 'PointedEars' Lahn wrote:

> [email protected] wrote:
[...]
>> And, why not? compare Py3.2 and Py3.3+ !
> 
> What are you getting at?


Don't waste your time with JMF. He is obsessed with a trivial performance
regression in Python 3.3. Unicode strings can be slightly more expensive to
create in Python 3.3 compared to earlier versions, due to a clever memory
optimization which saves up to 50% if your strings are all in the Basic
Multilingual Plane and up to 75% if they are all in Latin-1. Never mind
that for real-world code, that memory saving can often lead to applications
running faster, JMF is obsessed with an artificial benchmark of his own
devising that involves making, and throwing away, thousands of Unicode
strings as fast as possible in such a way as to exercise the worst-case of
the new Unicode model. From this unimportant performance regression, he has
convinced himself that this means that Python 3.3 and beyond is logically
and mathematically in violation of the Unicode standard.

Any time JMF mentions anything to do with Python versions or Unicode or
ASCII or French, he is in full-blown "pi equals 3 exactly" crank territory
and is best ignored.



-- 
Steven

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


Re: Calling a derived class's constructor from a parent method

2015-01-14 Thread Steven D'Aprano
jason wrote:

> If I have a class hierarchy like so:
> 
>  
> class A(object):
>   def __init__(self, s):
> self.s = s
>   def foo(self, s):
> return A(s)

A.foo is broken, or at least rude.  Change it to this:

def foo(self, s):
return type(self)(s)



> class B(A):
>   def __init__(self, s):
> A.__init__(self, s)

Unrelated:

It is better to call super than manually call the superclass. Calling A
directly means your class is no longer compatible with multiple
inheritance.

def __init__(self, s):
super(B, self).__init__(s)


> I'm using Python 2.7.5, but I'm curious what the 3.x answer is too.


The answer in 3.x is the same, except that super() can auto-detect the right
arguments:

super().__init__(s)



-- 
Steven

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


type versus __class__

2015-01-14 Thread Steven D'Aprano
There are (at least) two ways to determine the type of something in Python:


type(obj)

obj.__class__


By design, they are not guaranteed to give the same result. Is there a
definitive explanation given by the docs for the difference and which you
should use under different circumstances?


In Python 2, the type() of classic classes is always the same thing:

py> class A:  # classic class
... def spam(self): return "from A"
...
py> class B:
... def spam(self): return "from B"
...
py> a = A()
py> b = B()
py> a.__class__ is b.__class__
False
py> type(a) is type(b)  # a special type "instance"
True


Writing to __class__ lets you modify the behaviour of the instance:

py> a.spam()
'from A'
py> a.__class__ = B
py> a.spam()
'from B'


New-style classes are different:

py> class C(object):  # new-style class
... def spam(self):
... return "from C"
...
py> class D(object):
... def spam(self):
... return "from D"
...
py> c = C()
py> d = D()
py> c.__class__ is d.__class__
False
py> type(c) is type(d)
False

Like classic classes, you can override the __class__ attribute on instances,
and change their behaviour:

py> c.spam()
'from C'
py> c.__class__ = D
py> c.spam()
'from D'


Any other differences? 


-- 
Steven

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


Re: Performance in exec environnements

2015-01-14 Thread Steven D'Aprano
Jean-Baptiste Braun wrote:

> 2015-01-13 22:48 GMT+01:00 Steven D'Aprano <
> [email protected]>:
> 
>> So you have been comparing:
>>
>> 2
>>
>> versus
>>
>> exec('1+1')
>>
>>
>> The first case just fetches a reference to a pre-existing int object, and
>> then deletes the reference. That's fast.
>>
>> The second case:
>>
>> - creates a new string '1+1'
>> - does a global lookup to find the built-in exec function
>> - passes the string to the function
>> - the function then parses that string and compiles it to byte-code
>> - runs the keyhole optimizer over it
>> - and finally executes the byte code for "2", same as above.
>>
>> Only the last step is the same as your earlier test case.
>>
> What I don't understand is the ratio between test 2 / 4 and test 1 / 3.
> 
> Let 0.0229 sec be the execution time to read a bytecode (1st test).
> Executing two times that bytecode takes 0.042 sec (test 3), which looks
> coherent.
> 
> Let 11.6 sec be the execution time to call the global exec, parse, do some
> stuff and read a bytecode (test 2). I'm trying to understand why does
> doing the same thing and reading one more bytecode is much longer (15.7
> sec) in comparison.

I don't know. What code are you comparing? If you are comparing these:

exec('1 + 1')
exec('1 + 1; 1 + 1')

the second one has twice as much source code to parse and compile.

You can eliminate much (but not all) of the overhead of exec by pulling the
compilation step out:

[steve@ando ~]$ python -m timeit "exec('1+1')"
10 loops, best of 3: 18.5 usec per loop
[steve@ando ~]$ python -m timeit -s \
"c = compile('1+1', '', 'exec')" "exec(c)"
100 loops, best of 3: 1.49 usec per loop


Obviously this only helps if you are executing the same code repeatedly.


>> Bigger savings come from avoiding exec. Instead, try to use factory
>> functions, closures, etc. If you give an example of what you are trying
>> to generate with exec, we may be able to offer an alternative.
>>
> I think I'm going to compile before exec'ing.
> 
> What I'm trying to do is to map a transformation description in a markup
> langage (XSLT) in python to improve execution time. Here is a
> simplification of what it looks like :
> 
> XSLT :
> 
>  
>   
>Mr
>   
>   
>Mrs
>   
>  
> 
> 
> Generated python :
> print('')
> if gender == 'M':
> print('Mr')
> else:
> print('Mrs')
> print('')


I'm not entirely sure of the context here. If you're only executing that "if
gender ==" block once, then trying to optimize this is a waste of time. The
time you take to pre-compile will be at least as much as the time you save
from a single call. Only if you have multiple calls will it help.

Ideally, instead of exec'ing a block of code, it would be better to create a
function and call the function:

# === Don't do this! ===
while parsing XSLT:
if tag = 'choose':
handle_title = """print('')
 if gender == 'M': print('Mr')
 else:  print('Mrs')
 print('')"""

# much later...
exec(handle_title)
print "Smith"
exec(handle_title)
print "Jones"


# === This is better ===
while parsing XSLT:
if tag = 'choose':
def handle_title():
 print('')
 if gender == 'M': print('Mr')
 else:  print('Mrs')
 print('')


# much later...
handle_title()
print "Smith"
handle_title()
print "Jones"


Understand that this is just a sketch of a solution, not an actual solution.
But the important thing is that if you can build an actual function rather
than exec'ing source code, that will be much faster. If the XSLT file only
has a small number of functions such as "choose", you may be able to use
factory functions to build the functions and avoid exec.

And if not... oh well. There is nothing wrong with using exec. True, it is a
little slower, but how much data do you have to process? Engaging in
unnecessary optimization to shave the run time from 20 minutes to 19
minutes is probably a waste of effort.




-- 
Steven

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


Re: List of "python -m" tools

2015-01-14 Thread alex23

On 14/01/2015 7:33 PM, Albert-Jan Roskam wrote:

You say "Python 2.7.9 and 3.x comes with an easy way to install pip. Run python -m 
ensurepip and pypi is at your service." .
But here https://docs.python.org/3/library/ensurepip.html it says that "This module 
does not access the internet. All of the components needed to bootstrap pip are included 
as internal parts of the package." So pypi is not involved, right?


The original comment doesn't mean that PyPI is involved in the 
installation, but that installing it allows for packages to be installed 
via PyPI. Obviously that isn't true if you're not net-enabled, but pip 
can still handy for installing local packages.


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


Re: Calling a derived class's constructor from a parent method

2015-01-14 Thread Mark Lawrence

On 15/01/2015 00:40, Steven D'Aprano wrote:

jason wrote:


class B(A):
   def __init__(self, s):
 A.__init__(self, s)


Unrelated:

It is better to call super than manually call the superclass. Calling A
directly means your class is no longer compatible with multiple
inheritance.

 def __init__(self, s):
 super(B, self).__init__(s)



For the OP an excellent article on super here 
https://rhettinger.wordpress.com/2011/05/26/super-considered-super/


--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


Re: Python 3 regex?

2015-01-14 Thread Rustom Mody
On Tuesday, January 13, 2015 at 10:06:50 AM UTC+5:30, Steven D'Aprano wrote:
> On Mon, 12 Jan 2015 19:48:18 +, Ian wrote:
> 
> > My recommendation would be to write a recursive decent parser for your
> > files.
> > 
> > That way will be easier to write,
> 
> I know that writing parsers is a solved problem in computer science, and 
> that doing so is allegedly one of the more trivial things computer 
> scientists are supposed to be able to do, but the learning curve to write 
> parsers is if anything even higher than the learning curve to write a 
> regex.
> 
> I wish that Python made it as easy to use EBNF to write a parser as it 
> makes to use a regex :-(
> 
> http://en.wikipedia.org/wiki/Extended_Backus-Naur_Form
> 
> 
> 
> -- 
> Steven

There appears to be at least one python package for this
https://pypi.python.org/pypi/iscconf

And for those wanting to use regexes to parse CFGs, the requried
reading is:

http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python 3 regex?

2015-01-14 Thread Rustom Mody
On Tuesday, January 13, 2015 at 10:06:50 AM UTC+5:30, Steven D'Aprano wrote:
> On Mon, 12 Jan 2015 19:48:18 +, Ian wrote:
> 
> > My recommendation would be to write a recursive decent parser for your
> > files.
> > 
> > That way will be easier to write,
> 
> I know that writing parsers is a solved problem in computer science, and 
> that doing so is allegedly one of the more trivial things computer 
> scientists are supposed to be able to do, 

"Solved-CS-problem" often is showing that the problem is 
unsolvable :-)

http://blog.reverberate.org/2013/08/parsing-c-is-literally-undecidable.html

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


Re: Comparisons and sorting of a numeric class....

2015-01-14 Thread Ian Kelly
On Wed, Jan 14, 2015 at 8:00 PM, Andrew Robinson
 wrote:
> Hi Ian,
> On 01/14/2015 12:31 AM, Ian Kelly wrote:
>
> On Tue, Jan 13, 2015 at 5:12 PM, Andrew Robinson
>  wrote:
>
> So -- even a cursory thought shows that the information could be encoded in
> a very few lines even without an instance of a subclass:
>
> class CAllFalse():
> @classmethod
> def __nonzero__(Kls): return False
>
> class CPartFalse():
> @classmethod
> def __nonzero__(Kls): return False
>
> This doesn't actually work.
>
> class CAllFalse(object):
>
> ...   @classmethod
> ...   def __nonzero__(cls): return False
> ...
>
> bool(CAllFalse)
>
> True
>
> Yes, but I never used it that way.
> If I had not put the classmethod on there, I could not have used it with a
> class name in lieu of an instance...
>
> eg:
> class Dummy():
> def __nonzero__(self): return False
>
> bool( Dummy.__nonzero__() )
 bool( Dummy.__nonzero__() )
> Traceback (most recent call last):
>   File "", line 1, in 
> TypeError: unbound method __nonzero__() must be called with Dummy instance
> as first argument (got nothing instead)
>
> When Python evaluates the "truthiness" of an object, it looks up the
> __nonzero__ method on the object's class, not on the object itself
>
> Unless I call the __nonzero__ method using the underscores as I did...

So are you always going to call __nonzero__ then?  When you have an
uncertain boolean that you want to use in an if expression, are you
going to write:

if value.__nonzero__():

every single time? Or is it possible that client code that isn't aware
of the extended bool might occasionally just use:

if value:

and thus interpret CAllFalse as True? The entire point of the
__nonzero__ method is to specify for the Python *interpreter* whether
it should treat the object as truish or falsish. If you're not
interested in overriding that, then __nonzero__ is the wrong name for
the method. Dunder names are reserved for use by Python, and when you
use one, it signals that this method has a special meaning to the
interpreter. If the interpreter doesn't actually use that, then you're
just sending a confusing signal to readers of your code.  Call your
method nonzero() or bool() or is_true() or whatever you want, but
don't call it __nonzero__ because that's not what it is.

> Here's a clue:
>
> It may be easier to understand my example if you take something into
> account:  D'Aprano at the end of his email reply quoted to me a page from
> the Python online manuals and called it a "Promise", rather than telling me
> why Guido made a choice in denying subclassing of bool in the first
> place (which is what I asked about...)

It seems to me the answer to that has been given to you several times
already in this thread, but you don't seem to want to accept it for
some reason. You can't subclass bool because instances of the subclass
would be new instances of bool, which would break the class invariant.
Without instances all you're left with are static methods, at which
point there is no particular reason to host them on a subclass.

> So -- If you will now notice, I
> specifically  went out of my way to use __cmp__ and __nonzero__ methods in
> my limited example and they are both methods which have been discontinued in
> Python 3 in favor of more boolean friendly versions...

That would seem like all the less reason to use them, if you have any
interest in Python 3 compatibility.

> Given that Van Rosen

You mean Van Rossum?

> said that he denied subclassing of bool to prevent
> instances of bool, (eg: as a means to an end, not the end itself...)  I can
> see working up an example with no instances as a proposal that might make
> Guido happy and allow me to work around Guido's objections; but on the other
> hand -- I don't see putting a lot of effort into it until all other
> reasonable options have been exhausted... becase I can't subclass bool, he
> has effectively made the approach as defective as all others... ( eg: Threw
> the baby out with the bath water... though he may have had a good reason to
> do it... )

It would also be pointless, as I noted above. If the subclass only
exists to host static methods, then why is it important that they be
on a subclass?

> It's because Guido van Rosen said that there should never be another
> *instance* of a bool.   Now, I'm really agreeing with you -- that I would
> really like to subclass bool, and simply make an instance out of it -- even
> if a limited singleton type ; but Guido said No.  And Guido also objected to
> any kind of duck type, although he didn't specifically change the language
> to prevent them ... yet ...

When did Guido ever object to duck typing?

> And most of this thread has been nothing more than me asking "why" did Guido
> say to do that -- and people avoiding answering the question.

Wait, are you actually asking why bool is a doubleton? If nobody has
answered that, I think probably nobody understood you were asking it,
because it shouldn't 

lambdak: multi-line lambda implementation in native Python

2015-01-14 Thread yawar . amin
Hi all,

First off, to each reader--if you believe that 'multi-line' lambdas are
no good and we can just use functions, decorators, &c. to accomplish
everything in Python, advance warning: this post will annoy you.

Now, the crux of my message. I have implemented what I believe is a
fairly robust, if ugly-looking, native Python module made up of
combinator functions which compose together to form function expressions
(well, callable expressions).

I've seen a lot of discussions on possibly introducing syntax support
for multi-line lambdas in some future version, but I've never seen
anyone say, here's a way you can do this in Python _today._ So I believe
lambdak fits in that niche.

https://github.com/yawaramin/lambdak

Would anyone care to try it out? I would be happy to answer questions
whenever I can.

Regards,

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


Re: type versus __class__

2015-01-14 Thread Ian Kelly
On Wed, Jan 14, 2015 at 6:26 PM, Steven D'Aprano
 wrote:
> Any other differences?

Objects of classic builtin types don't even have the __class__ attribute.

>>> type(A)

>>> A.__class__
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: class A has no attribute '__class__'
>>> type(C)

>>> C.__class__

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


Re: lambdak: multi-line lambda implementation in native Python

2015-01-14 Thread Steven D'Aprano
On Wed, 14 Jan 2015 21:54:52 -0800, yawar.amin wrote:

> Now, the crux of my message. I have implemented what I believe is a
> fairly robust, if ugly-looking, native Python module made up of
> combinator functions which compose together to form function expressions
> (well, callable expressions).

How about a demo?

I have a function, which I put into an expression like this:

def func(a, b=None):
global spam
import math
spam = [a, b]*3
print spam
del spam


value = [1, "hello", int, func]
del func

How would I use lambdak to write that as an expression

value = [1, "hello", int, ??? ]

without the intermediate def?


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


Re: lambdak: multi-line lambda implementation in native Python

2015-01-14 Thread Ian Kelly
On Wed, Jan 14, 2015 at 11:06 PM, Steven D'Aprano  wrote:
> I have a function, which I put into an expression like this:
>
> def func(a, b=None):
> global spam
> import math
> spam = [a, b]*3
> print spam
> del spam
>
>
> value = [1, "hello", int, func]
> del func
>
> How would I use lambdak to write that as an expression
>
> value = [1, "hello", int, ??? ]
>
> without the intermediate def?


# Untested, but seems like this should work.

value = [1, "hello", int, given_(lambda a, b=None:
import_("math", lambda math:
import_("operator", lambda operator:
do_(lambda: operator.setitem(globals(), 'spam', [a, b]*3), lambda:
print_(globals()['spam'], lambda:
do_(lambda: operator.delitem(globals(), 'spam')))]


To the OP: I note that although import_ is used in the examples, it's
not found in the list of functions in the README.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to terminate the function that runs every n seconds

2015-01-14 Thread Frank Millman

"Dennis Lee Bieber"  wrote in message 
news:[email protected]...
>
> Pseudo (Python 2.x) code
>
> def aThread(delay=600.0): #default 10 minutes
> while keepRunning:
> print "thread triggered at %s" % time.time()
> time.sleep(delay)
> print "thread was commanded to exit"

I read a variation on this theme some years ago that uses 
event.wait(timeout=delay) to manage the delay.

>From the docs, "block until another thread calls set() to set the flag to 
true, or until the optional timeout occurs."

The benefit is that setting the flag terminates the loop straight away, as 
opposed to waiting for the sleep to finish.

I can show some sample code if anyone is interested.

Frank Millman



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


Re: Comparisons and sorting of a numeric class....

2015-01-14 Thread Andrew Robinson


And most of this thread has been nothing more than me asking "why" 
did Guido

say to do that -- and people avoiding answering the question.

Wait, are you actually asking why bool is a doubleton? If nobody has
answered that, I think probably nobody understood you were asking it,
because it shouldn't need to be explained.

In some ways, yes I am asking that -- but in another I am not;
to be more precise -- Why did Guido refuse to allow refinement of 
meaning when in industry -- logic equations generally are not limited to 
Charles Bool's original work, but include practical enhancements of his 
theories which make them far more useful.


A subclass is generally backward compatible in any event -- as it is 
built upon a class, so that one can almost always revert to the base 
class's meaning when desired -- but subclassing allows extended meanings 
to be carried.  eg: A subclass of bool is a bool -- but it can be MORE 
than a bool in many ways.


One example: It can also be a union. So when Guido chose to cut off 
subclassing -- his decision had a wider impact than just the one he 
mentioned; eg: extra *instances* of True and False as if he were 
trying to save memory or something.


The reason Guido's action puzzles me is twofold -- first it has been 
standard industry practice to subclass singleton  (or n-ton) objects to 
expand their meaning in new contexts, and this practice has been 
documented for many years.  So -- why did Guido go against the general 
OOP practice unless he didn't know about it?


The mere existence of a subclass does not threaten the integrity of 
bools or security in Python in any way I can see -- as one can do a type 
check, or a base type check, on an instance to decide how to handle a 
subclass vs. base class instance. So I'm guessing he was concerned about 
something else, but I don't know what.


In general -- it's not the goal of subclassing to create more instances 
of the base types -- but rather to refine meaning in a way that can be 
automatically reverted to the base class value (when appropriate) and to 
signal to users that the type can be passed to functions that require a 
bool because of backward compatibility.



Boolean algebra has two values: true and false, or 1 and 0, or humpty
and dumpty, or whatever you like to call them.
You're speaking to an Electrical engineer.  I know there are 10 kinds of 
people, those who know binary and those who don't.  But you're way off 
base if you think most industrial quality code for manipulating boolean 
logic uses (or even can be restricted to use) only two values and still 
accomplish their tasks correctly and within a finite amount of time.



   The bool class
represents the values of boolean algebra. Therefore, there are two of
them. If I have an object that I know is an instance of bool, the
implication is that it is one of those two values, not something
potentially completely different.

Can you name any other language that *does* allow subclassing of
booleans or creation of new boolean values?
Yes. Several off the top of my head -- and I have mentioned these 
before.  They generally come with the extra subclasses pre-created and 
the user doesn't get to create the classes, but only use them; none the 
less -- they have more than two values with which to do logic equations 
with.


VHDL, Verilog, HDL, Silos III, and there are IEEE variants also.
C/C++ historically allowed you to do it with instances included, 
although I am not sure it still does.


The third value is usually called "TRI-state" or "don't care". (Though 
its sometimes a misnomer which means -- don't know, but do care.)


Most of these high definition languages are used to do things like 
design micorprocessors... eg: the very intel or arm processor you 
typically run python on --- because trying to do it with boolean logic 
and theorems of the past in a pencil and paper compatible strict 
re-incarnation of what Charles Bool's did in his own time (even if done 
by computer) -- rather than including De-morgan and all the many other 
people who contributed afterward -- is about as red-neck backward as one 
can get -- and often doomed to failure (though for small applications 
you might get away with it.)


Often, only one extra ( tri state ) value is needed to do logic 
verification and testing; but in some cases, notably, where the 
exclusive 'or' function is involved, the relationship between don't care 
inputs can become important and more values are required; eg: to detect 
when in deeply nested logic, various sources of don't care inputs 
interfere with each and themselves in constructive or destructive ways 
to produce constant logic Trues or Falses.


We've discovered that we live in a quantum-mechanical universe -- yet 
people still don't grasp the pragmatic issue that basic logic can be 
indeterminate at least some of the time ?!


The name 'boolean logic' has never been re-named in honor of the many 
people who developed the advancements i