Re: Few Coding suggestions - resending

2015-01-12 Thread Ganesh Pal
On Mon, Jan 12, 2015 at 8:42 AM, Ganesh Pal  wrote:
> On Sun, Jan 11, 2015 at 7:57 PM, Dave Angel  wrote:
>>>
>>>
>> No idea how that represents "a difference of 5 minutes".  So I'll take a 
>> totally wild guess that you meant:
>>
>> Sunday 23:50 23:55
>> Monday 00:00 00:05
>> Monday 00:10 00:15
>> Monday 00:20 00:25
>> Monday 00:30 00:35
>>
>> which would have the 2nd column 5 minutes after the first.
>>
>> You need another datetime object, let's call it 'end'
>>
>> end = start + timedelta(minutes=5)
>>
>> and now you want to yield three things, in a tuple:
>>
>> yield (start.strftime("%A"), start.strftime("%H:%M"),
>>end.strftime("%H:%M"))
>>
>> Of course that's a pain to do twice, as you have two yields in that 
>> function.  I'll leave you to reorder the loop to make that unnecessary.  
>> Hint, do the yield first, THEN increment the start variable.
>
> Thanks for the inputs , I tried doing the increments and decrements
> first and then Yield
>
>  Program:
>
>   Login-1@SNAP-BOX-1 new]$ cat time_range_01.py
> #!/usr/bin/python
> import time
> from datetime import date, time, datetime, timedelta
> #h = datetime.strftime("%H")
> #m = datetime.strftime("%M")
> h=23
> m=50
> d = date.today()
> print d
>
> def yield_times():
> global  h,m,d
> start = datetime.combine(d, time(int(h),int(m)))
> end = datetime.combine(d, time(int(h),int(m)))
> while True:
> start += timedelta(minutes=10)
> end = start + timedelta(minutes=5)
> 
> yield(start.strftime("%A"),start.strftime("%H:%M"),end.strftime("%H:%M"))
> gen = yield_times()
> for i in range(10):
> print gen.next()
>
> output :
> [Login-1@SNAP-BOX-1 new]$ python time_range_01.py
> 2015-01-12
> ('Tuesday', '00:00', '00:05')
> ('Tuesday', '00:10', '00:15')
> ('Tuesday', '00:20', '00:25')
> ('Tuesday', '00:30', '00:35')
> ('Tuesday', '00:40', '00:45')
> ('Tuesday', '00:50', '00:55')
> ('Tuesday', '01:00', '01:05')
> ('Tuesday', '01:10', '01:15')
> ('Tuesday', '01:20', '01:25')
> ('Tuesday', '01:30', '01:35')
>
> PS :   Except formatting the results looks pretty much what i was
> expecting .I was pretty much happy to see this last night :) .
>
>
>> and in the other loop, you want
>> res = "{} {} {}\n".format(gen.next)
>> print res,
>>
>>>
>
> Thanks ,  I will have to modify for the new output
>
>>> (b)  how to copy the above output (i.e with the new column to a file.)
>>>
>>
>> Instead of printing, just do f.write(res).  Or do both. Notice that to make 
>> this easy, i avoided doing any formatting in the print statement. I used a 
>> trailing comma on it to avoid print adding a newline, and instead put it 
>> explicitly in the format method call.
>>
>>>
>
> Sure
>
>>> (c)  The  final output should be a file , having the entries  day ,
>>> start , end time of the remote file. how do  i pass the  this to
>>> yeild-times()
>>>
>>> import time
>>>
>>> /* Assuming I retrive the below values h,m,d from the remote machine*/
>>>
>>> h = time.strftime("%H")
>>> m = time.strftime("%M")
>>> d = date.today()
>>>
>>
>> Those first two are strings, but the third is a datetime object.  That last 
>> can be painful to pass between machines.
>
> Correct , also looks like I have another issue , if I happen to pass
> the first two strings
>
> #  instead hard coded value i.e h = 23 and y =50 , i try using the
> string directly I end up getting the below error :
>
> h = datetime.strftime("%H")
> m = datetime.strftime("%M")
>
> [Login-1@SNAP-BOX-1 new]$ python  time_range_01.py
> Traceback (most recent call last):
>   File "time_range_01.py", line 4, in 
> h = datetime.strftime("%H")
> TypeError: descriptor 'strftime' requires a 'datetime.date' object but
> received a 'str'
>
> The date time object is retrieved once once from the remote machine
> and just for building the file in the required format.
> Do you foresee any problems using this ?
>
>>
>>>
>>> def yield_times():
>>>global h,m,d
>>
>>
>> No idea what that's all about.  If you want to pass arguments to 
>> yield_times(), put them inside the parens.
>>
> sure .
>
> Regards,
> Gpal
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: application console with window

2015-01-12 Thread Amir Arsalan
and u have another below choice

http://docs.python.org/howto/curses.html
http://urwid.org/



On Thu, Jan 8, 2015 at 12:54 AM, Amir Arsalan 
wrote:

> Hi adam,
>
> U can use from npyscreen.
>
> Npyscreen.readthedocs.org
> On Jan 8, 2015 12:50 AM, "adam"  wrote:
>
>> Is in here maybe someone who speak Polish?
>>
>> I would like to write application witch looks like this
>> http://linuxiarze.pl/obrazy/internet1/ceni1.png
>>
>> I'm looking for some libs, tutorials, or other informations.
>> I'm searching this informations for python3.
>>
>> adam
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>>
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: application console with window

2015-01-12 Thread Amir Arsalan
Hi adam,

U can use from npyscreen.

Npyscreen.readthedocs.org
On Jan 8, 2015 12:50 AM, "adam"  wrote:

> Is in here maybe someone who speak Polish?
>
> I would like to write application witch looks like this
> http://linuxiarze.pl/obrazy/internet1/ceni1.png
>
> I'm looking for some libs, tutorials, or other informations.
> I'm searching this informations for python3.
>
> adam
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Happy new year with Dipy 0.8!

2015-01-12 Thread Eleftherios Garyfallidis
Dear all,


We are very happy to announce a new release of Diffusion Imaging in Python
(Dipy).


Here is a summary of the most important new features and developments.


DIPY 0.8.0 (Released on Tuesday, 6 Jan 2015)


Nonlinear Image-based Registration (SyN)


An implementation of the Symmetric Normalization method for nonlinear
diffeomorphic registration. This implementation is lightweight, and does
not depend on ITK or ANTS. It is written entirely in Python and Cython.


Streamline-based Linear Registration (SLR)


A new method that allows direct registration of bundles of streamlines.
Especially useful for creating atlases of specific types of bundles.


Linear Fascicle Evaluation (LiFE)


This is a Python implementation of a new method for evaluation of
tractrography solutions.


Sparse Fascicle Model (SFM)


A new signal reconstruction method , added to the large stack of
reconstruction models already available in Dipy, including implementations
of CSD and SHORE.


Non-local means denoising (NLMEANS)


Denoising is a technique that can boost most of your analysis techniques as
it can increase the signal to noise ratio of your data. We started this new
module by implementing a very generic denoising technique that can be used
also for fMRI and T1 images.


New modular tracking machinery


This is a collection of new objects which allows rapid development of new
fiber tracking algorithms.


In summary, since January 2014 (version 0.7.1), we closed 388 issues and
merged 155 pull requests. The project now has a total of more than 4000
commits and 29 contributors.


We would appreciate if you could forward this information to any interested
individuals or labs.


Yours sincerely,


On behalf of the Dipy developers,

Eleftherios Garyfallidis

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


Re: Array of hash table

2015-01-12 Thread Jason Friedman
>Can any one tell me how to create
>graph={
>   "nodes": [
> {
>   "id": "n0",
>   "label": "A node",
>   "x": 0,
[ ... elided ... ]
> }
>   ]
> }


Taking a guess and guessing that graphviz might be useful for you:
https://code.google.com/p/pydot/.
-- 
https://mail.python.org/mailman/listinfo/python-list


PythonFOSDEM 2015 - Schedule Updated… We will start at 10:30 AM

2015-01-12 Thread Stéphane Wirtel

Dear Python Community,

Because the FOSDEM will start at 10:30 AM on Saturday 31st January, the 
first talk will be at 10:30 AM.


Thus all the talks will be ajusted. The Schedule on the site of FOSDEM 
and on the PythonFOSDEM site have been updated.


https://fosdem.org/2015/schedule/track/python/

Regards,

Stephane
--
Stéphane Wirtel - http://wirtel.be - @matrixise
--
https://mail.python.org/mailman/listinfo/python-list


Reg: scrappring error

2015-01-12 Thread Sushanth
​Traceback (most recent call last):
  File "sample.py", line 28, in 
scrape_data(row['All Samples'],row['URL'])
  File "sample.py", line 13, in scrape_data
html = etree.HTML(urllib2.urlopen(url).read())
  File
"C:\Users\sushanth.thangamani\AppData\Local\Continuum\Anaconda\lib\urllib
2.py", line 127, in urlopen
return _opener.open(url, data, timeout)
  File
"C:\Users\sushanth.thangamani\AppData\Local\Continuum\Anaconda\lib\urllib
2.py", line 410, in open
response = meth(req, response)
  File
"C:\Users\sushanth.thangamani\AppData\Local\Continuum\Anaconda\lib\urllib
2.py", line 523, in http_response
'http', request, response, code, msg, hdrs)
  File
"C:\Users\sushanth.thangamani\AppData\Local\Continuum\Anaconda\lib\urllib
2.py", line 442, in error
result = self._call_chain(*args)
  File
"C:\Users\sushanth.thangamani\AppData\Local\Continuum\Anaconda\lib\urllib
2.py", line 382, in _call_chain
result = func(*args)
  File
"C:\Users\sushanth.thangamani\AppData\Local\Continuum\Anaconda\lib\urllib
2.py", line 629, in http_error_302
return self.parent.open(new, timeout=req.timeout)
  File
"C:\Users\sushanth.thangamani\AppData\Local\Continuum\Anaconda\lib\urllib
2.py", line 410, in open
response = meth(req, response)
  File
"C:\Users\sushanth.thangamani\AppData\Local\Continuum\Anaconda\lib\urllib
2.py", line 523, in http_response
'http', request, response, code, msg, hdrs)
  File
"C:\Users\sushanth.thangamani\AppData\Local\Continuum\Anaconda\lib\urllib
2.py", line 442, in error
result = self._call_chain(*args)
  File
"C:\Users\sushanth.thangamani\AppData\Local\Continuum\Anaconda\lib\urllib
2.py", line 382, in _call_chain
result = func(*args)
  File
"C:\Users\sushanth.thangamani\AppData\Local\Continuum\Anaconda\lib\urllib
2.py", line 629, in http_error_302
return self.parent.open(new, timeout=req.timeout)
  File
"C:\Users\sushanth.thangamani\AppData\Local\Continuum\Anaconda\lib\urllib
2.py", line 410, in open
response = meth(req, response)
  File
"C:\Users\sushanth.thangamani\AppData\Local\Continuum\Anaconda\lib\urllib
2.py", line 523, in http_response
'http', request, response, code, msg, hdrs)
  File
"C:\Users\sushanth.thangamani\AppData\Local\Continuum\Anaconda\lib\urllib
2.py", line 442, in error
result = self._call_chain(*args)
  File
"C:\Users\sushanth.thangamani\AppData\Local\Continuum\Anaconda\lib\urllib
2.py", line 382, in _call_chain
result = func(*args)
  File
"C:\Users\sushanth.thangamani\AppData\Local\Continuum\Anaconda\lib\urllib
2.py", line 629, in http_error_302
return self.parent.open(new, timeout=req.timeout)
  File
"C:\Users\sushanth.thangamani\AppData\Local\Continuum\Anaconda\lib\urllib
2.py", line 410, in open
response = meth(req, response)
  File
"C:\Users\sushanth.thangamani\AppData\Local\Continuum\Anaconda\lib\urllib
2.py", line 523, in http_response
'http', request, response, code, msg, hdrs)
  File
"C:\Users\sushanth.thangamani\AppData\Local\Continuum\Anaconda\lib\urllib
2.py", line 442, in error
result = self._call_chain(*args)
  File
"C:\Users\sushanth.thangamani\AppData\Local\Continuum\Anaconda\lib\urllib
2.py", line 382, in _call_chain
result = func(*args)
  File
"C:\Users\sushanth.thangamani\AppData\Local\Continuum\Anaconda\lib\urllib
2.py", line 629, in http_error_302
return self.parent.open(new, timeout=req.timeout)
  File
"C:\Users\sushanth.thangamani\AppData\Local\Continuum\Anaconda\lib\urllib
2.py", line 410, in open
response = meth(req, response)
  File
"C:\Users\sushanth.thangamani\AppData\Local\Continuum\Anaconda\lib\urllib
2.py", line 523, in http_response
'http', request, response, code, msg, hdrs)
  File
"C:\Users\sushanth.thangamani\AppData\Local\Continuum\Anaconda\lib\urllib
2.py", line 442, in error
result = self._call_chain(*args)
  File
"C:\Users\sushanth.thangamani\AppData\Local\Continuum\Anaconda\lib\urllib
2.py", line 382, in _call_chain
result = func(*args)
  File
"C:\Users\sushanth.thangamani\AppData\Local\Continuum\Anaconda\lib\urllib
2.py", line 629, in http_error_302
return self.parent.open(new, timeout=req.timeout)
  File
"C:\Users\sushanth.thangamani\AppData\Local\Continuum\Anaconda\lib\urllib
2.py", line 410, in open
response = meth(req, response)
  File
"C:\Users\sushanth.thangamani\AppData\Local\Continuum\Anaconda\lib\urllib
2.py", line 523, in http_response
'http', request, response, code, msg, hdrs)
  File
"C:\Users\sushanth.thangamani\AppData\Local\Continuum\Anaconda\lib\urllib
2.py", line 442, in error
result = self._call_chain(*args)
  File
"C:\Users\sushanth.thangamani\AppData\Local\Continuum\Anaconda\lib\urllib
2.py", line 382, in _call_chain
result = func(*args)
  File
"C:\Users\sushanth.thangamani\AppData\Local\Continuum\Anaconda\lib\urllib
2.py", line 629, in http_error_302
return self.parent.open(new, timeout=req.timeout)
  File
"C:\Users\sushanth.thangamani\AppData\Local\Continuum\Anaconda\lib\urllib
2.py", line 410, i

Track down SIGABRT

2015-01-12 Thread Israel Brewster
I have a long-running python/CherryPy Web App server process that I am running on Mac OS X 10.8.5. Python 2.7.2 running in 32-bit mode (for now, I have the code in place to change over to 64 bit, but need to schedule the downtime to do it). On the 6th of this month, during normal operation from what I can tell, and after around 33 days of trouble-free uptime, the python process crashed with a SIGABRT. I restarted the process, and everything looked good again until yesterday, when it again crashed with a SIGABRT. The crash dump the system gave me doesn't tell me much, other than that it looks like python is calling some C function when it crashes. I've attached the crash report, in case it can mean something more to someone else.Can anyone give me some hints as to how to track down the cause of this crash? It's especially problematic since I can't mess with the live server for testing, and it is quite a while between crashes, making it difficult, if not impossible, to reproduce in testing. Thanks.
---Israel BrewsterSystems Analyst IIRavn Alaska5245 Airport Industrial RdFairbanks, AK 99709(907) 450-7293---BEGIN:VCARD
VERSION:3.0
N:Brewster;Israel;;;
FN:Israel Brewster
ORG:Frontier Flying Service;MIS
TITLE:PC Support Tech II
EMAIL;type=INTERNET;type=WORK;type=pref:[email protected]
TEL;type=WORK;type=pref:907-450-7293
item1.ADR;type=WORK;type=pref:;;5245 Airport Industrial Wy;Fairbanks;AK;99701;
item1.X-ABADR:us
CATEGORIES:General
X-ABUID:36305438-95EA-4410-91AB-45D16CABCDDC\:ABPerson
END:VCARD




Python_2015-01-08-152219_minilogger.crash
Description: Binary data
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: annoying doctest problem

2015-01-12 Thread Skip Montanaro
ISTR that when Tim Peters first implemented first, the typical way you were
expected to get tests into a doc string was to copy from an interactive
session, which would not have this problem.

Also, to Steven's comment about fussiness, it isn't so much that it's
fussy. It's more that it's dumb. I just does a simple string comparison of
the expected and actual outputs. It would be impossible for doctest to know
whether the expected output was something like repr or str output, and thus
safe to exchange single for double (don't forget to escape other embedded
quotes!), or was some sort of user-generated string, perhaps intended to be
text in  another programming language which has different quoting rules
than Python. Therefore, fussy (or dumb) is exactly what you want.

I-said-what-I-meant-ly y'rs,

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


unidecode

2015-01-12 Thread Jacob Kruger
Along lines of recent queries/questions, which had mostly sorted out, but, 
which was still trying to fine tune with regards to text file formats, in terms 
of writing as strings, or as binary files, and where was still trying to decide 
if wanted to worry about maybe specifically just handling forms of 
manual/hard-coded translations of certain accented characters which might 
relate to some of them that might get used quite often in names, etc., I just 
came across the following unidecode module:
https://pypi.python.org/pypi/Unidecode/

Seems to just take any unicode value, and 'translate' it into a string value, 
and will apparently try handle some forms of character substitution - but will 
play around with it a bit more, and let's see...

Stay well

Jacob Kruger
Blind Biker
Skype: BlindZA
"Roger Wilco wants to welcome you...to the space janitor's closet..."

- Original Message - 
From: Jacob Kruger 
To: Jacob Kruger 
Sent: Monday, January 12, 2015 2:15 PM
Subject: unidecode


https://pypi.python.org/pypi/Unidecode/

Jacob Kruger
Blind Biker
Skype: BlindZA
"Roger Wilco wants to welcome you...to the space janitor's closet..."
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: annoying doctest problem

2015-01-12 Thread Skip Montanaro
On Mon, Jan 12, 2015 at 6:15 AM, Skip Montanaro
 wrote:
> ... first implemented first ...

s/first/doctest/

Darn auto-correct...

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


Re: annoying doctest problem

2015-01-12 Thread Steven D'Aprano
Skip Montanaro wrote:

> ISTR that when Tim Peters first implemented first, the typical way you
> were expected to get tests into a doc string was to copy from an
> interactive session, which would not have this problem.

I believe that is still documented as the way to generate doctests.
 
> Also, to Steven's comment about fussiness, it isn't so much that it's
> fussy. It's more that it's dumb. I just does a simple string comparison of
> the expected and actual outputs. It would be impossible for doctest to
> know whether the expected output was something like repr or str output,
> and thus safe to exchange single for double (don't forget to escape other
> embedded quotes!), or was some sort of user-generated string, perhaps
> intended to be
> text in  another programming language which has different quoting rules
> than Python. Therefore, fussy (or dumb) is exactly what you want.

I didn't mean to give the impression that doctest was wrong to be fussy, or
dumb if you prefer. I think it's exactly the right behaviour.



-- 
Steven

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


Re: Few Coding suggestions - resending

2015-01-12 Thread Ganesh Pal
Thanks to all and  dear Dave Angel, I got this working :)  , using
your valuable  inputs .

Here is the final program:
Throttling-1# cat sleep_schedule_file.py
#!/usr/bin/python
import time
import pdb
from datetime import date, time, datetime, timedelta

#'''run these on the remote commmand '''

n = datetime.now()
h = n.strftime("%H")
m = n.strftime("%M")
d = date.today()

#''' get the sleep_schedule_structure '''

def yield_times():
start = datetime.combine(d, time(int(h),int(m)))
end = datetime.combine(d, time(int(h),int(m)))
while True:
start += timedelta(minutes=4)
end = start + timedelta(minutes=2)

yield(start.strftime("%A"),start.strftime("%H:%M"),end.strftime("%H:%M"))
gen = yield_times()

f = open('/tmp/sleep_sched.conf','wb')
for ii in range(10):
 a,b,c = gen.next()
 res = "{0} {1} {2} \n".format(a,b,c)
 print res,
 f.write(res)

f.close()

Gpal

On Mon, Jan 12, 2015 at 3:23 PM, Ganesh Pal  wrote:
> On Mon, Jan 12, 2015 at 8:42 AM, Ganesh Pal  wrote:
>> On Sun, Jan 11, 2015 at 7:57 PM, Dave Angel  wrote:


>>> No idea how that represents "a difference of 5 minutes".  So I'll take a 
>>> totally wild guess that you meant:
>>>
>>> Sunday 23:50 23:55
>>> Monday 00:00 00:05
>>> Monday 00:10 00:15
>>> Monday 00:20 00:25
>>> Monday 00:30 00:35
>>>
>>> which would have the 2nd column 5 minutes after the first.
>>>
>>> You need another datetime object, let's call it 'end'
>>>
>>> end = start + timedelta(minutes=5)
>>>
>>> and now you want to yield three things, in a tuple:
>>>
>>> yield (start.strftime("%A"), start.strftime("%H:%M"),
>>>end.strftime("%H:%M"))
>>>
>>> Of course that's a pain to do twice, as you have two yields in that 
>>> function.  I'll leave you to reorder the loop to make that unnecessary.  
>>> Hint, do the yield first, THEN increment the start variable.
>>
>> Thanks for the inputs , I tried doing the increments and decrements
>> first and then Yield
>>
>>  Program:
>>
>>   Login-1@SNAP-BOX-1 new]$ cat time_range_01.py
>> #!/usr/bin/python
>> import time
>> from datetime import date, time, datetime, timedelta
>> #h = datetime.strftime("%H")
>> #m = datetime.strftime("%M")
>> h=23
>> m=50
>> d = date.today()
>> print d
>>
>> def yield_times():
>> global  h,m,d
>> start = datetime.combine(d, time(int(h),int(m)))
>> end = datetime.combine(d, time(int(h),int(m)))
>> while True:
>> start += timedelta(minutes=10)
>> end = start + timedelta(minutes=5)
>> 
>> yield(start.strftime("%A"),start.strftime("%H:%M"),end.strftime("%H:%M"))
>> gen = yield_times()
>> for i in range(10):
>> print gen.next()
>>
>> output :
>> [Login-1@SNAP-BOX-1 new]$ python time_range_01.py
>> 2015-01-12
>> ('Tuesday', '00:00', '00:05')
>> ('Tuesday', '00:10', '00:15')
>> ('Tuesday', '00:20', '00:25')
>> ('Tuesday', '00:30', '00:35')
>> ('Tuesday', '00:40', '00:45')
>> ('Tuesday', '00:50', '00:55')
>> ('Tuesday', '01:00', '01:05')
>> ('Tuesday', '01:10', '01:15')
>> ('Tuesday', '01:20', '01:25')
>> ('Tuesday', '01:30', '01:35')
>>
>> PS :   Except formatting the results looks pretty much what i was
>> expecting .I was pretty much happy to see this last night :) .
>>
>>
>>> and in the other loop, you want
>>> res = "{} {} {}\n".format(gen.next)
>>> print res,
>>>

>>
>> Thanks ,  I will have to modify for the new output
>>
 (b)  how to copy the above output (i.e with the new column to a file.)

>>>
>>> Instead of printing, just do f.write(res).  Or do both. Notice that to make 
>>> this easy, i avoided doing any formatting in the print statement. I used a 
>>> trailing comma on it to avoid print adding a newline, and instead put it 
>>> explicitly in the format method call.
>>>

>>
>> Sure
>>
 (c)  The  final output should be a file , having the entries  day ,
 start , end time of the remote file. how do  i pass the  this to
 yeild-times()

 import time

 /* Assuming I retrive the below values h,m,d from the remote machine*/

 h = time.strftime("%H")
 m = time.strftime("%M")
 d = date.today()

>>>
>>> Those first two are strings, but the third is a datetime object.  That last 
>>> can be painful to pass between machines.
>>
>> Correct , also looks like I have another issue , if I happen to pass
>> the first two strings
>>
>> #  instead hard coded value i.e h = 23 and y =50 , i try using the
>> string directly I end up getting the below error :
>>
>> h = datetime.strftime("%H")
>> m = datetime.strftime("%M")
>>
>> [Login-1@SNAP-BOX-1 new]$ python  time_range_01.py
>> Traceback (most recent call last):
>>   File "time_range_01.py", line 4, in 
>> h = datetime.strftime("%H")
>> TypeError: descriptor 'strftime' requires a 'datetime.date' object but
>> received a 'str'
>>
>> The date time object is retrieved once once from the remote machine
>> and just for building the file in the required format.
>> Do you foresee any problems using this ?
>>

Re: class-based class decorator

2015-01-12 Thread Albert-Jan Roskam


- Original Message -

> From: Ian Kelly 
> To: Python 
> Cc: 
> Sent: Saturday, January 10, 2015 7:30 AM
> Subject: Re: class-based class decorator
> 
> On Fri, Jan 9, 2015 at 2:26 PM, Albert-Jan Roskam
> 
>  wrote:
>> 
>> 
>>  Hi,
>> 
>>  I am trying to write a class decorator that checks whether deprecated 
> parameters with non-default
>> 
>>  arguments are used. More complete code is here: 

> http://pastebin.com/ZqnMis6M.

Oops, the period was included in the url. It was 
http://pastebin.com/ZqnMis6M

>> In the code below,>>  how should I modify __call__ such that f.bar(old="oh 
>> no") prints 
> "hello world"?
>>  I thought it would be a fun way to play with the inspect module, but my 
> head is numb now and I am thirsty for a beer!
> 
> You can use inspect.getargspec to look up the default argument values
> for the target function. You can use inspect.getcallargs to map your
> *args and **kwargs to a dictionary of argument names and values. Since
> the target function will be the unbound __init__ method, you'll also
> want to pass in a dummy value like None for the self argument. Then
> you just look up each of the deprecated names in the result and flag
> any where the value doesn't match the default.
> 
> If you were using Python 3.3+, then I would recommend using
> inspect.signature instead, which is a lot more powerful. But your code
> appears to be Python 2, so we work with what we've got.


Hi Ian, Chris,

Thanks for your replies. I changed it into a regular decorator (not a class 
decorator). It would have been even nicer if I only needed to specify it once 
per class, but, well, in my case this hardly matters. The code below works as 
intended. One problem (not specific to the code): the decorator destroys the 
signature: it always becomes "*args, **kwargs"). This is annoying with help(), 
but what worries me most is that my Sphinx documentation is also affected. The 
information about the defaults gets lost (the parameters are decribed in the 
docstring, but not the defaults). That kind of sucks. Is there a builtin way 
around this (in other words: I am aware of this package: 
https://pypi.python.org/pypi/decorator). I am hoping to get the code working on 
Python 2.7 and 3.3 and up.

import functools
import inspect
import warnings

warnings.simplefilter("always")

class check_deprecated_args(object):

def __init__(self, deprecated_params, msg=None):
self.deprecated_params = deprecated_params
self.msg = msg

def __call__(self, func):
@functools.wraps(func)
def inner(*args, **kwargs):
argspec = inspect.getargspec(func)
default_signature = dict(zip(argspec.args[1:], argspec.defaults))
callargs = inspect.getcallargs(func, *args, **kwargs)
deprecated_calls = [(p, a) for p, a in callargs.items() if 
 p in self.deprecated_params and 
 a != default_signature[p]]
for (param, arg) in deprecated_calls:
msg = "you're using obsolete parameters in %s: [%s:%s]" 
msg = msg % (func.__name__, param, arg)
msg = msg + " " + self.msg if self.msg else msg 
warnings.warn(msg, DeprecationWarning, stacklevel=2)
return func(*args, **kwargs)
functools.update_wrapper(inner, func)
return inner

if __name__ == "__main__":
class Foo(object):

@check_deprecated_args(["old", "older"], "use 'brand_new' param 
instead")
def __init__(self, old="old", older="ancient"):
print "hello"

@check_deprecated_args(deprecated_params=["old", "older"])
def bar(self, old="default"):
print "world"

f = Foo(old="old", older="dino era")
f.bar("gnarly")

help(f)  # now the signature is *args, **kwargs, which makes my Sphinx 
documentation less readable!

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


Re: annoying doctest problem

2015-01-12 Thread Mark Lawrence

On 12/01/2015 13:00, Steven D'Aprano wrote:

Skip Montanaro wrote:


ISTR that when Tim Peters first implemented first, the typical way you
were expected to get tests into a doc string was to copy from an
interactive session, which would not have this problem.


I believe that is still documented as the way to generate doctests.


Also, to Steven's comment about fussiness, it isn't so much that it's
fussy. It's more that it's dumb. I just does a simple string comparison of
the expected and actual outputs. It would be impossible for doctest to
know whether the expected output was something like repr or str output,
and thus safe to exchange single for double (don't forget to escape other
embedded quotes!), or was some sort of user-generated string, perhaps
intended to be
text in  another programming language which has different quoting rules
than Python. Therefore, fussy (or dumb) is exactly what you want.


I didn't mean to give the impression that doctest was wrong to be fussy, or
dumb if you prefer. I think it's exactly the right behaviour.



If doctest is dumb then that's clearly down to the author.  Perhaps we 
should refer him or her to the Zen of Python so they don't repeat the 
mistake with future design decisions?


--
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: class-based class decorator

2015-01-12 Thread Jean-Michel Pichavant
- Original Message -
> From: "Albert-Jan Roskam" 
> import functools
> import inspect
> import warnings
> 
> warnings.simplefilter("always")
> 
> class check_deprecated_args(object):
> 
> def __init__(self, deprecated_params, msg=None):
> self.deprecated_params = deprecated_params
> self.msg = msg
> 
> def __call__(self, func):
> @functools.wraps(func)
> def inner(*args, **kwargs):
> argspec = inspect.getargspec(func)
> default_signature = dict(zip(argspec.args[1:],
> argspec.defaults))
> callargs = inspect.getcallargs(func, *args, **kwargs)
> deprecated_calls = [(p, a) for p, a in callargs.items()
> if
>  p in self.deprecated_params and
>  a != default_signature[p]]
> for (param, arg) in deprecated_calls:
> msg = "you're using obsolete parameters in %s:
> [%s:%s]"
> msg = msg % (func.__name__, param, arg)
> msg = msg + " " + self.msg if self.msg else msg
> warnings.warn(msg, DeprecationWarning, stacklevel=2)
> return func(*args, **kwargs)
> functools.update_wrapper(inner, func)
> return inner
> 
> if __name__ == "__main__":
> class Foo(object):
> 
> @check_deprecated_args(["old", "older"], "use 'brand_new'
> param instead")
> def __init__(self, old="old", older="ancient"):
> print "hello"
> 
> @check_deprecated_args(deprecated_params=["old", "older"])
> def bar(self, old="default"):
> print "world"
> 
> f = Foo(old="old", older="dino era")
> f.bar("gnarly")
> 
> help(f)  # now the signature is *args, **kwargs, which makes my
> Sphinx documentation less readable!
> 
> Best wishes,
> Albert-Jan

I don't really understand how you successfuly manage positional parameters, 
since the caller may not name them.
I'm asking because if your intend to check only the keyword parameters, there's 
a much simplier solution.

JM


-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be 
privileged. If you are not the intended recipient, please notify the sender 
immediately and do not disclose the contents to any other person, use it for 
any purpose, or store or copy the information in any medium. Thank you.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: annoying doctest problem

2015-01-12 Thread Ned Batchelder

On 1/11/15 11:20 PM, [email protected] wrote:


It failed with an unknown reason that evaluate two expected equal value but got 
an unexpected result! I'm struggling with this problem for a long time. Did I 
did something wrong? And how do I to fix it?

any help is appreciated. :)



My recommendation is to use doctest to test the code samples that 
naturally occur in your docstrings, but not to use it as a 
general-purpose testing tool.  It has too many limitations and quirks, 
and if you're going to write separate test methods anyway, why not just 
write actual code?


More detail here: 
http://nedbatchelder.com/blog/200811/things_i_dont_like_about_doctest.html



--
Ned Batchelder, http://nedbatchelder.com

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


Re: annoying doctest problem

2015-01-12 Thread Steven D'Aprano
Mark Lawrence wrote:

> If doctest is dumb then that's clearly down to the author.  Perhaps we
> should refer him or her to the Zen of Python so they don't repeat the
> mistake with future design decisions?

o_O


I don't even ... 

wait ... 

[starts typing]

[stops typing]

... okay.

-- 
Steven

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


Re: annoying doctest problem

2015-01-12 Thread Mark Lawrence

On 12/01/2015 15:58, Steven D'Aprano wrote:

Mark Lawrence wrote:


If doctest is dumb then that's clearly down to the author.  Perhaps we
should refer him or her to the Zen of Python so they don't repeat the
mistake with future design decisions?


o_O


I don't even ...

wait ...

[starts typing]

[stops typing]

... okay.



Drat, drat and double drat, for just one minute I thought I'd hooked a 
large, highly respected Antipodean :)


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


I must be missing something obvious in installing Python 3.4.2...

2015-01-12 Thread Andrew Koenig
Downloaded and installed 64-bit Python 3.4 and pywin32-219. Both installed 
smoothly on my 64-bit Win7 machine. I added C:\Python34 to the search path.

If I launch a Windows command window and run

python -m ensurepip

I get the following:

Ignoring indexes: https://pypi.python.org/simple/
Downloading/unpacking setuptools
Cleaning up...
Exception:
Traceback (most recent call last):
  File "C:\Users\ark\AppData\Local\Temp\tmpe3ckts9f\pip-1.5.6-py2.py3-none-any.w
hl\pip\basecommand.py", line 122, in main
status = self.run(options, args)
  File "C:\Users\ark\AppData\Local\Temp\tmpe3ckts9f\pip-1.5.6-py2.py3-none-any.w
hl\pip\commands\install.py", line 278, in run
requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundl
e=self.bundle)
  File "C:\Users\ark\AppData\Local\Temp\tmpe3ckts9f\pip-1.5.6-py2.py3-none-any.w
hl\pip\req.py", line 1177, in prepare_files
url = finder.find_requirement(req_to_install, upgrade=self.upgrade)
  File "C:\Users\ark\AppData\Local\Temp\tmpe3ckts9f\pip-1.5.6-py2.py3-none-any.w
hl\pip\index.py", line 209, in find_requirement
file_locations, url_locations = self._sort_locations(locations)
  File "C:\Users\ark\AppData\Local\Temp\tmpe3ckts9f\pip-1.5.6-py2.py3-none-any.w
hl\pip\index.py", line 128, in _sort_locations
sort_path(os.path.join(path, item))
  File "C:\Users\ark\AppData\Local\Temp\tmpe3ckts9f\pip-1.5.6-py2.py3-none-any.w
hl\pip\index.py", line 109, in sort_path
if mimetypes.guess_type(url, strict=False)[0] == 'text/html':
  File "C:\Python34\lib\mimetypes.py", line 287, in guess_type
init()
  File "C:\Python34\lib\mimetypes.py", line 348, in init
db.read_windows_registry()
  File "C:\Python34\lib\mimetypes.py", line 255, in read_windows_registry
with _winreg.OpenKey(hkcr, subkeyname) as subkey:
TypeError: OpenKey() argument 2 must be str without null characters or None, not
 str

The pip.log file contains the same text.

I'm sure I'm doing something obviously wrong here--but what?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: annoying doctest problem

2015-01-12 Thread Skip Montanaro
On Mon, Jan 12, 2015 at 7:00 AM, Steven D'Aprano <
[email protected]> wrote:

> I didn't mean to give the impression that doctest was wrong to be fussy, or
> dumb if you prefer. I think it's exactly the right behaviour.
>

I wasn't actually concerned that Steven might have misunderstood how
doctest works, just that other less experienced programmers might interpret
his statement as suggesting that doctest's comparisons were somehow wrong
or might be made less "fussy". That word suggests to me that there is some
sort of critical evaluation by the fussy party going on (doctest, in this
case), when there is none. Which is why I chose the word "dumb."

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


Machine Learning Startup Hiring

2015-01-12 Thread Charles Weitzer
Hello,

My name is Charles Weitzer. I do recruiting with a focus on quantitative 
sciences. One of my clients is a machine learning startup
located in Northern California. The founders include a successful veteran 
entrepreneur with a PhD in CS from Stanford, while the
other founder is on the faculty at UC Berkeley. Their team has made unpublished 
discoveries in the field of Machine Learning and in
other areas as well.  

My client would like to hire an extremely talented Senior Data Infrastructure 
Software Engineer as soon as possible. Their job
description can be found below. This group is going to be the next big thing in 
terms of quantitative strategies and systems.  They
are rebuilding their current system and also designing a new system.  You could 
work on either or both. I would love to speak with
you or anyone else you might highly recommend about this opportunity.

Thank you,

Charles Weitzer

CEO/Senior Recruiter
Charles Weitzer and Associates, Inc.
Global Financial Recruiting Services
Email: [email protected]
Phone: (USA) 510 558-9182

*
Senior Data Infrastructure Software Engineer

Fast-growing science and technology-driven startup seeks a Senior Data 
Infrastructure Software Engineer. You will be a core member
working with the RnD, Software, Infrastructure, and Trading teams to develop, 
test, deploy, and manage research, operational and
production software. 

The firm researches and deploys machine learning and statistical trading 
strategies that are designed to generate attractive returns
without being dependent on the performance of the overall market.  Join a team 
of people that includes a professor at a premier
university as well as over ten PhDs and two MBAs from top schools, led by the 
founder and CEO of a successful Internet
Infrastructure technology firm. We have a casual and collegial office 
environment, weekly catered lunches, and offer competitive
benefits packages. 

Focus areas for the position include creating software infrastructure for our 
research department and production trading systems,
implementing and automating back office and reporting systems, as well as 
supporting the next generation of our compute and storage
hardware systems. We seek a candidate who can bring both development and 
operations skills to rework existing software
infrastructure and guide test/automation of new and ongoing deployments.

Qualifications:

.   Experience writing Python code in a *nix environment
.   Experience working with mission critical RDBMS, performance and fault 
tolerance
.   Industry experience as a software engineer
.   Automation deployment and virtualization (Ansible, KVM, Jenkins, etc.)
.   Experience with debugging and performance profiling, including the use 
of tools such as strace, valgrind, gdb, tcpdump, etc.

Useful Skills (Desirable, but not required):

.   Monitor and network management (Nagios, Logstash, Graphite, Cacti, etc.)
.   Knowledge of distributed systems, cluster computing, and fault tolerance
.   Experience making commits on open-source technology
.   Familiarity with cluster management and job queuing systems (Chronos, 
Luigi, Oracle grid engine, Mesos, etc.)
.   Experience in operations for highly available services

Please note that a background in finance is not required.

*
To apply for this position, please send your resume to 
[email protected].

Thank you,

Charles Weitzer

CEO/Senior Recruiter
Charles Weitzer and Associates, Inc.
Global Financial Recruiting Services
Email: [email protected]
Phone: (USA) 510 558-9182



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


Re: annoying doctest problem

2015-01-12 Thread gordianknot1981
--
> My recommendation is to use doctest to test the code samples that 
> naturally occur in your docstrings, but not to use it as a 
> general-purpose testing tool.  It has too many limitations and quirks, 
> and if you're going to write separate test methods anyway, why not just 
> write actual code?
> 
> More detail here: 
> http://nedbatchelder.com/blog/200811/things_i_dont_like_about_doctest.html

"Docstrings may be good ways to explain what code does, but explaining and 
testing are two different tasks"..That's really to the point, and make 
sense. anyway, thanks for your article I benefit a lot from it.
-- 
https://mail.python.org/mailman/listinfo/python-list


Python 3 regex?

2015-01-12 Thread Jason Bailey

Hi all,

I'm working on a Python _3_ project that will be used to parse ISC DHCPD 
configuration files for statistics and alarming purposes (IP address 
pools, etc). Anyway, I'm hung up on this one section and was hoping 
someone could provide me with some insight.


My script first reads the DHCPD configuration file into memory - 
variable "filebody". It then utilizes the re module to find the 
configuration details for the wanted "shared network".


The config file might look something like this:

##

shared-network My-Network-MOHE {
  subnet 192.168.0.0 netmask 255.255.248.0 {
option routers 192.168.0.1;
option tftp-server-name "192.168.90.12";
pool {
  deny dynamic bootp clients;
  range 192.168.0.20 192.168.7.254;
}
  }
}

shared-network My-Network-CDCO {
  subnet 192.168.8.0 netmask 255.255.248.0 {
option routers 10.101.8.1;
option tftp-server-name "192.168.90.12";
pool {
  deny dynamic bootp clients;
  range 192.168.8.20 192.168.15.254;
}
  }
}

shared-network My-Network-FECO {
  subnet 192.168.16.0 netmask 255.255.248.0 {
option routers 192.168.16.1;
option tftp-server-name "192.168.90.12";
pool {
  deny dynamic bootp clients;
  range 192.168.16.20 192.168.23.254;
}
  }
}

##

Suppose I'm trying to grab the shared network called "My-Network-FECO" 
from the above config file stored in the variable 'filebody'.


First I have my variable 'shared_network' which contains the string 
"My-Network-FECO".


I compile my regex:
m = re.compile(r"^(shared\-network (" + re.escape(shared_network) + r") 
\{((\n|.|\r\n)*?)(^\}))", re.MULTILINE|re.UNICODE)


I search for regex matches in my config file:
m.search(filebody)

Unfortunately, I get no matches. From output on the command line, I can 
see that Python is adding extra backslashes to my re.compile string. I 
have added the raw 'r' in front of the strings to prevent it, but to no 
avail.


Thoughts on this?

Thanks


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


Design and Build Software Engineer Opportunity

2015-01-12 Thread Charles Weitzer
My name is Charles Weitzer.  I do recruiting for machine learning teams 
worldwide. One of my clients is a startup quantitative hedge
fund located in Northern, California. The founders previous worked together at 
one of the most successful quantitative hedge funds
in the world in New York City. Now they are ready to do it again. The team 
would like to hire an extremely talented design and build
software engineer as soon as possible..  They are rebuilding their current 
trading strategy and system and also designing a new
strategy.  You could work on either or both.  The title and level of seniority 
are very flexible. Their team has made unpublished
discoveries in the field of machine learning and in other areas as well.  This 
is an opportunity to leverage these discoveries in a
real world environment.

Here is their description of the position:

**
Design and Build Software Engineer

Fast-growing quantitative trading firm seeks an exceptional software engineer. 
You will design and build new production trading
systems, machine learning infrastructure, data integration pipelines, and 
large-scale storage systems.

We seek a candidate with a proven track record of building correct, 
well-designed software, solving hard problems, and delivering
complex projects on time. You should preferably have experience designing and 
implementing fault-tolerant distributed systems.
Experience with building large-scale data infrastructure, stream processing 
systems, or latency-sensitive programs is a bonus. We
are getting big fast. Willingness to take initiative, and a gritty 
determination to productize, are essential.

Join a team that includes faculty at premier universities and PhD's from 
top-tier schools, led by the founder and CEO of a
successful Internet infrastructure startup. You will have a high impact, and 
you can expect frequent interaction with the
researchers, officers, and founders.

Compensation and benefits are highly competitive.

Qualifications:

* Experience developing with C/C++/Python/Go in a Linux environment with a 
focus on performance, concurrency, and correctness.
* Experience working in TCP/IP networking, multithreading and server 
development.
* Experience working with common Internet protocols (IP, TCP/UDP, SSL/TLS, 
HTTP, SNMP, etc.)
* Experience architecting and designing highly-available critical systems.
* Experience architecting and designing large-scale data management 
infrastructure.
* Experience working in large codebases and building modular, manageable code.

Useful Skills:

* Experience with debugging and performance profiling, including the use of 
tools such as strace, valgrind, gdb, tcpdump, etc.
* Experience with build and test automation tools.
* Experience working with well-defined change management processes.
* Has experience hunting down RDBMS performance problems, understands indexing 
options, can read an execution/explain plan, has some
experience with ORM and optimization at the code layer, etc.
* Experience with messaging queues (such as RabbitMQ and Redis), as well as 
distributed caching systems.
**
This group is currently managing a very healthy amount of capital. And their 
job description above is really just a starting point
in terms of possible responsibilities and seniority.  They can be very flexible 
for the right person.

If you are interested, let me know the best way to get in touch and we can 
discuss details.

Talk soon,

Charles Weitzer

CEO\Senior Recruiter
Charles Weitzer and Associates, Inc.
Global Financial Recruiting Services
[email protected]
Voice: USA (510) 558-9182





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


Python 3 regex woes (parsing ISC DHCPD config)

2015-01-12 Thread Jason Bailey

Hi all,

I'm working on a Python _3_ project that will be used to parse ISC DHCPD 
configuration files for statistics and alarming purposes (IP address 
pools, etc). Anyway, I'm hung up on this one section and was hoping 
someone could provide me with some insight.


My script first reads the DHCPD configuration file into memory - 
variable "filebody". It then utilizes the re module to find the 
configuration details for the wanted "shared network".


The config file might look something like this:

##

shared-network My-Network-MOHE {
  subnet 192.168.0.0 netmask 255.255.248.0 {
option routers 192.168.0.1;
option tftp-server-name "192.168.90.12";
pool {
  deny dynamic bootp clients;
  range 192.168.0.20 192.168.7.254;
}
  }
}

shared-network My-Network-CDCO {
  subnet 192.168.8.0 netmask 255.255.248.0 {
option routers 10.101.8.1;
option tftp-server-name "192.168.90.12";
pool {
  deny dynamic bootp clients;
  range 192.168.8.20 192.168.15.254;
}
  }
}

shared-network My-Network-FECO {
  subnet 192.168.16.0 netmask 255.255.248.0 {
option routers 192.168.16.1;
option tftp-server-name "192.168.90.12";
pool {
  deny dynamic bootp clients;
  range 192.168.16.20 192.168.23.254;
}
  }
}

##

Suppose I'm trying to grab the shared network called "My-Network-FECO" 
from the above config file stored in the variable 'filebody'.


First I have my variable 'shared_network' which contains the string 
"My-Network-FECO".


I compile my regex:
m = re.compile(r"^(shared\-network (" + re.escape(shared_network) + r") 
\{((\n|.|\r\n)*?)(^\}))", re.MULTILINE|re.UNICODE)


I search for regex matches in my config file:
m.search(filebody)

Unfortunately, I get no matches. From output on the command line, I can 
see that Python is adding extra backslashes to my re.compile string. I 
have added the raw 'r' in front of the strings to prevent it, but to no 
avail.


Thoughts on this?

Thanks


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


Re: I must be missing something obvious in installing Python 3.4.2...

2015-01-12 Thread Chris Angelico
On Tue, Jan 13, 2015 at 5:02 AM, Andrew Koenig  wrote:
> Downloaded and installed 64-bit Python 3.4 and pywin32-219. Both installed 
> smoothly on my 64-bit Win7 machine. I added C:\Python34 to the search path.
>
> If I launch a Windows command window and run
>
> python -m ensurepip

Do you have any other Pythons in PATH? Try:

python --version

Is it what you expect? If it is, see if there's a PYTHONPATH or
somesuch from a different version of Python.

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


Re: Python 3 regex?

2015-01-12 Thread Chris Angelico
On Tue, Jan 13, 2015 at 5:03 AM, Jason Bailey  wrote:
> Unfortunately, I get no matches. From output on the command line, I can see
> that Python is adding extra backslashes to my re.compile string. I have
> added the raw 'r' in front of the strings to prevent it, but to no avail.
>

Regexes are notoriously hard to debug. Is there any particular reason
you _have_ to use one here? ISTM you could simplify it enormously by
just looking for the opening string:

shared_network = "My-Network-FECO"
network = filebody.split("\nshared-network "+shared_network+"
{",1)[1].split("\n}\n")[0]

Assuming your file is always correctly indented, and assuming you
don't have any other instances of that header string, you should be
fine.

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


Re: Python 3 regex?

2015-01-12 Thread Ian

On 12/01/2015 18:03, Jason Bailey wrote:

Hi all,

I'm working on a Python _3_ project that will be used to parse ISC 
DHCPD configuration files for statistics and alarming purposes (IP 
address pools, etc). Anyway, I'm hung up on this one section and was 
hoping someone could provide me with some insight.


My script first reads the DHCPD configuration file into memory - 
variable "filebody". It then utilizes the re module to find the 
configuration details for the wanted "shared network".



Hi Jason,

If you actually look at the syntax of what you are parsing, it is very 
simple.


My recommendation would be to write a recursive decent parser for your 
files.


That way will be easier to write, much easier to modify and almost 
certainly faster that a RE solution - and it can easily give you all the 
information in the file thus future proofing it.


'Some people, when confronted with a problem, think "I know, I'll use 
regular expressions." Now they have two problems.' -  Jamie Zawinski.


Regards

Ian


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


Re: Python 3 regex woes (parsing ISC DHCPD config)

2015-01-12 Thread Dave Angel

On 01/12/2015 01:20 PM, Jason Bailey wrote:

Hi all,



What changed between 1:03 and 1:20 that made you post a nearly identical 
second message, as a new thread?




Unfortunately, I get no matches. From output on the command line, I can
see that Python is adding extra backslashes to my re.compile string. I
have added the raw 'r' in front of the strings to prevent it, but to no
avail.



What makes you think that?  Please isolate this part of your problem 
with a simple short program, so we can diagnose it.  You're probably 
getting confused between str() and repr().  The latter adds backslash 
escape sequences for good reason, and if you don't understand it, you 
might think the strings are getting corrupted.



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


Namespace puzzle, list comprehension fails within class definition

2015-01-12 Thread John Ladasky
I've never come across this before.  Here's a minimal example (in Python 3.4):


Code:
-

d = {0:"a", 1:"b", 2:"c", 3:"d"}
e = [d[x] for x in (0,2)]

class Foo:
f = {0:"a", 1:"b", 2:"c", 3:"d"}
print(f)
g = [f[x] for x in (0,2)]

foo = Foo()


Output:
-

{0: 'a', 1: 'b', 2: 'c', 3: 'd'}

Traceback (most recent call last):
  File "minimal example.py", line 6, in 
class Foo:
  File "minimal example.py", line 9, in Foo
g = [f[x] for x in (0,2)]
  File "minimal example.py", line 9, in 
g = [f[x] for x in (0,2)]
NameError: name 'f' is not defined

-

When I am working in the top-level namespace, I get no errors when referencing 
the dictionary, d, inside the list comprehension which generates e.

When I am working inside the class namespace, the print function call on line 8 
recognizes the name f and prints the dictionary bound to that name.

However, the LIST COMPREHENSION defined inside the class namespace generates a 
NameError.

In all my years of Python programming, I guess that I have never tried to 
define a class attribute using a list comprehension.  Why doesn't it work?  Any 
advice, and suggestions for Pythonic workarounds, are appreciated.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python 3 regex?

2015-01-12 Thread Chris Angelico
On Tue, Jan 13, 2015 at 6:48 AM, Ian  wrote:
> My recommendation would be to write a recursive decent parser for your
> files.
>
> That way will be easier to write, much easier to modify and almost certainly
> faster that a RE solution - and it can easily give you all the information
> in the file thus future proofing it.

Generally, even a recursive descent parser will be overkill. It's
pretty easy to do simple string manipulation to get the info you want;
maybe that means restricting the syntax some, but for a personal-use
script, that's usually no big cost. The example I gave requires that
the indentation be correct, and on this mailing list, I think people
agree that that's not a deal-breaker :)

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


Re: Namespace puzzle, list comprehension fails within class definition

2015-01-12 Thread Ethan Furman
On 01/12/2015 12:25 PM, John Ladasky wrote:
> d = {0:"a", 1:"b", 2:"c", 3:"d"}
> e = [d[x] for x in (0,2)]
> 
> class Foo:
> f = {0:"a", 1:"b", 2:"c", 3:"d"}
> print(f)
> g = [f[x] for x in (0,2)]

In Foo 'f' is part of an unnamed namespace; the list comp 'g' has its own 
namespace, effectively making be a nonlocal;
class name lookup skips nonlocal namespaces.

Workaround:  use an actual for loop.

--
~Ethan~



signature.asc
Description: OpenPGP digital signature
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Namespace puzzle, list comprehension fails within class definition

2015-01-12 Thread John Ladasky
Following up to myself: I finally did the right keyword search, and found a 
relevant article:

http://stackoverflow.com/questions/13905741/accessing-class-variables-from-a-list-comprehension-in-the-class-definition

Maybe I HAVE tried to define a list comprehension inside a class definition 
before.  What I tried to do would have apparently worked in Python 2.  But in 
Python 3, the namespace behavior has changed (and I'm still reading the article 
to understand how and why).

I guess that I could define my objects as globals... now, why would Python 
force me into doing that? :^(
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Namespace puzzle, list comprehension fails within class definition

2015-01-12 Thread John Ladasky
On Monday, January 12, 2015 at 12:41:30 PM UTC-8, Ethan Furman wrote:

> In Foo 'f' is part of an unnamed namespace; the list comp 'g' has its own 
> namespace, effectively making be a nonlocal;
> class name lookup skips nonlocal namespaces.
> 
> Workaround:  use an actual for loop.

Thanks, Ethan.  That works.

As you can see from my other post, I've just discovered that the scoping rules 
for list comprehensions were changed between Py2 and Py3.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Namespace puzzle, list comprehension fails within class definition

2015-01-12 Thread Chris Angelico
On Tue, Jan 13, 2015 at 7:25 AM, John Ladasky
 wrote:
> When I am working inside the class namespace, the print function call on line 
> 8 recognizes the name f and prints the dictionary bound to that name.
>
> However, the LIST COMPREHENSION defined inside the class namespace generates 
> a NameError.

A list comp is defined with a function call:

>>> def f():
...  return [x*x for x in range(4)]
...
>>> dis.dis(f)
  2   0 LOAD_CONST   1 ( at
0x7fdf25981420, file "", line 2>)
  3 LOAD_CONST   2 ('f..')
  6 MAKE_FUNCTION0
  9 LOAD_GLOBAL  0 (range)
 12 LOAD_CONST   3 (4)
 15 CALL_FUNCTION1 (1 positional, 0 keyword pair)
 18 GET_ITER
 19 CALL_FUNCTION1 (1 positional, 0 keyword pair)
 22 RETURN_VALUE

This prevents leakage of the iterator into the enclosing scope.
Personally, I think it's a hack to get around the fact that Python
doesn't have any concept of sub-function-scope (similar to the weird
hack in try/except); if it weren't for that, true nesting would be
easier. As it is, function definitions in class scope have a special
meaning, and that interferes a bit with list comps.

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


Re: I must be missing something obvious in installing Python 3.4.2...

2015-01-12 Thread Tim Golden



On 12/01/2015 18:02, Andrew Koenig wrote:

Downloaded and installed 64-bit Python 3.4 and pywin32-219. Both installed 
smoothly on my 64-bit Win7 machine. I added C:\Python34 to the search path.

If I launch a Windows command window and run

 python -m ensurepip

I get the following:

Ignoring indexes: https://pypi.python.org/simple/
Downloading/unpacking setuptools
Cleaning up...
Exception:
Traceback (most recent call last):
   File 
"C:\Users\ark\AppData\Local\Temp\tmpe3ckts9f\pip-1.5.6-py2.py3-none-any.w
hl\pip\basecommand.py", line 122, in main
 status = self.run(options, args)
   File 
"C:\Users\ark\AppData\Local\Temp\tmpe3ckts9f\pip-1.5.6-py2.py3-none-any.w
hl\pip\commands\install.py", line 278, in run
 requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, 
bundl
e=self.bundle)
   File 
"C:\Users\ark\AppData\Local\Temp\tmpe3ckts9f\pip-1.5.6-py2.py3-none-any.w
hl\pip\req.py", line 1177, in prepare_files
 url = finder.find_requirement(req_to_install, upgrade=self.upgrade)
   File 
"C:\Users\ark\AppData\Local\Temp\tmpe3ckts9f\pip-1.5.6-py2.py3-none-any.w
hl\pip\index.py", line 209, in find_requirement
 file_locations, url_locations = self._sort_locations(locations)
   File 
"C:\Users\ark\AppData\Local\Temp\tmpe3ckts9f\pip-1.5.6-py2.py3-none-any.w
hl\pip\index.py", line 128, in _sort_locations
 sort_path(os.path.join(path, item))
   File 
"C:\Users\ark\AppData\Local\Temp\tmpe3ckts9f\pip-1.5.6-py2.py3-none-any.w
hl\pip\index.py", line 109, in sort_path
 if mimetypes.guess_type(url, strict=False)[0] == 'text/html':
   File "C:\Python34\lib\mimetypes.py", line 287, in guess_type
 init()
   File "C:\Python34\lib\mimetypes.py", line 348, in init
 db.read_windows_registry()
   File "C:\Python34\lib\mimetypes.py", line 255, in read_windows_registry
 with _winreg.OpenKey(hkcr, subkeyname) as subkey:
TypeError: OpenKey() argument 2 must be str without null characters or None, not
  str


Ouch! That looks like another variation on a bug of which we've fixed at 
least two variations to do with reading mimetypes from the Windows 
registry. Can you see what the following code produces, please:



import winreg as _winreg

hkcr = _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT, "")

with open("classes.txt", "w") as f:
i = 0
while True:
try:
ctype = _winreg.EnumKey(hkcr, i)
except EnvironmentError:
break
else:
f.write("%s -> %r\n" % (type(ctype), ctype))

i += 1



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


Re: I must be missing something obvious in installing Python 3.4.2...

2015-01-12 Thread Andrew Koenig
python --version reports 3.4.2, which is what I expected. I see no PYTHONPATH 
variable, or any environment variable with a name beginning PY (either upper or 
lower case).
-- 
https://mail.python.org/mailman/listinfo/python-list


Broken IF statement

2015-01-12 Thread joboppsgpp
https://bpaste.net/show/93be9e15634b <--- Line 19 through 22

At all times, my program is assigning the object priority of 0, even if one 
already exists in the database with a priority of 0 (it's supposed to be 
assigning it a priority of 1 in those cases).

I'm a non developer trying to fix a freelancer's code. Would anybody be able to 
suggest changes to the IF logic that might be able to fix it, assuming the 
statements in the code provided look flawed?

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


Re: Broken IF statement

2015-01-12 Thread Chris Angelico
On Tue, Jan 13, 2015 at 8:19 AM,   wrote:
> https://bpaste.net/show/93be9e15634b <--- Line 19 through 22
>
> At all times, my program is assigning the object priority of 0, even if one 
> already exists in the database with a priority of 0 (it's supposed to be 
> assigning it a priority of 1 in those cases).
>
> I'm a non developer trying to fix a freelancer's code. Would anybody be able 
> to suggest changes to the IF logic that might be able to fix it, assuming the 
> statements in the code provided look flawed?

Normally, I would suggest talking to the freelancer who wrote the
code, unless you're no longer working with him/her. Changing someone's
code out from under them is a great way to annoy and confuse.

Including the text in-line as it's short enough for that:

def create_socialaccount(profile, urls, twitters, facebooks, statuses=None):
"""
Add or update all the social accounts linked to the 'profile'
"""
results = []
l = [(urls, SocialAccount.HOMEPAGE, HOMEPAGE_COL_START),
 (twitters, SocialAccount.TWITTER, TWITTER_COL_START),
 (facebooks, SocialAccount.FACEBOOK, FACEBOOK_COL_START)]
for objs, service, offset in l:
for i, value in enumerate(objs):
if value:
obj, created = SocialAccount.objects.get_or_create(
social_profile=profile,
service=service,
value=value,
)
# The first object added/updated gets a priority of 0, all
# others get a 1
if i == 0:
obj.priority = 0
else:
obj.priority = 1


What kind of object is this 'obj'? After you make a change to it, do
you need to tell it to write to a database or something?

What you could try is changing the priority assignments to, say, 2 and
3. That would tell you that it's making the change. But if the
intention is to have the first successful one at priority 0 and all
others at priority 1 (which is what the comment implies), then I'd
write it like this:

def create_socialaccount(profile, urls, twitters, facebooks, statuses=None):
"""
Add or update all the social accounts linked to the 'profile'
"""
l = [(urls, SocialAccount.HOMEPAGE, HOMEPAGE_COL_START),
 (twitters, SocialAccount.TWITTER, TWITTER_COL_START),
 (facebooks, SocialAccount.FACEBOOK, FACEBOOK_COL_START)]
for objs, service, offset in l:
prio = 0
for value in objs:
if value:
obj, created = SocialAccount.objects.get_or_create(
social_profile=profile,
service=service,
value=value,
)
# The first object added/updated gets a priority of 0, all
# others get a 1
obj.priority = prio
prio = 1

It's not clear whether "first" means "first of each type" or "first
overall". For instance, if someone has three twitters and two
facebooks, should one twitter and one facebook be given priority 0, or
should one twitter get prio 0 and everything else prio 1? I've coded
it for the former, but you could easily make it the latter by simply
shifting the "prio = 0" statement one line further up (and unindenting
it), thus putting it before the entire loop.

Does that help, at all?

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


Re: I must be missing something obvious in installing Python 3.4.2...

2015-01-12 Thread Andrew Koenig
It runs and creates a classes.txt file with 803 lines. The first few:

 -> '$cpfile12'
 -> '$crfile12'
 -> '$cxfile12'
 -> '*'
 -> '.$cp'
 -> '.$cr'
 -> '.$cx'
 -> '.386'
 -> '.3ds'

A few lines in the middle that might be relevant:

 -> '.py'
 -> '.pyc'
 -> '.pyo'
 -> '.pys'
 -> '.pyw'

And the last few:

 -> '{C7A40493-BF23-4B53-AB2A-4A923B3EE34B}\x00'
 -> '{E14E55A7-29C8-4389-8E5A-3EF964510FCA}\x00'
 -> '{F5E30566-7C8F-4037-A8FF-A7382E251C56}\x00'

Anything in particular I should be looking for?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Broken IF statement

2015-01-12 Thread joboppsgpp
Thanks Chris. This definitely helps. I will test it and see what happens. In 
terms of the previous code, what it was intended to do wasn't actually 
happening. 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: I must be missing something obvious in installing Python 3.4.2...

2015-01-12 Thread Tim Golden



On 12/01/2015 21:45, Andrew Koenig wrote:

It runs and creates a classes.txt file with 803 lines. The first few:

 -> '$cpfile12'
 -> '$crfile12'
 -> '$cxfile12'
 -> '*'
 -> '.$cp'
 -> '.$cr'
 -> '.$cx'
 -> '.386'
 -> '.3ds'

A few lines in the middle that might be relevant:

 -> '.py'
 -> '.pyc'
 -> '.pyo'
 -> '.pys'
 -> '.pyw'

And the last few:

 -> '{C7A40493-BF23-4B53-AB2A-4A923B3EE34B}\x00'
 -> '{E14E55A7-29C8-4389-8E5A-3EF964510FCA}\x00'
 -> '{F5E30566-7C8F-4037-A8FF-A7382E251C56}\x00'

Anything in particular I should be looking for?



I was actually expecting to see something like a byestring, but in fact 
those last few items look suspicious with their trailing NULs. The 
mimetypes code tries to open each key to find its associated mimetype(s) 
and would probably fail on those.


The PyArg_ParseTupleAndKeywords in OpenKey is using the "Z" pattern for 
its first parameter which disallows embedded NULs. Obviously we can 
trap-and-skip within the mimetypes code but that won't help you 
immediately (short of your patching your own lib/mimetypes).


Is it possible to temporarily rename those keys out of the way, or to 
strip their trailing NULs to see if the problem goes away?


TJG

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


Re: Python 3 regex woes (parsing ISC DHCPD config)

2015-01-12 Thread Albert-Jan Roskam


- Original Message -

> From: Jason Bailey 
> To: [email protected]
> Cc: 
> Sent: Monday, January 12, 2015 7:20 PM
> Subject: Python 3 regex woes (parsing ISC DHCPD config)
> 
> Hi all,
> 
> I'm working on a Python _3_ project that will be used to parse ISC DHCPD 
> configuration files for statistics and alarming purposes (IP address 
> pools, etc). Anyway, I'm hung up on this one section and was hoping 
> someone could provide me with some insight.
> 
> My script first reads the DHCPD configuration file into memory - 
> variable "filebody". It then utilizes the re module to find the 
> configuration details for the wanted "shared network".
> 
> The config file might look something like this:
> 
> ##
> 
> shared-network My-Network-MOHE {
>subnet 192.168.0.0 netmask 255.255.248.0 {
>  option routers 192.168.0.1;
>  option tftp-server-name "192.168.90.12";
>  pool {
>deny dynamic bootp clients;
>range 192.168.0.20 192.168.7.254;
>  }
>}
> }
> 
> shared-network My-Network-CDCO {
>subnet 192.168.8.0 netmask 255.255.248.0 {
>  option routers 10.101.8.1;
>  option tftp-server-name "192.168.90.12";
>  pool {
>deny dynamic bootp clients;
>range 192.168.8.20 192.168.15.254;
>  }
>}
> }
> 
> shared-network My-Network-FECO {
>subnet 192.168.16.0 netmask 255.255.248.0 {
>  option routers 192.168.16.1;
>  option tftp-server-name "192.168.90.12";
>  pool {
>deny dynamic bootp clients;
>range 192.168.16.20 192.168.23.254;
>  }
>}
> }
> 
> ##
> 
> Suppose I'm trying to grab the shared network called 
> "My-Network-FECO" 
> from the above config file stored in the variable 'filebody'.
> 
> First I have my variable 'shared_network' which contains the string 
> "My-Network-FECO".
> 
> I compile my regex:
> m = re.compile(r"^(shared\-network (" + re.escape(shared_network) 
> + r") 
> \{((\n|.|\r\n)*?)(^\}))", re.MULTILINE|re.UNICODE)
> 
> I search for regex matches in my config file:
> m.search(filebody)
> 
> Unfortunately, I get no matches. From output on the command line, I can 
> see that Python is adding extra backslashes to my re.compile string. I 
> have added the raw 'r' in front of the strings to prevent it, but to no 
> avail.
> 
> Thoughts on this?


Will the following work for you? My brain shuts down when I try to read your 
regex, but I believe you also used a non-greedy match.


Python 3.4.2 (default, Nov 20 2014, 13:01:11) 
[GCC 4.7.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> cfg = """shared-network My-Network-MOHE {
...   subnet 192.168.0.0 netmask 255.255.248.0 {
... option routers 192.168.0.1;
... option tftp-server-name "192.168.90.12";
... pool {
...   deny dynamic bootp clients;
...   range 192.168.0.20 192.168.7.254;
... }
...   }
... }
... 
... shared-network My-Network-CDCO {
...   subnet 192.168.8.0 netmask 255.255.248.0 {
... option routers 10.101.8.1;
... option tftp-server-name "192.168.90.12";
... pool {
...   deny dynamic bootp clients;
...   range 192.168.8.20 192.168.15.254;
... }
...   }
... }
... 
... shared-network My-Network-FECO {
...   subnet 192.168.16.0 netmask 255.255.248.0 {
... option routers 192.168.16.1;
... option tftp-server-name "192.168.90.12";
... pool {
...   deny dynamic bootp clients;
...   range 192.168.16.20 192.168.23.254;
... }
...   }
... }"""
>>> import re
>>> re.findall(r"shared\-network (.+) \{?", cfg)
['My-Network-MOHE', 'My-Network-CDCO', 'My-Network-FECO']
>>> 
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2015-01-12 Thread Andrew Robinson


On 01/07/2015 04:04 PM, Ethan Furman wrote:

On 01/06/2015 07:37 PM, Andrew Robinson wrote:


Explain; How does mere subclassing of bool break the contract that bool has?
eg: What method or data would the superclass have that my subclass would not?

bool's contract is that there are only two values (True and False) and only one 
instance each of those two values (True
and False).  If bool were subclassable, new values could be added with either 
completely different values (PartTrue) or
with more of the same value (True, ReallyTrue, AbsolutelyTrue) -- hence, broken 
contract.

--
~Ethan~



Huh? I'm not adding any values when I merely subclass bool ; and even if 
the subclass could be instantiated -- that's doesn't mean a new value or 
instance of the base class (bool) must exist.  For I could happily work 
with a new subclass that contains no new data, but only an already 
_existing instance_ of 'True' or 'False_'_ as its value source.   That 
means there is no new value...  but at most (and even that could be 
worked around) a new instance of a subclass containing an existing 
instance of it's base class.


Note:  Guido only mentioned that he didn't want multiple instances of 
the base class bool -- But that's not technically the same as objecting 
to a subclass having an instance which CONTAINS an original bool 
instance and not a new one.


There are other ways Guido could have modified the Python language to 
prevent creation of new values without preventing the creation of a 
subclass --  if that's what he was really afterSo -- no -- I 
disagree with you.


Subclassing is allowed in many other OOP languages (not just C++) when 
working with singletons, and dualtons, n-tons... and the very PURPOSE of 
those objects is to prevent multiple instances, or spreading of 
control.  BUT -- Most object oriented languages I know of -- allow as 
standard practice, subclassing of n-tons -- while (often) simultaneously 
controlling or even eliminating the number of instances a subclass may 
have / and or the values it may take.  eg: depending on how flexible the 
type/class definition of a language is -- languages handle sub-classing 
of singletons differently.


Besides, a contract for a class can only be assumed to be valid for code 
designed for that specific class -- not code made for a subclass;  The 
contract for bool simply says nothing about programs designed for any 
other classes that bool is found inside of -- either as a subclass or a 
subelement;  eg:  I can still put bool inside another object with 
different methods: eg: I just write: (False,) as proof -- so Guido 
couldn't possibly have been trying to limit the methods which can 
operate on bool or the number of links to bool.


So I don't understand why Guido cared to restrict subclassing of bool -- 
and what the contract you mention has to do with it --  eg: what was his 
actual goal ?  Was it memory conservation, or compatability of built-in 
return types -- or what ?  There is no written 'contract' saying exactly 
what Guido's design objectives were in detail and more importantly 
'WHY';  but Guido only said that subclassing instances allowed a 'back 
door' (not that subclassiing itself was bad, just that it allowed some 
side effect...) to whatever Guido really didn't want to happen.


Python generally allows subclassing of singleton instances; so that 
makes 'bool' is an arbitrary exception to the general rule which Guido 
decided  to make... and he did so without any very clear explanation as 
to why.  eg: he cited what C/C++ *must *do as part of his reasoning -- 
but C/C++ can apparently do something that Guido  thought it couldn't 
(Guido was flat wrong) and for that reason, I really wonder if 
Guido's decision was some kind of spur of the moment erroneous epiphany 
-- bolstered by the fact that he said "I realized last night..." like 
something he had never thought of before, or thought through carefully


https://mail.python.org/pipermail/python-dev/2002-March/020822.html


And worse, Guido's problem even apparently extends to making 'duck 
types' which other writers in this thread have been proposing I do. eg: 
They, too, are going against canonical Guido's epiphany night...


Of course, you can define your own subclass of int similar to the bool
class I show in the PEP, and you can give it any semantics you want --
but that_would also defeat the purpose of having a standard bool_.

Not to mention, that Charles Bool's two initial values True and False 
are NOT the only ones used in computer engineering and science; for 
boolean logic only became useful at a time when engineers and 
mathematicians realized that at least a third type, AKA: 'Don't care' -- 
was necessary for doing correctness testing of logic in a tractable 
way.   So -- Guido arbitrarily chose to make bool based on a historical 
curiosity that never has been mainstream in logic analysis.  eg: He 
based it on a beta quality revision of boolean theory 

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

2015-01-12 Thread Chris Angelico
On Tue, Jan 13, 2015 at 9:27 AM, Andrew Robinson
 wrote:
> Huh? I'm not adding any values when I merely subclass bool ; and even if the
> subclass could be instantiated -- that's doesn't mean a new value or
> instance of the base class (bool) must exist.  For I could happily work with
> a new subclass that contains no new data, but only an already existing
> instance of 'True' or 'False' as its value source.   That means there is no
> new value...  but at most (and even that could be worked around) a new
> instance of a subclass containing an existing instance of it's base class.

If you subclass bool and instantiate your subclass, you have made a
new instance of bool, because every instance of a subclass is an
instance of its superclass. The Python bool type has the following
invariant, for any object x:

assert not isinstance(x, bool) or x is True or x is False

(You can fiddle with this in Py2 by rebinding the names True and
False, but you could replace those names with (1==1) and (1==0) if you
want to be completely safe. Likewise, the name "bool" could be
replaced with (1==1).__class__ to avoid any stupidities there. But
conceptually, that's the invariant.)

Subclassing bool breaks this invariant, unless you never instantiate
the subclass, in which case it's completely useless.

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


What does "pip install" do?

2015-01-12 Thread Fabien

Folks,

I've learned a lot today about python packaging and stuff, thanks to a 
couple of good websites. I've learned how to install a package from PyPi 
with pip, choose which version, upgrade it, uninstall it, use 
virtualenv, BUT I'm still asking myself: what does "pip install" 
*concretely* do on my virtual environment?


It seems to add a directory in the /site-packages directory, but what 
does it do when for example command-line scripts are shipped with it? Is 
something else happening in the background?


Neither does the "Python Packaging Authority" website 
(https://pip.pypa.io/en/latest/user_guide.html) nor the 
"python-packaging-user-guide" 
(http://python-packaging-user-guide.readthedocs.org/en/latest/installing.html) 
nor the "Installing python packages" from the python docs 
(https://docs.python.org/3/installing/) seem to provide an answer to 
this question.


Or did I simply miss it?

Thanks!

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


Re: I must be missing something obvious in installing Python 3.4.2...

2015-01-12 Thread Andrew Koenig
Not sure that would be a good idea: There are 22 such keys, as opposed to only 
two keys with Windows ID strings that don't end in nulls. I found this article:

http://www.swarley.me.uk/blog/2014/04/23/python-pip-and-windows-registry-corruption/

with the comment "If you are happy to completely remove any problematic keys, 
and accept any associated breakages that may occur, then this is the easiest 
solution."

I am concerned that if I change these keys, something else might break and I 
have no easy way of figuring out what.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What does "pip install" do?

2015-01-12 Thread Chris Angelico
On Tue, Jan 13, 2015 at 9:35 AM, Fabien  wrote:
>
> I've learned a lot today about python packaging and stuff, thanks to a
> couple of good websites. I've learned how to install a package from PyPi
> with pip, choose which version, upgrade it, uninstall it, use virtualenv,
> BUT I'm still asking myself: what does "pip install" *concretely* do on my
> virtual environment?

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.

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


Re: Reg: scrappring error

2015-01-12 Thread Emile van Sebille

On 1/11/2015 9:27 PM, Sushanth wrote:



urllib2.HTTPError: HTTP Error 302: The HTTP server returned a redirect
error that would lead to an infinite loop.


Looks like the server has a link on this page pointing back to itself or 
somesuch.


Does this help?

Emile


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


Re: I must be missing something obvious in installing Python 3.4.2...

2015-01-12 Thread Andrew Koenig
Fixed it!

The aforementioned article is correct. I downloaded the RegDelNull program 
mentioned in the article 
(http://technet.microsoft.com/en-us/sysinternals/bb897448.aspx) and ran it on 
hkcr, hkcu, hklm, hku, and hkcc (short for HKEY_CLASSES_ROOT, 
HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS, and HKEY_CURRENT_CONFIG), 
respectively. It deleted a bunch of keys. Rerunning the program you posted 
earlier revealed no keys with embedded nulls in their names, and ensurepip now 
works.

I have no idea how these keys got there. For all I know they are the result of 
malware.

I think it would be worthwhile changing the Python code to detect nulls and 
perhaps issue a warning that directs people to this article or something like 
it.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python 3 regex?

2015-01-12 Thread Rick Johnson
> 'Some people, when confronted with a problem, think "I
> know, I'll use regular expressions." Now they have two
> problems.' -  Jamie Zawinski.

This statement is one of my favorite examples of "powerful
propaganda", which has scared more folks away from regexps
than even the "Upright Citizens Brigade" could manage with
their "Journey through the center of gas giant #7" and it's
resulting "aggravated assault" on American coinage!

I wonder if Jamie's conclusions are a result of careful
study, or merely, an attempt to resolve his own cognitive
dissonance? Of course, if the latter is true, then i give
him bonus points for his use of the third person to veil his
own inadequacies -- nice Jamie, *very* nice!

"Rick it sounds like you're accusing Jamie of cowardice
resulting in "sour grapes"?"

Indeed! The problem with statements like his is that, the
ironic humor near the end *fortifies* the argument so much
that the reader forgets the limits of the harm (quantified
as: "some people") and immediately accepts the consequences
as effecting "all people who choose to use regexps", or more
generally, accepts the argument as a "universal unbounded
truth". Besides, who would want to be a member of a group
for which the individuals are too stupid to know good
choices from bad choices?

HA, PEER PRESSURE, IT'S A POWERFUL THING!

But there is more going on here than just mere "slight of
forked tongue" my friends, because, even the most
accomplished propagandist cannot fool *most* of the people.
No, this type of "powerful propaganda" only succeeds when
the subject matter is both cryptic *AND* esoteric.

For instance, in the following example, i contrive a
similarly ironic statement to showcase the effects of such
propaganda, but one that covers a subject matter in which
laymen either: already understand, or, can easily attain
"enough knowledge" to appreciate the humor.


#   Ironic Twist   #

# Some diabetics, when confronted with hunger, think "I#
# know, I'll eat a box of sugar cookies." -- now they have #
# two problems!'   #


"Wait a minute Rick! After eating the cookies the
diabetic would not longer be hungry, so how could he
have two problems? Your logic is flawed!

Au Contraire! Read the statement carefully, I said:
"When *CONFRONTED* with hunger" -- the two problems
(and the eventual side effect) exist at the *MOMENT* the
diabetic considers eating the cookies.

PROBLEM1: Need to eat!
PROBLEM2: Cookies raise glucose too quickly

In this example, even a layman would understand that the
statement is meant to showcase the irony of resolving a
problem (hunger) with a solution (eating a box of cookies)
that results in the undesirable outcome of (hyperglycemia).

And while this statement, and the one about regexps, both
contain a "factual underlying truth" (basically that
negative side effects should be avoided) the layman will
lack the esoteric knowledge of regexps to confirm the
factual basis for himself, and will instead blindly adopt
the propagandist assertion as truth, based solely on the
humorous prowess of the propagandist.

The most effective propaganda utilizes the sub-conscience.
You see, the role of propaganda is to "modify behavior", and
it is a more prevalent and powerful tool than most people
realize! The propagandist will seek to control you; he'll
use your ignorance against you; but you didn't notice
because he made you laugh!

WHO'S LAUGHING NOW? -- YOU MINDLESS ROBOTS!

"But what's so evil about that Rick? He scared away a
few feeble minded folks. SO WHAT!

I argue that we are all "feeble minded" in any subject we
have not yet mastered. His propaganda (be it intentional or
not) is so powerful that it defeats the neophyte before they
can even begin. Because it gives them the false impression
that regexps are only used by foolish people.

Yes, i'll admit, regexps are very cryptic, but once you
grasp their intricacies, you appreciate the succinctness of
there syntax, because, what makes them so powerful is not
only the extents of their pattern matching abilities, but
their conciseness.

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


Re: Python 3 regex?

2015-01-12 Thread Chris Angelico
On Tue, Jan 13, 2015 at 10:47 AM, Rick Johnson
 wrote:
> WHO'S LAUGHING NOW? -- YOU MINDLESS ROBOTS!

It's very satisfying when mindless robots laugh.

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


Re: Python 3 regex?

2015-01-12 Thread Mark Lawrence

On 12/01/2015 23:47, Rick Johnson wrote:

'Some people, when confronted with a problem, think "I
know, I'll use regular expressions." Now they have two
problems.' -  Jamie Zawinski.




[normal cobblers snipped]

If you wish to use a hydrogen bomb instead of a tooth pick feel free, I 
won't lose any sleep over it.  Meanwhile I'll get on with writing code, 
and for the simple jobs that can be completed with string methods I'll 
carry on using them.  When that gets too complicated I'll reach for the 
regex manual, knowing full well that there's enough data in books and 
online to help even a novice such as myself get over all the hurdles. 
If that isn't good enough then maybe a full blown parser, such as the 
pile listed here http://nedbatchelder.com/text/python-parsers.html ?


--
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: Comparisons and sorting of a numeric class....

2015-01-12 Thread Andrew Robinson


On 01/12/2015 02:35 PM, Chris Angelico wrote:

On Tue, Jan 13, 2015 at 9:27 AM, Andrew Robinson
 wrote:

Huh? I'm not adding any values when I merely subclass bool ; and even if the
subclass could be instantiated -- that's doesn't mean a new value or
instance of the base class (bool) must exist.  For I could happily work with
a new subclass that contains no new data, but only an already existing
instance of 'True' or 'False' as its value source.   That means there is no
new value...  but at most (and even that could be worked around) a new
instance of a subclass containing an existing instance of it's base class.

Hmmm
That may be true in python, as it is now, but that doesn't mean that 
Guido had to leave it that way when he decided to change the language to 
single out bool, and make it's subclassing rules abnormal in the first 
place.

He was changing the language when he made the decision after all !!

What I am wanting to know is WHY did Guido think it so important to do 
that ?   Why was he so focused on a strict inability to have any 
instances of a bool subclass at all -- that he made a very arbitrary 
exception to the general rule that base types in Python can be subclassed ?


There's no reason in object oriented programming principles in general 
that requires a new subclass instance to be a COMPLETELY DISTINCT 
instance from an already existing superclass instance nor, have I 
ever seen Guido say that Python is designed intentionally to force this 
to always be the case... so I'm not sure that's its anything more than a 
non guaranteed implementation detail that Python acts the way you say it 
does


I don't see, within Python, an intrinsic reason (other than lack of 
support/foresight in the historical evolution of Python to date), as to 
why a subclass couldn't be instanted with the data coming from an 
*already* existing instance of it's superclass.


There is no need to copy data from an initialized superclass instance 
into a subclass instance that has no new data, but only rebind -- or add 
a binding/proxy object -- to bind the superclass instance to the 
subclass methods.


eg: what is now standard practice to create a new copy of the superclass:

class myFalse( bool ): __new__( self, data ): return super( myFalse, 
self ).__new__(self,data)


Could be replaced by a general purpose proxy meant to handle singleton 
subclassing:


class myFalse( bool ):  __new__( self ): return 
bind_superinstance_to_subclass( False, myFalse )



he Python bool type has the following
invariant, for any object x:

assert not isinstance(x, bool) or x is True or x is False

Really !???
Where, in the language definition, did Guido explicitly guarantee this 
invariant ?



(You can fiddle with this in Py2 by rebinding the names True and
False, but you could replace those names with (1==1) and (1==0) if you
want to be completely safe. Likewise, the name "bool" could be
replaced with (1==1).__class__ to avoid any stupidities there. But
conceptually, that's the invariant.)
Interesting ... but rebinding True and False, won't extend the new 
capabilities to modules which are imported.  They will still, I think, 
be bound to the old True and False values.   I know, for example -- I 
can redefine the class bool altogether; although the type string becomes 
'main.bool' -- none the less, it does not exist in default scope when I 
switch namespaces; eg: in a module being imported 'bool' still means the 
old version of class bool.


I have to do something more drastic, like __builtins__.bool = class 
bool(int): ...  And then, even modules will recognize the changed class 
definition.


Hmmm.
>>> __builtins__.True='yes'
>>> True
'yes'

However, such actions -- I think -- are rather drastic; because they 
produce situations where another third party library in competition with 
mine might also have need of subclassing 'bool' and then we are in a 
fight for a static binding name with winner takes all ... rather than 
sharing dynamically compatible definitions based on subclasses.



Subclassing bool breaks this invariant, unless you never instantiate
the subclass, in which case it's completely useless.


Well we can't subclass bool now -- and the language would have to change 
in order for us to be able to subclass it; So -- I don't think your 
assert statement guarantees anything if case the language changes... On 
the other hand, I also don't see that your assert statement would ever 
return False even as it is written --- even if the object (x) was 
sub-classed or a totally different object than True or False  and so 
I *definitely* don't see why your assert statement would Fail if the 
language changed in one of several subtle ways I think it could.


I mean, even right now -- with the language as-is -- let's define 
something that blatantly creates a new instance of something neither an 
actual instance of True nor False, and make that x -- and see if your 
assertion catches it:


Python 2.7.5 (def

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

2015-01-12 Thread Chris Angelico
On Tue, Jan 13, 2015 at 12:59 PM, Andrew Robinson
 wrote:
> There is no need to copy data from an initialized superclass instance into a
> subclass instance that has no new data, but only rebind -- or add a
> binding/proxy object -- to bind the superclass instance to the subclass
> methods.
>
> eg: what is now standard practice to create a new copy of the superclass:
>
> class myFalse( bool ): __new__( self, data ): return super( myFalse, self
> ).__new__(self,data)
>
> Could be replaced by a general purpose proxy meant to handle singleton
> subclassing:
>
> class myFalse( bool ):  __new__( self ): return
> bind_superinstance_to_subclass( False, myFalse )

I don't understand. What do you expect this to be doing? Are you
trying to replace the original False with your new subclass?

>> he Python bool type has the following
>> invariant, for any object x:
>>
>> assert not isinstance(x, bool) or x is True or x is False
> I mean, even right now -- with the language as-is -- let's define something
> that blatantly creates a new instance of something neither an actual
> instance of True nor False, and make that x -- and see if your assertion
> catches it:
>
> Python 2.7.5 (default, May 29 2013, 02:28:51)
> [GCC 4.8.0] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
 x=(False,)
 assert not isinstance(x, bool) or x is True or x is False

>
> LOL ... no exception was raised... and we know if the assertion Failed, an
> exception ought to be raised:

The assertion did not fail. There are three parts, and as long as one
of them is true, the assertion will pass:

1) x isn't an instance of bool
2) x is the object known as True
3) x is the object known as False

You just gave an example of the first part of the invariant. That's an
instance of tuple, which is not a subclass of bool, ergo isinstance(x,
bool) returns False, negating that makes True, and the assertion
passes. [1]

> So -- your assertion, at least as shown, is pretty useless in helping
> determine why subclassing is not allowed, or instances of subclasses that
> are not distinct from their superclasses existing instance.

It exactly defines the nature of Python's bool type: there are
precisely two instances of it.

ChrisA

[1] Which puts me in mind of https://www.youtube.com/watch?v=D0yYwBzKAyY
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Track down SIGABRT

2015-01-12 Thread Jason Friedman
> I have a long-running python/CherryPy Web App server process that I am
> running on Mac OS X 10.8.5. Python 2.7.2 running in 32-bit mode (for now, I
> have the code in place to change over to 64 bit, but need to schedule the
> downtime to do it). On the 6th of this month, during normal operation from
> what I can tell, and after around 33 days of trouble-free uptime, the python
> process crashed with a SIGABRT. I restarted the process, and everything
> looked good again until yesterday, when it again crashed with a SIGABRT.

Can you monitor disk and memory on the host?  Perhaps it is climbing
towards an unacceptable value right before crashing.

Do you have the option of stopping and starting your process every
night or every week?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python 3 regex?

2015-01-12 Thread Rick Johnson
On Monday, January 12, 2015 at 7:55:32 PM UTC-6, Mark Lawrence wrote:
> On 12/01/2015 23:47, Rick Johnson wrote:
> >> 'Some people, when confronted with a problem, think "I
> >> know, I'll use regular expressions." Now they have two
> >> problems.' -  Jamie Zawinski.
> >
>
> [snip]
>
> If you wish to use a hydrogen bomb instead of a tooth pick
> feel free, I won't lose any sleep over it.  Meanwhile I'll
> get on with writing code, and for the simple jobs that can
> be completed with string methods I'll carry on using them.
> When that gets too complicated I'll reach for the regex
> manual, knowing full well that there's enough data in
> books and online to help even a novice such as myself get
> over all the hurdles. If that isn't good enough then maybe
> a full blown parser, such as the pile listed here [snip]

Mark, if you're going to quote me, then at least quote me in
a manner that does not confuse the content of my post. The
snippet you posted was not a statement of mine, rather, it
was a quote that i was responding to, and without any
context of my response, what is the point of quoting
anything at all? It would be better to quote nothing and
just say @Rick, then to quote something which does not have
any context.

Every python programmer worth his *SALT* should master the
following three text processing capabilities of Python, and
he should know how and when to apply them (for they all have
strengths and weaknesses):

(1) String methods: Simplistic API, but with limited
capabilities -- but never underestimate the
possibilities!

(2) Regexps: Great for pattern matching with a powerful
and concise syntax, but highly cryptic and unintuitive
for the neophyte (and sometimes even the guru! *wink*).

(3) Parsers: Great for extracting deeper meaning from text,
but if pattern matching is all you need, then why not
use (1) or (2) -- are you scared or uninformed?

"We can easily forgive a child who is afraid of the
dark; the real tragedy of life is when men are afraid of
the light. -- Plato

IMHO, if you seek to only match patterns, then string
methods should be your first choice, however, if the pattern
is too difficult for string methods, then graduate to
regexps. If you need to extract deeper meaning from
text, by all means utilize a parser.

But above all, don't fall for these "religious teachings"
about how regexps are too difficult for mortals -- that's
just hysteria. If you follow the outline i provided above,
you should find Python's "text processing Nirvana".

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


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

2015-01-12 Thread Andrew Robinson

Hmm

LOL ... no exception was raised... and we know if the assertion Failed, an
exception ought to be raised:

The assertion did not fail. There are three parts, and as long as one
of them is true, the assertion will pass:

1) x isn't an instance of bool
2) x is the object known as True
3) x is the object known as False

You just gave an example of the first part of the invariant. That's an
instance of tuple, which is not a subclass of bool, ergo isinstance(x,
bool) returns False, negating that makes True, and the assertion
passes. [1]


Uh ... yeah... and so an assertion meant to test if something is or is 
not a bool let a non-bool pass the assertion.  That seems rather ... 
silly, and useless, ... and so I really doubt the assertion -- based on 
it's behavior --can distinguish an actual bool from a subclassed one or 
a totally unrelated object ...


I mean, let's start by testing if x as an actual boolean will cause the 
assertion to act differently from a fake-non bool object which we 
already tried.


x=True
>>> x=True
>>> assert not isinstance(x, bool) or x is True or x is False
>>>
Wow. No difference in behavior.

So (as a test) it can't distinguish between an actual boolean and a 
faked one.
They both pass the assertion.  So -- What good is this assertion? It 
tells us nothing useful when executed.


Also what if we put in a subclass

Instead of pretending what if -- let's actually REPLACE python's built 
in bool class with an emulation that ALLOWS subclassing and THEN let's 
TEST my hypothesis that the assert statement you gave me can't tell the 
difference between bools any anthing else by it's actions...  ( h 
... another back-door that Guido forgot about... or perhaps purposely 
left open...)


class bool( int ):
def __new__(self,data): return super( bool, self).__new__(self, 
[0,1][data!=0] )

def __repr__(self): return [ 'True', 'False' ][self>0]

__builtins__.bool=bool
__builtins__.True=bool( 1==1 )
__builtins__.False=bool( 1==0 )

And, running it in the python interpreter...

>>> class bool( int ):
... def __new__(self,data): return super( bool, self).__new__(self, 
[0,1][data!=0] )

... def __repr__(self): return [ 'True', 'False' ][self>0]
...
>>> __builtins__.bool=bool
>>> __builtins__.True=bool( 1==1 )
>>> __builtins__.False=bool( 1==0 )
>>>
>>> type(True)


I now have proof that the replacement succeeded.
So let's subclass bool !!!

>>> class subBool(bool): pass
...
>>>

and now, let's see if your assertion believes a subclass of bool is a 
bool...

>>> x=subBool(0)
>>> assert not isinstance(x, bool) or x is True or x is False
>>>

Wow. No change.
So -- it doesn't fail when the object ISN'T a bool, it doesn't fail when 
the object IS a bool, and it doesn't fail when the object is a subclass 
of bool; and #1 matches that of a True bool! or if #1 doesn't match that 
of a true bool.


>>> isinstance( x, bool ) ,
True
>>> isinstance( True, bool )
True

Therefore, your explantion ( so far ) of how to interpret the invariant, 
is consistent with it explicitly and *precisely* 'passing'  all possible 
instances of subclasses of bool, all instances of non-bools, and all 
instances of bools.  Yes, the assertion you chose passes precisely 
ANYTHING! (facetous use of precise.).


It's a worthless assertion in the sense that it has no explicit logic to 
DISTINGUISH what Guido/Python does want a bool to be from what you have 
said and implied Guido doesn't want (even though I've never seen Guido 
agree with you on this assertion thing...) .

So -- your assertion, at least as shown, is pretty useless in helping
determine why subclassing is not allowed, or instances of subclasses that
are not distinct from their superclasses existing instance.

It exactly defines the nature of Python's bool type: there are
precisely two instances of it.

ChrisA

[1] Which puts me in mind of https://www.youtube.com/watch?v=D0yYwBzKAyY


Uh, no, -- your assertion excludes nothing you've been telling me is not 
a bool by contract -- so it doesn't 'define' anything precisely because 
it's by definition inaccurate.  it's simply an invariant form of spam 
with three terms you can interpret any way you like.


Where did you get that assertion from anyway and how is it related to 
Guido and formal definitions of the python language ???  Are you really 
trying to imply that Guido wrote that assertion ?



On Tue, Jan 13, 2015 at 12:59 PM, Andrew Robinson
  wrote:

There is no need to copy data from an initialized superclass instance into a
subclass instance that has no new data, but only rebind -- or add a
binding/proxy object -- to bind the superclass instance to the subclass
methods.

eg: what is now standard practice to create a new copy of the superclass:

class myFalse( bool ): __new__( self, data ): return super( myFalse, self
).__new__(self,data)

Could be replaced by a general purpose proxy meant to handle singleton
subclassing:

class myFalse( bool ):  __new__( self ): return
bind_superins

Re: Python 3 regex?

2015-01-12 Thread Steven D'Aprano
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
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Namespace puzzle, list comprehension fails within class definition

2015-01-12 Thread Steven D'Aprano
On Mon, 12 Jan 2015 12:40:13 -0800, Ethan Furman wrote:

> On 01/12/2015 12:25 PM, John Ladasky wrote:
>> d = {0:"a", 1:"b", 2:"c", 3:"d"}
>> e = [d[x] for x in (0,2)]
>> 
>> class Foo:
>> f = {0:"a", 1:"b", 2:"c", 3:"d"}
>> print(f)
>> g = [f[x] for x in (0,2)]
> 
> In Foo 'f' is part of an unnamed namespace; the list comp 'g' has its
> own namespace, effectively making be a nonlocal; class name lookup skips
> nonlocal namespaces.


Actually, no it doesn't.

py> def factory():
... x = 23
... class Inner(object):
... print('x is', x)
... return Inner
... 
py> o = factory()
x is 23


The "problem" is that *functions* lookup don't include the class body in 
their scope. This is by design, and goes back to Python 1.5 or older:

[steve@ando ~]$ python1.5
Python 1.5.2 (#1, Aug 27 2012, 09:09:18)  [GCC 4.1.2 20080704 (Red Hat 
4.1.2-52)] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> class Outer:
... x = 23
... f = lambda: x+1
... y = f()
... 
Traceback (innermost last):
  File "", line 1, in ?
  File "", line 4, in Outer
  File "", line 3, in 
NameError: x



> Workaround:  use an actual for loop.


Sad but true.



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


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

2015-01-12 Thread Steven D'Aprano
On Tue, 13 Jan 2015 09:35:43 +1100, Chris Angelico wrote:

> Subclassing bool breaks this invariant, unless you never instantiate the
> subclass, in which case it's completely useless.

Not necessarily. A class that you never instantiate, but use as an object 
itself, is another way of implementing the singleton design pattern. 
Obviously all the methods have to be class methods, but it is doable.

But wacky exceptions like subclasses that aren't instantiated aside, 
normally if you subclass something you intend to instantiate it.

In Java terms bool is a "final" class which means it cannot be subclassed. 
Although Python is not anywhere near as restrictive as Java, and makes a 
virtue out of allowing the programmer to shot themselves in the foot, 
there are some restrictions and this is one of them.


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


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

2015-01-12 Thread Chris Angelico
On Tue, Jan 13, 2015 at 3:57 PM, Steven D'Aprano  wrote:
> On Tue, 13 Jan 2015 09:35:43 +1100, Chris Angelico wrote:
>
>> Subclassing bool breaks this invariant, unless you never instantiate the
>> subclass, in which case it's completely useless.
>
> Not necessarily. A class that you never instantiate, but use as an object
> itself, is another way of implementing the singleton design pattern.
> Obviously all the methods have to be class methods, but it is doable.
>
> But wacky exceptions like subclasses that aren't instantiated aside,
> normally if you subclass something you intend to instantiate it.

True, I should have said "probably useless" instead of "completely
useless". :) There's always *something* you could do with it.

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


Re: class-based class decorator

2015-01-12 Thread Ian Kelly
On Jan 12, 2015 6:47 AM, "Albert-Jan Roskam"  wrote:
> Thanks for your replies. I changed it into a regular decorator (not a class 
> decorator). It would have been even nicer if I only needed to specify it once 
> per class, but, well, in my case this hardly matters. The code below works as 
> intended. One problem (not specific to the code): the decorator destroys the 
> signature: it always becomes "*args, **kwargs"). This is annoying with 
> help(), but what worries me most is that my Sphinx documentation is also 
> affected. The information about the defaults gets lost (the parameters are 
> decribed in the docstring, but not the defaults). That kind of sucks. Is 
> there a builtin way around this (in other words: I am aware of this package: 
> https://pypi.python.org/pypi/decorator). I am hoping to get the code working 
> on Python 2.7 and 3.3 and up.

As of (I think) 3.4 pydoc and the help() function will show you the
signature of the wrapped function as long as you use functools.wraps.
I don't know what the status is for Sphinx; all I can offer is this
stackoverflow link.

http://stackoverflow.com/questions/3687046/python-sphinx-autodoc-and-decorated-members
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python 3 regex?

2015-01-12 Thread Mark Lawrence

On 13/01/2015 02:53, Rick Johnson wrote:

On Monday, January 12, 2015 at 7:55:32 PM UTC-6, Mark Lawrence wrote:

On 12/01/2015 23:47, Rick Johnson wrote:

'Some people, when confronted with a problem, think "I
know, I'll use regular expressions." Now they have two
problems.' -  Jamie Zawinski.




[snip]

If you wish to use a hydrogen bomb instead of a tooth pick
feel free, I won't lose any sleep over it.  Meanwhile I'll
get on with writing code, and for the simple jobs that can
be completed with string methods I'll carry on using them.
When that gets too complicated I'll reach for the regex
manual, knowing full well that there's enough data in
books and online to help even a novice such as myself get
over all the hurdles. If that isn't good enough then maybe
a full blown parser, such as the pile listed here [snip]


Mark, if you're going to quote me, then at least quote me in
a manner that does not confuse the content of my post. The
snippet you posted was not a statement of mine, rather, it
was a quote that i was responding to, and without any
context of my response, what is the point of quoting
anything at all? It would be better to quote nothing and
just say @Rick, then to quote something which does not have
any context.


You snipped the bit that says [normal cobblers snipped].



Every python programmer worth his *SALT* should master the
following three text processing capabilities of Python, and
he should know how and when to apply them (for they all have
strengths and weaknesses):

 (1) String methods: Simplistic API, but with limited
 capabilities -- but never underestimate the
 possibilities!

 (2) Regexps: Great for pattern matching with a powerful
 and concise syntax, but highly cryptic and unintuitive
 for the neophyte (and sometimes even the guru! *wink*).

 (3) Parsers: Great for extracting deeper meaning from text,
 but if pattern matching is all you need, then why not
 use (1) or (2) -- are you scared or uninformed?



String methods, regexes, parsers, isn't that what I've already said 
above?  Why repeat it?



 "We can easily forgive a child who is afraid of the
 dark; the real tragedy of life is when men are afraid of
 the light. -- Plato

IMHO, if you seek to only match patterns, then string
methods should be your first choice, however, if the pattern
is too difficult for string methods, then graduate to
regexps. If you need to extract deeper meaning from
text, by all means utilize a parser.



I feel humbled that a great such as yourself is again repeating what 
I've already said.



But above all, don't fall for these "religious teachings"
about how regexps are too difficult for mortals -- that's
just hysteria. If you follow the outline i provided above,
you should find Python's "text processing Nirvana".



My favourite things in programming all go along the lines of DRY and 
KISS, with "Although practicality beats purity" being the most important 
of the lot.  So called "religious teachings" never enter into my way of 
doing things.  For example I can't stand code which jumps through hoops 
to avoid using GOTO, whereas nothing is cleaner than (say) GOTO ERROR. 
You'll (plural) find loads of them in cPython.


--
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: Comparisons and sorting of a numeric class....

2015-01-12 Thread Steven D'Aprano
On Mon, 12 Jan 2015 17:59:42 -0800, Andrew Robinson wrote:

[...]
> What I am wanting to know is WHY did Guido think it so important to do
> that ?   Why was he so focused on a strict inability to have any
> instances of a bool subclass at all -- that he made a very arbitrary
> exception to the general rule that base types in Python can be
> subclassed ?

It's not arbitrary. All the singleton (doubleton in the case of bool) 
classes cannot be subclassed. E.g. NoneType:

py> class X(type(None)):
... pass
... 
Traceback (most recent call last):
  File "", line 1, in 
TypeError: Error when calling the metaclass bases
type 'NoneType' is not an acceptable base type


Likewise for the NotImplemented and Ellipsis types.

The reason is the same: if a type promises that there is one and only one 
instance (two in the case of bool), then allowing subtypes will break 
that promise in the 99.99% of cases where the subtype is instantiated.

I suppose in principle Python could allow you to subclass singleton 
classes to your hearts content, and only raise an error if you try to 
instantiate them, but that would probably be harder and more error-prone 
to implement, and would *definitely* be harder to explain.


There may be others too:

py> from types import FunctionType
py> class F(FunctionType):
... pass
... 
Traceback (most recent call last):
  File "", line 1, in 
TypeError: Error when calling the metaclass bases
type 'function' is not an acceptable base type


My guess here is that functions are so tightly coupled to the Python 
interpreter that allowing you to subclass them, and hence break required 
invariants, could crash the interpreter. Crashing the interpreter from 
pure Python code is *absolutely not allowed*, so anything which would 
allow that is forbidden.


> There's no reason in object oriented programming principles in general
> that requires a new subclass instance to be a COMPLETELY DISTINCT
> instance from an already existing superclass instance

True. But what is the point of such a subclass? I don't think you have 
really thought this through in detail.

Suppose we allowed bool subclasses, and we implement one which *only* 
returns True and False, without adding a third instance:

class MyBool(bool):
def __new__(cls, arg):
if cls.condition(arg):
return True
else:
return False
@classmethod
def condition(cls, obj):
# decide whether obj is true-ish or false-ish.
pass
def spam(self):
return self.eggs()
def eggs(self):
return 23


And then you do this:

flag = MyBool(something)
flag.spam()


What do you expect to happen?


Since flag can *only* be a regular bool, True or False, it won't have 
spam or eggs methods.

You might think of writing the code using unbound methods:

MyBool.spam(flag)

(assuming that methods don't enforce the type restriction that "self" 
must be an instance of their class), but that fails when the spam method 
calls "self.eggs". So you have to write your methods like this:

def spam(self):
return MyBool.eggs(self)

hard-coding the class name! You can't use type(self), because that's 
regular bool, not MyBool.

This is a horrible, error-prone, confusing mess of a system. If you're 
going to write code like this, you are better off making MyBool a module 
with functions instead of a class with methods.


> nor, have I
> ever seen Guido say that Python is designed intentionally to force this
> to always be the case... so I'm not sure that's its anything more than a
> non guaranteed implementation detail that Python acts the way you say it
> does

It is a documented restriction on bool. Whether you agree with the 
decision or not, it is not an implementation detail, it is a language 
promise.

https://docs.python.org/2/library/functions.html#bool




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


Re: Namespace puzzle, list comprehension fails within class definition

2015-01-12 Thread Ethan Furman
On 01/12/2015 08:49 PM, Steven D'Aprano wrote:
> On Mon, 12 Jan 2015 12:40:13 -0800, Ethan Furman wrote:
>> 
>> [...] class name lookup skips nonlocal namespaces.
> 
> Actually, no it doesn't.
> [...] 
> The "problem" is that *functions* lookup don't include the class body in 
> their scope.

Ah, thanks for the correction!

--
~Ethan~



signature.asc
Description: OpenPGP digital signature
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: I must be missing something obvious in installing Python 3.4.2...

2015-01-12 Thread Tim Golden

On 12/01/2015 23:12, Andrew Koenig wrote:

Fixed it!

The aforementioned article is correct. I downloaded the RegDelNull
program mentioned in the article
(http://technet.microsoft.com/en-us/sysinternals/bb897448.aspx) and
ran it on hkcr, hkcu, hklm, hku, and hkcc (short for
HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS,
and HKEY_CURRENT_CONFIG), respectively. It deleted a bunch of keys.
Rerunning the program you posted earlier revealed no keys with
embedded nulls in their names, and ensurepip now works.

I have no idea how these keys got there. For all I know they are the
result of malware.

I think it would be worthwhile changing the Python code to detect
nulls and perhaps issue a warning that directs people to this article
or something like it.



Thanks, yes. Unfortunate that no-one's actually reported this before as 
a bug -- as far as I'm aware. Would you be in a position to report this 
at bugs.python.org? If not, I'll do it when I get a chance.


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


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

2015-01-12 Thread Chris Angelico
On Tue, Jan 13, 2015 at 4:32 PM, Steven D'Aprano  wrote:
> Crashing the interpreter from
> pure Python code is *absolutely not allowed*, so anything which would
> allow that is forbidden.

Except when you willingly shoot yourself in the foot.

rosuav@sikorsky:~$ python
Python 2.7.3 (default, Mar 13 2014, 11:03:55)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> def f(): sys.setrecursionlimit(sys.getrecursionlimit()+1) or f()
...
>>> f()
Segmentation fault
rosuav@sikorsky:~$ python3
Python 3.5.0a0 (default:1c51f1650c42+, Dec 29 2014, 02:29:06)
[GCC 4.7.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> def f(): sys.setrecursionlimit(sys.getrecursionlimit()+1) or f()
...
>>> f()
Segmentation fault

But otherwise, yes. You shouldn't be able to segfault Python with Python code.

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


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

2015-01-12 Thread Ian Kelly
On Mon, Jan 12, 2015 at 9:16 PM, Andrew Robinson
 wrote:
> Instead of pretending what if -- let's actually REPLACE python's built in
> bool class with an emulation that ALLOWS subclassing and THEN let's TEST my
> hypothesis that the assert statement you gave me can't tell the difference
> between bools any anthing else by it's actions...  ( h ... another
> back-door that Guido forgot about... or perhaps purposely left open...)
>
> class bool( int ):
> def __new__(self,data): return super( bool, self).__new__(self,
> [0,1][data!=0] )
> def __repr__(self): return [ 'True', 'False' ][self>0]
>
> __builtins__.bool=bool
> __builtins__.True=bool( 1==1 )
> __builtins__.False=bool( 1==0 )

Your posts are so wordy that I'm not even going to try to respond to
all of them, but this is an interesting point that bears explanation.
The reason you can reassign True and False in Python 2.x is for
backward compatibility with scripts that predated the built-in bool
type and would do things like:

True = 1
False = 0

However, this longstanding wart was fixed in Python 3 by making True
and False keywords:

>>> True = 42
  File "", line 1
SyntaxError: can't assign to keyword
>>> __builtins__.True = 42
  File "", line 1
__builtins__.True = 42
^
SyntaxError: invalid syntax
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python 3 regex?

2015-01-12 Thread Steven D'Aprano
On Mon, 12 Jan 2015 15:47:08 -0800, Rick Johnson wrote:

>> 'Some people, when confronted with a problem, think "I know, I'll use
>> regular expressions." Now they have two problems.' -  Jamie Zawinski.
> 
> I wonder if Jamie's conclusions are a result of careful study, or
> merely, an attempt to resolve his own cognitive dissonance? 

Zawinski is one of the pantheon of geek demi-gods, with Linus, Larry, 
Guido, RMS, and a few others. (Just don't ask me to rank them. I'm not 
qualified.) His comment isn't based on a failure to grok regular 
expressions, but on an understanding that many people use regular 
expressions inappropriately.

Here is more on the context of the famous quote:

http://regex.info/blog/2006-09-15/247


(By the way, the quote actually wasn't original to JZ, he stole it from 
an all but identical quote about awk.)


[...]
> For instance, in the following example, i contrive a similarly ironic
> statement to showcase the effects of such propaganda, but one that
> covers a subject matter in which laymen either: already understand, or,
> can easily attain "enough knowledge" to appreciate the humor.
> 
>  #  
> Ironic Twist   #
>  # Some
> diabetics, when confronted with hunger, think "I# # know, I'll
> eat a box of sugar cookies." -- now they have # # two problems!'

Not the best of analogies, since there are two forms of diabetes. Those 
with Type 2 diabetes can best manage their illness by avoiding sugar 
cookies. Those with Type 1 should keep a box of sugar cookies (well, 
perhaps glucose lollies are more appropriate) on hand for emergencies.

http://www.betterhealth.vic.gov.au/bhcv2/bhcarticles.nsf/pages/Diabetes_explained?open

In any case, most people with diabetes (or at least those who are still 
alive) are reasonably good at managing their illness and wouldn't make 
the choice you suggest. You have missed the point that people who misuse 
regexes are common in programming circles, while diabetics who eat a box 
of sugar cookies instead of a meal are rare.

To take your analogy to an extreme:

  Some people, when faced with a problem, say "I know, I'll cut 
  my arm off with a pocketknife!" Now they have two problems.

This is not insightful or useful. Except in the most specialised and 
extreme circumstances, such as being trapped in the wilderness with a 
boulder on your arm, nobody would consider this to be good advice. But 
using regexes to validate email addresses or parse HTML? The internet is 
full of people who thought that was a good idea.


[...]
> Yes, i'll admit, regexps are very cryptic, but once you grasp their
> intricacies, you appreciate the succinctness of there syntax, because,
> what makes them so powerful is not only the extents of their pattern
> matching abilities, but their conciseness.

Even Larry Wall says that regexes are too concise and cryptic:

http://perl6.org/archive/doc/design/apo/A05.html



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


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

2015-01-12 Thread Steven D'Aprano
On Mon, 12 Jan 2015 05:24:00 -0800, wxjmfauth wrote:

> To tell you the truth, I'm unable to
> put your product to work.


If you follow the instructions in the README, and it still doesn't work, 
that's a bug and I will be happy to fix it.

If you insist on doing things your own way, and breaking the package, 
then I cannot help you.



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


Re: I must be missing something obvious in installing Python 3.4.2...

2015-01-12 Thread cjgohlke
On Monday, January 12, 2015 at 10:09:03 PM UTC-8, Tim Golden wrote:
> On 12/01/2015 23:12, Andrew Koenig wrote:
> > Fixed it!
> >
> > The aforementioned article is correct. I downloaded the RegDelNull
> > program mentioned in the article
> > (http://technet.microsoft.com/en-us/sysinternals/bb897448.aspx) and
> > ran it on hkcr, hkcu, hklm, hku, and hkcc (short for
> > HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS,
> > and HKEY_CURRENT_CONFIG), respectively. It deleted a bunch of keys.
> > Rerunning the program you posted earlier revealed no keys with
> > embedded nulls in their names, and ensurepip now works.
> >
> > I have no idea how these keys got there. For all I know they are the
> > result of malware.
> >
> > I think it would be worthwhile changing the Python code to detect
> > nulls and perhaps issue a warning that directs people to this article
> > or something like it.
> >
> 
> Thanks, yes. Unfortunate that no-one's actually reported this before as 
> a bug -- as far as I'm aware. Would you be in a position to report this 
> at bugs.python.org? If not, I'll do it when I get a chance.
> 
> TJG


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


[RELEASE] ‘python-daemon’ version 2.0.2 released

2015-01-12 Thread Ben Finney
Howdy all,

I am pleased to announce the release of version 2.0.2 of the
‘python-daemon’ library.

The current release is always available at
https://pypi.python.org/pypi/python-daemon/>.

The project's forums and VCS are hosted at Alioth
https://alioth.debian.org/projects/python-daemon/>.


Significant changes since the previous version
==

This is a bug fix release, addressing packaging bugs:

* Declare test-time dependency on recent ‘unittest2’.
* Declare packaging-time dependency on ‘docutils’ library.

Many thanks to users who promptly reported these omissions.


What is the ‘python-daemon’ library?


‘python-daemon’ is a Python library to implement a well-behaved Unix
daemon process.

-- 
 \   “… one of the main causes of the fall of the Roman Empire was |
  `\that, lacking zero, they had no way to indicate successful |
_o__)  termination of their C programs.” —Robert Firth |
Ben Finney 


signature.asc
Description: Digital signature
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What does "pip install" do?

2015-01-12 Thread Fabien

On 12.01.2015 23:46, Chris Angelico wrote:

On Tue, Jan 13, 2015 at 9:35 AM, Fabien  wrote:

>BUT I'm still asking myself: what does "pip install"*concretely*  do on my
>virtual environment?

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?" ;-). 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) 

"Installing python packages" from the python docs 
(https://docs.python.org/3/installing/)


I've found a bit more about this ("How installation works") in the 
Python 3 doc which is now considered legacy and might therefore not be 
read:

https://docs.python.org/3.4/install/index.html#installing-python-modules-legacy-version

Fabien




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