Re: x=something, y=somethinelse and z=crud all likely to fail - how do i wrap them up

2016-01-31 Thread Steven D'Aprano
On Sun, 31 Jan 2016 05:29 pm, Veek. M wrote:

> I'm using lxml.html


Hmmm. Well, I've never used lxml, but the first obvious problem I see is
that your lines:

description = li_item.find_class('vip')[0].text_content()

link = li_item.find_class('vip')[0].get('href')

price_dollar = li_item.find_class('lvprice prc')[0].xpath('span')[0].text

bids = li_item.find_class('lvformat')[0].xpath('span')[0].text


look suspiciously like a violation of the Liskov Substitution Principle.
("Talk to your dog, not to the dog's legs!") A long series of chained dot
accesses (or equivalent getitem, call, getitem, dot, etc) is a code-smell
suggesting that you are trying to control your dog's individual legs,
instead of just calling the dog.

But, I'll assume that this is part of the design of lxml, and so allowed. So
let's refactor by adding some helper methods and tidying the parse_page
method. This will also make it easier to test, refactor and maintain the
code, especially if the format of the XML file changes.




def extract(self, item, clsname, extractor, default="unknown"):
"""Return the class of item, or default if unknown."""
try:
cls = item.find_class(clsname)
except lxml.ClassNotFoundError:  # what should this be?
return default
return extractor(cls)

def get_time(self, clsname, default='No time found'):
extractor = lambda obj: obj[0].xpath('span')[0].get('timems')
t = self.extract(li_item, clsname, extractor, None)
if t is None:
return default
return int(t)/1000 - time.time()

def parse_page(self, root):
for li_item in root.xpath(
'//li[re:test(@id, "^item[a-z0-9]+$")]', 
namespaces={'re': "http://exslt.org/regular-expressions"}
):
description = self.extract(li_item, 'vip',
lambda obj: obj[0].text_content(), "no description")
link = self.extract(li_item, 'vip', 
lambda obj: obj[0].get('href'))
price_dollar = self.extract(li_item, 'lvprice prc', 
lambda obj: obj[0].xpath('span')[0].text)
bids = self.extract(li_item, 'lvformat', 
lambda obj: obj[0].xpath('span')[0].text)
time_hrs = self.get_time('tme')
shipping = self.extract(li_item, 'lvshipping',
lambda obj: obj[0].xpath(
'span/span/span')[0].text_content()
)
print('{} {} {} {} {}'.format(
link, price_dollar, time_hrs, shipping, bids))
print('-'*70)


###


If you prefer a more Java-style object-oriented solution:



def get_class(self, item, clsname):
"""Return the class of item, or None if unknown."""
try:
return item.find_class(clsname)
except lxml.ClassNotFoundError:  # what should this be?
return None

def get_description(self, maybe_cls, default="unknown"):
if maybe_cls is None:
return default
return maybe_cls[0].text_content()

def get_link(self, maybe_cls, tag='href', default='none'):
if maybe_cls is None:
return default
return maybe_cls[0].get(tag)

def get_text(self, maybe_cls, default='unknown'):
if maybe_cls is None:
return default
return maybe_cls[0].xpath('span')[0].text

def get_time(self, maybe_cls, default='No time found'):
if maybe_cls is None:
return default
t = maybe_cls[0].xpath('span')[0].get('timems')
return int(t)/1000 - time.time()

def get_shipping(self, maybe_cls, default='unknown shipping'):
if maybe_cls is None:
return default
return maybe_cls[0].xpath('span/span/span')[0].text_content()

def parse_page(self, root):
for li_item in root.xpath(
'//li[re:test(@id, "^item[a-z0-9]+$")]', 
namespaces={'re': "http://exslt.org/regular-expressions"}
):
description = self.get_description(
self.get_class(li_item, 'vip'), "no description")
link = self.get_link(self.get_class(li_item, 'vip'))
price_dollar = self.get_text(
self.get_class(li_item, 'lvprice prc'))
bids = self.get_text(
self.get_class(li_item, 'lvformat')
time_hrs = self.get_time(self.get_class(li_item, 'tme'))
shipping = self.get_shipping(
self.get_class(li_item, 'lvshipping')
print('{} {} {} {} {}'.format(
link, price_dollar, time_hrs, shipping, bids))
print('-'*70)






Obviously I haven't tested this code.




-- 
Steven

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


Re: psss...I want to move from Perl to Python

2016-01-31 Thread Steven D'Aprano
On Sun, 31 Jan 2016 02:48 pm, Cameron Simpson wrote:

> I have. I've got one right here. It happens to be in perl, but it has been
> in need of a recode in Python for a long time. It has about 3000 regexps.

Wow. What's the code do?


-- 
Steven

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


Re: psss...I want to move from Perl to Python

2016-01-31 Thread Cameron Simpson

On 31Jan2016 20:23, Steven D'Aprano  wrote:

On Sun, 31 Jan 2016 02:48 pm, Cameron Simpson wrote:

I have. I've got one right here. It happens to be in perl, but it has been
in need of a recode in Python for a long time. It has about 3000 regexps.


Wow. What's the code do?


Adzapper. It has many many regexps matching URLs. (Actually a more globlike 
syntax, but it gets turned into a regexp.) You plug it into your squid proxy.


Cheers,
Cameron Simpson 
--
https://mail.python.org/mailman/listinfo/python-list


Re: x=something, y=somethinelse and z=crud all likely to fail - how do i wrap them up

2016-01-31 Thread Steven D'Aprano
On Sun, 31 Jan 2016 08:40 pm, Chris Angelico wrote:

> On Sun, Jan 31, 2016 at 8:21 PM, Steven D'Aprano 
> wrote:
>> Hmmm. Well, I've never used lxml, but the first obvious problem I see is
>> that your lines:
>>
>> description = li_item.find_class('vip')[0].text_content()
>>
>> link = li_item.find_class('vip')[0].get('href')
>>
>> price_dollar = li_item.find_class('lvprice prc')[0].xpath('span')[0].text
>>
>> bids = li_item.find_class('lvformat')[0].xpath('span')[0].text
>>
>>
>> look suspiciously like a violation of the Liskov Substitution Principle.
>> ("Talk to your dog, not to the dog's legs!") A long series of chained dot
>> accesses (or equivalent getitem, call, getitem, dot, etc) is a code-smell
>> suggesting that you are trying to control your dog's individual legs,
>> instead of just calling the dog.
> 
> (Isn't that the Law of Demeter, not LSP?)

D'oh!

I mean, yes, excellent, you have passed my test!

*wink*


> The principle of "one dot maximum" is fine when dots represent a form
> of ownership. The dog owns his legs; you own (or, have a relationship
> with) the dog. But in this case, the depth of subscripting is more 
> about the inherent depth of the document, and it's more of a data
> thing than a code one.

Yes. that's right. That's why I said it was a code smell, not necessarily
wrong. But you do make a good point -- the Law of Demeter is not
*necessarily* about the number of dots. But the number of dots is a good
hint that you're looking too deeply into an object.


> Imagine taking a large and complex JSON blob 
> and loading it into a Python structure with nested lists and dicts -
> it wouldn't violate software design principles to call up
> info["records"][3]["name"], even though that's three indirections in a
> row. Parsing HTML is even worse, as there's generally going to be
> numerous levels of structure that have no semantic meaning (they're
> there for layout) - so instead of three levels, you might easily have
> a dozen.

This might not be a Law of Demeter violation, but it's certain a violation
of "Flat Is Better Than Nested".



-- 
Steven

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


Re: x=something, y=somethinelse and z=crud all likely to fail - how do i wrap them up

2016-01-31 Thread Peter Otten
Veek. M wrote:

> Chris Angelico wrote:
> 
>> On Sun, Jan 31, 2016 at 3:58 PM, Veek. M  wrote:
>>> I'm parsing html and i'm doing:
>>>
>>> x = root.find_class(...
>>> y = root.find_class(..
>>> z = root.find_class(..
>>>
>>> all 3 are likely to fail so typically i'd have to stick it in a try.
>>> This is a huge pain for obvious reasons.
>>>
>>> try:
>>>  
>>> except something:
>>>  x = 'default_1'
>>> (repeat 3 times)
>>>
>>> Is there some other nice way to wrap this stuff up?
>> 
>> I'm not sure what you're using to parse HTML here (there are several
>> libraries for doing that), but the first thing I'd look for is an
>> option to have it return a default if it doesn't find something - even
>> if that default has to be (say) None.
>> 
>> But failing that, you can always write your own wrapper:
>> 
>> def find_class(root, ...):
>> try:
>> return root.find_class(...)
>> except something:
>> return 'default_1'
>> 
>> Or have the default as a parameter, if it's different for the different
>> ones.
>> 
>> ChrisA
> 
> I'm using lxml.html
> 
> def parse_page(self, root):
> for li_item in root.xpath('//li[re:test(@id, "^item[a-z0-9]+$")]',
> namespaces={'re': "http://exslt.org/regular-expressions"}):
> description = li_item.find_class('vip')[0].text_content()
> link = li_item.find_class('vip')[0].get('href')
> price_dollar = li_item.find_class('lvprice prc')
> [0].xpath('span')[0].text
> bids = li_item.find_class('lvformat')[0].xpath('span')[0].text
> 
> tme_time = li_item.find_class('tme')[0].xpath('span')
> [0].get('timems')
> if tme_time:
> time_hrs = int(tme_time)/1000 - time.time()
> else:
> time_hrs = 'No time found'
> 
> shipping = li_item.find_class('lvshipping')
> [0].xpath('span/span/span')[0].text_content()"
> 
> print('{} {} {} {} {}'.format(link, price_dollar, time_hrs,
> shipping, bids))
> 
print('-')

When you use XPath instead of the chained function calls your initial

> Pass the statement as a string to a try function?

idea works out naturally:

def parse_page(self, root):
def get_xpath(path, default=""):
result = li_item.xpath(path)
if result:
return " ".join(part.strip() for part in result)
return default

for li_item in root.xpath(
'//li[re:test(@id, "^item[a-z0-9]+$")]',
namespaces={'re': "http://exslt.org/regular-expressions"}):

description = get_xpath("*[@class='vip']//text()")
link = get_xpath("*[@class='vip']/@href")
price = get_xpath("*[@class='lvprice prc']/span/text()")
bids = get_xpath("*[@class='lvformat']/span/text()")
tme_time = get_xpath("*[@class='tme']/span/@timems", None)
if tme_time is not None:
time_hrs = int(tme_time)/1000 - time.time()
else:
time_hrs = "No time found"
shipping = get_xpath(
"*[@class='lvshipping']/span/span/span//text()")


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


Error : 0x80070570 file or directory is corrupted or unavailable

2016-01-31 Thread archi dsouza
Hi,

I was trying to install Python.exe in windows 8.1. But got error mention in
subject line. find attached log file.

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


Re: The computer that mastered Go

2016-01-31 Thread Bernardo Sulzbach
On Fri, Jan 29, 2016 at 6:53 PM, Ian Kelly  wrote:
>
> Given the game, and the fact that it's Google, I would be very
> disappointed if it's not written in Go.
>

Then be disappointed. The AI field seems to have strong feelings for
Lua. However using Go instead of C++ would have promoted their baby a
little.

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


ANN: distlib 0.2.2 released on PyPI

2016-01-31 Thread Vinay Sajip
I've just released version 0.2.2 of distlib on PyPI [1]. For newcomers,
distlib is a library of packaging functionality which is intended to be
usable as the basis for third-party packaging tools.

The main changes in this release are as follows:

* Fixed issue #81: Added support for detecting distributions installed by
  wheel versions >= 0.23 (which use metadata.json rather than pydist.json).
* Updated default PyPI URL to https://pypi.python.org/pypi
* Updated to use different formatting for description field for V1.1
  metadata.
* Corrected “classifier” to “classifiers” in the mapping for V1.0 metadata.
* Improved support for Jython when quoting executables in output scripts.
* Fixed issue #77: Made the internal URL used for extended metadata fetches
  configurable via a module attribute.
* Fixed issue #78: Improved entry point parsing to handle leading spaces in
  ini-format files.

A more detailed change log is available at [2].

Please try it out, and if you find any problems or have any suggestions for
improvements, please give some feedback using the issue tracker! [3]

Regards,

Vinay Sajip

[1] https://pypi.python.org/pypi/distlib/0.2.2
[2] https://goo.gl/M3kQzR
[3] https://bitbucket.org/pypa/distlib/issues/new
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: x=something, y=somethinelse and z=crud all likely to fail - how do i wrap them up

2016-01-31 Thread Chris Angelico
On Sun, Jan 31, 2016 at 8:21 PM, Steven D'Aprano  wrote:
> Hmmm. Well, I've never used lxml, but the first obvious problem I see is
> that your lines:
>
> description = li_item.find_class('vip')[0].text_content()
>
> link = li_item.find_class('vip')[0].get('href')
>
> price_dollar = li_item.find_class('lvprice prc')[0].xpath('span')[0].text
>
> bids = li_item.find_class('lvformat')[0].xpath('span')[0].text
>
>
> look suspiciously like a violation of the Liskov Substitution Principle.
> ("Talk to your dog, not to the dog's legs!") A long series of chained dot
> accesses (or equivalent getitem, call, getitem, dot, etc) is a code-smell
> suggesting that you are trying to control your dog's individual legs,
> instead of just calling the dog.

(Isn't that the Law of Demeter, not LSP?)

The principle of "one dot maximum" is fine when dots represent a form
of ownership. The dog owns his legs; you own (or, have a relationship
with) the dog. But in this case, the depth of subscripting is more
about the inherent depth of the document, and it's more of a data
thing than a code one. Imagine taking a large and complex JSON blob
and loading it into a Python structure with nested lists and dicts -
it wouldn't violate software design principles to call up
info["records"][3]["name"], even though that's three indirections in a
row. Parsing HTML is even worse, as there's generally going to be
numerous levels of structure that have no semantic meaning (they're
there for layout) - so instead of three levels, you might easily have
a dozen.

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


Re: psss...I want to move from Perl to Python

2016-01-31 Thread Cameron Simpson

On 30Jan2016 19:53, rusi  wrote:

On Sunday, January 31, 2016 at 9:18:31 AM UTC+5:30, Cameron Simpson wrote:

On 30Jan2016 19:22, rusi wrote:
>Have you ever seen a program that uses 512 re's?
>I havent :-)

I have. I've got one right here. It happens to be in perl, but it has been in
need of a recode in Python for a long time. It has about 3000 regexps.

Of course they will be explicitly compiled in the recode.


I would guess it needs more recoding than explicit compilation!
Maybe something like http://www.colm.net/open-source/ragel/
Unfortunately no python binding so far :-(


Interesting. Thanks,
Cameron Simpson 
--
https://mail.python.org/mailman/listinfo/python-list


Re: Unable to activate Python once installed, as far I am aware i have full permission. The download is in programs but does not show when i use run in windows start menu, any suggestions would be hel

2016-01-31 Thread Terry Reedy

On 1/30/2016 10:32 AM, Haydn James wrote:



Please repost with a short subject line, about 40 chars.

Put your messages in the body of the post, with adequate detail.
What version of Windows.
What version of Python.
Where did you get it.
How did you install it.
How did you try to run it. 'run in windows start menu' seems a bit garbled.

--
Terry Jan Reedy

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


Re: Error : 0x80070570 file or directory is corrupted or unavailable

2016-01-31 Thread Terry Reedy

On 1/31/2016 2:26 AM, archi dsouza wrote:


I was trying to install Python.exe in windows 8.1. But got error mention in
subject line.


Repeat such detail in the body of the message.  But first try a web search.


find attached log file.


Nope.  This is a no-attachment mail list.  YOU should look through the 
log looking for error information.  But search the web first.


--
Terry Jan Reedy

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


Re: The computer that mastered Go

2016-01-31 Thread Terry Reedy

On 1/30/2016 8:53 AM, Bernardo Sulzbach wrote:

On Fri, Jan 29, 2016 at 6:53 PM, Ian Kelly  wrote:


Given the game, and the fact that it's Google, I would be very
disappointed if it's not written in Go.



Then be disappointed. The AI field seems to have strong feelings for
Lua. However using Go instead of C++ would have promoted their baby a
little.


Google acquired DeepMind 2 years ago (for a reported $400 million). 
DeepMind began as an independent company at least 2 years before.  One 
of the founders wrote Theme Park in the 1990s with an innovative park 
visitor AI.  DeepMind itself first developed a generalized game AI to 
play Atari 2600 games.  https://www.youtube.com/watch?v=rbsqaJwpu6A

gives a better idea of what they are about.

--
Terry Jan Reedy

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


Re: carry **arguments through different scopes/functions

2016-01-31 Thread Terry Reedy

On 1/31/2016 7:19 AM, c.bu...@posteo.jp wrote:

I am not sure what the problem is here, so I don't really know how I
should call the subject for that question. Please offer a better
subject.

The code below is a extrem simplified example of the original one. But
it reproduce the problem very nice. Please focus on the variable
`return_code`.

There is a `list()` of numbers without the number `7` in. The code
check if the number `7` is in and should tell that it is not in. But it
does tell me that `7 is in`. ;)


Python-list is not Stackoverflow.  It is a text mailing list, not a web 
page.  Here, the backticks are not markup; they are just distracting 
noise.  Don't do this!



I think I didn't know some special things about scopes of variables in
Python. This might be a very good problem to learn more about that. But
I don't know on which Python topic I should focus here to find a
solution for my own.

 #!/usr/bin/env python3
 import sys

 def walkOn_ids(ids, handlerFunction, **handlerArgs):
 for one_id in ids:
 handlerFunction(one_id=one_id, **handlerArgs)
 print('after handler-call for id {}\t{}'
   .format(one_id, handlerArgs))


Ditto.  Here, the initial 4 space indents are not markup.  They just 
makes it impossible to cut, paste, and run your code without extra work.



--
Terry Jan Reedy

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


Re: psss...I want to move from Perl to Python

2016-01-31 Thread Rustom Mody
True...

$ python
Python 2.7.10 (default, Oct 14 2015, 16:09:02) 
[GCC 5.2.1 20151010] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> re._MAXCACHE
100

But 100 is still large enough that for most normal users/uses re-compilation is 
pointless.

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


Re: carry **arguments through different scopes/functions

2016-01-31 Thread Joel Goldstick
On Sun, Jan 31, 2016 at 7:19 AM,  wrote:

> I am not sure what the problem is here, so I don't really know how I
> should call the subject for that question. Please offer a better
> subject.
>
> The code below is a extrem simplified example of the original one. But
> it reproduce the problem very nice. Please focus on the variable
> `return_code`.
>
> There is a `list()` of numbers without the number `7` in. The code
> check if the number `7` is in and should tell that it is not in. But it
> does tell me that `7 is in`. ;)
>
> I think I didn't know some special things about scopes of variables in
> Python. This might be a very good problem to learn more about that. But
> I don't know on which Python topic I should focus here to find a
> solution for my own.
>
> #!/usr/bin/env python3
> import sys
>
> def walkOn_ids(ids, handlerFunction, **handlerArgs):
> for one_id in ids:
> handlerFunction(one_id=one_id, **handlerArgs)
> print('after handler-call for id {}\t{}'
>   .format(one_id, handlerArgs))
>

> def _on_id(one_id, return_code):
> if return_code is False:
> return
>

The above returns None, not False.  Maybe you want False, so return False.
However, when you call this function below, you set return_code = True, so
it will never be False

>
> if one_id == 7:
> return_code = True
> else:
> return_code = False
>

The above could be just:
   return one_id == 7

no need for if/else

>
> print('one_id: {}\treturn_code: {}'.format(one_id, return_code))
>
>
> def _isSevenInIt(ids):
> return_code = True
>
> walkOn_ids(ids=ids,
>handlerFunction=_on_id,
>return_code=return_code)
>
> return return_code
>

This will always return True because you set return_code = True and never
change it.  You might want this:

  return_code = walkOn_ids(...

>
>
> ids = [1,2,3,4,5,6,8,9]  # NO 7
> print(ids)
>
> if _isSevenInIt(ids) is True:
> print('7 is in')
> else:
> print('no 7 in it')
>
> sys.exit()
>
> Of course I could make `return_code` a `global` variable. But that is
> not the goal. The goal is to carry this variable inside the
> walker-function and bring the result back. In the original code I will
> use some more complexe data structures with `**handlerArgs`.
>

Before you start randomly making variables global, make sure you have
written code that does what you want it to.  Globals to fix bad coding will
quickly bring a big mess.  Of course its more subtle than this, but mostly
globals are a very bad idea.

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



-- 
Joel Goldstick
http://joelgoldstick.com/stats/birthdays
-- 
https://mail.python.org/mailman/listinfo/python-list


Unable to activate Python once installed, as far I am aware i have full permission. The download is in programs but does not show when i use run in windows start menu, any suggestions would be helpful

2016-01-31 Thread Haydn James

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


Re: The computer that mastered Go

2016-01-31 Thread Marko Rauhamaa
Bernardo Sulzbach :

> On Fri, Jan 29, 2016 at 6:53 PM, Ian Kelly  wrote:
>> Given the game, and the fact that it's Google, I would be very
>> disappointed if it's not written in Go.
>
> Then be disappointed. The AI field seems to have strong feelings for
> Lua. However using Go instead of C++ would have promoted their baby a
> little.

Lua is a fun little language.

Go gets a nod for some interesting ideas.

C++ has all but collapsed under its own weight, but has some expressive
advantages over C.

None of those languages gets me excited, though.

Somebody once asked the question, What if you were stranded on a desert
island and could bring an unlimited supply of one food item, which food
item would that be? And the answer, supposedly, was: strawberry ice
cream. Apparently it has the most balanced distribution of nutrients for
survival.

Now what if you were stranded on a desert island and could bring only
one programming language with you? I think Python is a great candidate
(even if there are other good contenders). It really provides a
well-balanced diet to most programming tasks.

Philosophically, Scheme gets me excited, and the down-to-earth nature of
plain old C is hard to beat.


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


carry **arguments through different scopes/functions

2016-01-31 Thread c.buhtz
I am not sure what the problem is here, so I don't really know how I
should call the subject for that question. Please offer a better
subject.

The code below is a extrem simplified example of the original one. But
it reproduce the problem very nice. Please focus on the variable
`return_code`.

There is a `list()` of numbers without the number `7` in. The code
check if the number `7` is in and should tell that it is not in. But it
does tell me that `7 is in`. ;)

I think I didn't know some special things about scopes of variables in
Python. This might be a very good problem to learn more about that. But
I don't know on which Python topic I should focus here to find a
solution for my own.

#!/usr/bin/env python3
import sys

def walkOn_ids(ids, handlerFunction, **handlerArgs):
for one_id in ids:
handlerFunction(one_id=one_id, **handlerArgs)
print('after handler-call for id {}\t{}'
  .format(one_id, handlerArgs))


def _on_id(one_id, return_code):
if return_code is False:
return

if one_id == 7:
return_code = True
else:
return_code = False

print('one_id: {}\treturn_code: {}'.format(one_id, return_code))


def _isSevenInIt(ids):
return_code = True

walkOn_ids(ids=ids,
   handlerFunction=_on_id,
   return_code=return_code)

return return_code


ids = [1,2,3,4,5,6,8,9]  # NO 7
print(ids)

if _isSevenInIt(ids) is True:
print('7 is in')
else:
print('no 7 in it')

sys.exit()

Of course I could make `return_code` a `global` variable. But that is
not the goal. The goal is to carry this variable inside the
walker-function and bring the result back. In the original code I will
use some more complexe data structures with `**handlerArgs`.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: psss...I want to move from Perl to Python

2016-01-31 Thread Terry Reedy

On 1/30/2016 10:22 PM, Rustom Mody wrote:

On Sunday, January 31, 2016 at 7:27:06 AM UTC+5:30, Steven D'Aprano wrote:

On Sunday 31 January 2016 09:18, Gregory Ewing wrote:



Correct. The re module keeps a cache of the last N regexes used, for some
value of N (possibly 10?) so for casual use there's no real point to pre-
compiling other than fussiness.

But if you have an application that makes heavy-duty use of regexes, e.g.
some sort of parser with dozens of distinct regexes, you might not want to
rely on the cache.



import re
re._MAXCACHE

512




Have you ever seen a program that uses 512 re's?
I havent :-)


I do not know if the cache was always there.  It used to be smaller, 
maybe 100, then 200.


--
Terry Jan Reedy

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


Re: x=something, y=somethinelse and z=crud all likely to fail - how do i wrap them up

2016-01-31 Thread Chris Angelico
On Sun, Jan 31, 2016 at 9:14 PM, Steven D'Aprano  wrote:
>> Imagine taking a large and complex JSON blob
>> and loading it into a Python structure with nested lists and dicts -
>> it wouldn't violate software design principles to call up
>> info["records"][3]["name"], even though that's three indirections in a
>> row. Parsing HTML is even worse, as there's generally going to be
>> numerous levels of structure that have no semantic meaning (they're
>> there for layout) - so instead of three levels, you might easily have
>> a dozen.
>
> This might not be a Law of Demeter violation, but it's certain a violation
> of "Flat Is Better Than Nested".

Oh, absolutely! But "Flat is better than nested" is a principle of
design, and when you're parsing someone else's data structure, you
follow their design, not yours. This isn't the best of designs, but
it's what works with the file he has.

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


Re: carry **arguments through different scopes/functions

2016-01-31 Thread Peter Otten
c.bu...@posteo.jp wrote:

> I am not sure what the problem is here, so I don't really know how I
> should call the subject for that question. Please offer a better
> subject.
> 
> The code below is a extrem simplified example of the original one. But
> it reproduce the problem very nice. Please focus on the variable
> `return_code`.
> 
> There is a `list()` of numbers without the number `7` in. The code
> check if the number `7` is in and should tell that it is not in. But it
> does tell me that `7 is in`. ;)
> 
> I think I didn't know some special things about scopes of variables in
> Python. This might be a very good problem to learn more about that. But
> I don't know on which Python topic I should focus here to find a
> solution for my own.
> 
> #!/usr/bin/env python3
> import sys
> 
> def walkOn_ids(ids, handlerFunction, **handlerArgs):
> for one_id in ids:
> handlerFunction(one_id=one_id, **handlerArgs)
> print('after handler-call for id {}\t{}'
>   .format(one_id, handlerArgs))
> 
> 
> def _on_id(one_id, return_code):
> if return_code is False:
> return
> 
> if one_id == 7:
> return_code = True
> else:
> return_code = False
> 
> print('one_id: {}\treturn_code: {}'.format(one_id, return_code))
> 
> 
> def _isSevenInIt(ids):
> return_code = True
> 
> walkOn_ids(ids=ids,
>handlerFunction=_on_id,
>return_code=return_code)
> 
> return return_code
> 
> 
> ids = [1,2,3,4,5,6,8,9]  # NO 7
> print(ids)
> 
> if _isSevenInIt(ids) is True:
> print('7 is in')
> else:
> print('no 7 in it')
> 
> sys.exit()
> 
> Of course I could make `return_code` a `global` variable. But that is
> not the goal. The goal is to carry this variable inside the
> walker-function and bring the result back. In the original code I will
> use some more complexe data structures with `**handlerArgs`.

The solution that requires the least changes to your code is probably to 
turn return_code into an attribute of a mutable argument (called context 
below):

#!/usr/bin/env python3
import sys
import types


def walkOn_ids(ids, handlerFunction, **handlerArgs):
for one_id in ids:
handlerFunction(one_id=one_id, **handlerArgs)
print('after handler-call for id {}\t{}'
  .format(one_id, handlerArgs))


def _on_id(one_id, context):
if context.return_code:
return

if one_id == 7:
context.return_code = True

print('one_id: {}\treturn_code: {}'.format(one_id, context.return_code))


def _isSevenInIt(ids):
context = types.SimpleNamespace(return_code=False)

walkOn_ids(ids=ids,
   handlerFunction=_on_id,
   context=context)

return context.return_code


ids = [1, 2, 3, 4, 5, 6, 8, 9]  # NO 7
print(ids)

if _isSevenInIt(ids):
print('7 is in')
else:
print('no 7 in it')

However, the resulting code is not very pythonic.

In idiomatic Python the toy problem you present -- looking for the first 
item in an iterable that fulfills a certain condition -- would be solved 
like that:

def equals_seven(id):
return id == 7

ids = [1, 2, 3, 4, 5, 6, 8, 9]  # NO 7

if any(equals_seven(id) for id in ids):
print('7 is in')
else:
print('no 7 in it')

It is likely that your actual code can be simplified, too.

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


Re: deb's for wheezy?

2016-01-31 Thread Mark Lawrence

On 31/01/2016 15:33, Gene Heskett wrote:

Greetings;

It seems I need python3-pil and python3-pil.imagetk for a camera display
& control utility I would like to run for a machine vision application.

Is there a URL where I can find wheezy versions of those? I have python3,
V3.2.3 here.

Thanks everybody.

Cheers, Gene Heskett



I don't know about Mr Wheezy but I think you'll want pillow, the 
friendly fork of pil, see https://pillow.readthedocs.org/en/3.1.x/ and 
https://pypi.python.org/pypi/Pillow/3.1.0


--
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: x=something, y=somethinelse and z=crud all likely to fail - how do i wrap them up

2016-01-31 Thread Veek. M
Thanks guys: you've given me some good ideas - I really need to re-read 
the lxml docs for xpath. (basically trying to scrape ebay and score a 
mobo - ebaysdk doesn't work) Also need to google those principles :)  
thanks! (i knew one shouldn't overly rely on chained attribute lookups - 
didn't figure that had a name :))
-- 
https://mail.python.org/mailman/listinfo/python-list


deb's for wheezy?

2016-01-31 Thread Gene Heskett
Greetings;

It seems I need python3-pil and python3-pil.imagetk for a camera display 
& control utility I would like to run for a machine vision application.

Is there a URL where I can find wheezy versions of those? I have python3, 
V3.2.3 here.

Thanks everybody.

Cheers, Gene Heskett
-- 
"There are four boxes to be used in defense of liberty:
 soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author)
Genes Web page 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: psss...I want to move from Perl to Python

2016-01-31 Thread Paul Rubin
Rustom Mody  writes:
> I would guess it needs more recoding than explicit compilation!
> Maybe something like http://www.colm.net/open-source/ragel/
> Unfortunately no python binding so far :-(

Neat, that looks kind of like lex/flex but with more backends.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: psss...I want to move from Perl to Python

2016-01-31 Thread Paul Rubin
Cameron Simpson  writes:
> Adzapper. It has many many regexps matching URLs. (Actually a more
> globlike syntax, but it gets turned into a regexp.) You plug it into
> your squid proxy.

Oh cool, is that out there in circulation?

It sounds like the approach of merging all the regexes into one and
compiling to a FSM could be a big win.  I wouldn't expect too big a
state space explosion.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: show instant data on webpage

2016-01-31 Thread mustang



How about https://plot.ly/python/ ?


Free versione: Create 1 private chart
And if I've 3 sensor with 3 different plots?
--
https://mail.python.org/mailman/listinfo/python-list


Re: x=something, y=somethinelse and z=crud all likely to fail - how do i wrap them up

2016-01-31 Thread Larry Hudson via Python-list

On 01/30/2016 10:29 PM, Veek. M wrote:
[snip]

Trivial comment (and irrelevant to your question)...

Replace your
print('-')
with the shorter
print('-' * 65)

Of course, feel free to disagree if you think the longer version is visually 
more obviously a line.

 -=- Larry -=-

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


Re: create Email with multiple HTML blocks embedded

2016-01-31 Thread Thomas 'PointedEars' Lahn
kevind0...@gmail.com wrote:

> Following your suggestions, I now have code that looks like the below.
> I get the same result.   First HTML is displayed in body of email and
> second shows up as an attachment.

Works as designed.  The message structure is correct now, but there is no 
plain-text part.  So a MUA that is capable and configured to display HTML
e-mails has no choice but to assume that you wanted the first part to be 
displayed directly as it is not marked as an attachment.  Consequently, it 
*must* consider and display the second HTML part as an attachment.

It might help if you inserted a plain-text part before the HTML part, and 
you should have such (so that people who are not keen to see HTML messages 
see anything in that message), but you should also mark the parts as 
*attachments* that you want to be considered that by the MUA.

See RFC 5233 and RTFM for details.
 
> A dump of the email message is also below.
> 
> Maybe this would help.

It does (although you could have trimmed the parts in the middle), but next 
time you want to post a follow-up to *my* message (or that of whoever posted 
a follow-up to yours), not to your own.  This newsgroup/mailing list is a 
discussion medium that uses *threads*.  As you have not observed that, I 
have seen your posting only now as I checked the newsgroup manually, while 
otherwise I would have been notified of it when I started my newsreader and 
automatically checked all newsgroups for new postings.

Since you are using Google Groups, see
 pp. for the details.

-- 
PointedEars

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


Re: show instant data on webpage

2016-01-31 Thread Joel Goldstick
On Sun, Jan 31, 2016 at 2:19 PM, mustang  wrote:

>
> How about https://plot.ly/python/ ?
>>
>> Free versione: Create 1 private chart
> And if I've 3 sensor with 3 different plots?
> --
> https://mail.python.org/mailman/listinfo/python-list
>

You need free and private?  The price is low -- if this is a business why
not pay?  If not a business, why private?

There is another charting library called pygal which is great.  It can
produce output to .svg file format or .png.  You could distribute the files
which will display in a browser.  Or you could build them into web pages if
you are so inclined to build a website.  It seems that isn't what you want
to do.  You could also upload images to some free image site. Photobucket
supports .png images.  pygal can create .png images



-- 
Joel Goldstick
http://joelgoldstick.com/stats/birthdays
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: psss...I want to move from Perl to Python

2016-01-31 Thread Cameron Simpson

On 31Jan2016 09:49, Paul Rubin  wrote:

Cameron Simpson  writes:

Adzapper. It has many many regexps matching URLs. (Actually a more
globlike syntax, but it gets turned into a regexp.) You plug it into
your squid proxy.


Oh cool, is that out there in circulation?


Yes:

 http://adzapper.sourceforge.net/

which includes the installation instructions (install script, add a line to 
squid.conf).


However my publication workflow is broken. (And source forge isn't what it used 
to be.) I need to get the update process improved. I'm happy to send the latest 
copy to people by private email.



It sounds like the approach of merging all the regexes into one and
compiling to a FSM could be a big win.  I wouldn't expect too big a
state space explosion.


Perhaps so. The existing script (a) merges regexps for successive patterns for 
the same class and (b) use's perl's "study" function, which examines a string 
which will have several regexps applies to it - it nots things like character 
positions I gather, which is used in the matching process. Since the zapper 
applies all the rules to most URLs this is a performance win.


Cheers,
Cameron Simpson 
--
https://mail.python.org/mailman/listinfo/python-list


Re: create Email with multiple HTML blocks embedded

2016-01-31 Thread Thomas 'PointedEars' Lahn
Thomas 'PointedEars' Lahn wrote:

> See RFC 5233 and RTFM for details.

RFC _5322_ (“Internet Message Format”)



-- 
PointedEars

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


Re: psss...I want to move from Perl to Python

2016-01-31 Thread Michael Torrie
On 01/31/2016 03:34 PM, Fillmore wrote:
> On 01/30/2016 05:26 AM, wxjmfa...@gmail.com wrote:
> 
>>> Python 2 vs python 3 is anything but "solved".
>>
>>
>> Python 3.5.1 is still suffering from the same buggy
>> behaviour as in Python 3.0 .
> Can you elaborate?

Sad to say jmf is a long-time troll on this list who seems to thinks he
understands Unicode better than he does.  We've been round and round
with him on this list over his issues and he's since been banned from
the email part of this list, but still spams the Usenet side from time
to time. If you're using NNTP, just add him to your kill file or block list.

There's nothing to elaborate on.  Python 3.5's integrated handling of
unicode is the best I've seen in any language.  It just works and you
don't need to worry about it (unlike most other languages), other than
to remember that anytime you take bytes into the program (say from a
file) they must be "decoded" into unicode, and whenever you write bytes
out (say to a file or socket) unicode strings must be "encoded" to a
desired byte encoding, such as UTF-8.  Python's in-memory representation
of unicode strings is correct (unlike Python 2.7, which had UCS-2 narrow
builds that can't handle non-BLM code points) for all unicode codepoints.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: carry **arguments through different scopes/functions

2016-01-31 Thread Thomas 'PointedEars' Lahn
Terry Reedy wrote:

> On 1/31/2016 7:19 AM, c.bu...@posteo.jp wrote:
>> I am not sure what the problem is here, so I don't really know how I
>> should call the subject for that question. Please offer a better
>> subject.
>>
>> The code below is a extrem simplified example of the original one. But
>> it reproduce the problem very nice. Please focus on the variable
>> `return_code`.
>>
>> There is a `list()` of numbers without the number `7` in. The code
>> check if the number `7` is in and should tell that it is not in. But it
>> does tell me that `7 is in`. ;)
> 
> Python-list is not Stackoverflow.  It is a text mailing list, not a web
> page.  Here, the backticks are not markup; they are just distracting
> noise.  Don't do this!

IBTD.  I find it useful to mark up source code in plain text prose even if 
it is not rendered differently (you cannot know that; there are various user 
agents, and there could be one that does).  I would have used typographical 
quotation marks (“…”) instead, but I think backticks are a good alternative 
in US-ASCII, better than straight quotation marks or apostrophes at least 
since those delimit strings in Python which can lead to ambiguities.  (That 
is probably also why not only Stack Overflow, but *Markdown* uses them this 
way as Stack Overflow just uses a Markdown flavour: 
)
 
>>  #!/usr/bin/env python3
>>  import sys
>>
>>  def walkOn_ids(ids, handlerFunction, **handlerArgs):
>>  for one_id in ids:
>>  handlerFunction(one_id=one_id, **handlerArgs)
>>  print('after handler-call for id {}\t{}'
>>.format(one_id, handlerArgs))
> 
> Ditto.  Here, the initial 4 space indents are not markup.  They just
> makes it impossible to cut, paste, and run your code without extra work.

That much is true, because *in Python* indentation means program structure; 
so the Python program above is syntactically invalid and should not have 
been posted this way.

However, with a good editor the "extra work" is limited to one application 
of Shift+Tab, and the extra indentation does not hinder the understanding of 
the posted source code as much as you imply.

-- 
PointedEars

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


Re: psss...I want to move from Perl to Python

2016-01-31 Thread Fillmore

On 01/30/2016 05:26 AM, wxjmfa...@gmail.com wrote:


Python 2 vs python 3 is anything but "solved".



Python 3.5.1 is still suffering from the same buggy
behaviour as in Python 3.0 .



Can you elaborate?



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


ts.plot() pandas: No plot!

2016-01-31 Thread Paulo da Silva
Hi!

I am learning pandas and following the tutorial I tried the following:
ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000',
periods=1000))
ts = ts.cumsum()
ts.plot()

No plot is produced!
Only the following output:


Any clue on what is happening?

I'm using kubuntu and python3.

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


Re: ts.plot() pandas: No plot!

2016-01-31 Thread Chris Angelico
On Mon, Feb 1, 2016 at 11:46 AM, Paulo da Silva
 wrote:
> I am learning pandas and following the tutorial I tried the following:
> ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000',
> periods=1000))
> ts = ts.cumsum()
> ts.plot()
>
> No plot is produced!
> Only the following output:
> 
>
> Any clue on what is happening?
>
> I'm using kubuntu and python3.
>

Hmm. Normally I would expect matplotlib to pop up a graph there. Are
you running this from a terminal, or from some sort of GUI? It might
make a difference to how it brings up the graph. Interestingly, I'm
not able to run your example on my system:

>>> pd.__version__
'0.17.1'
>>> pd.date_range('1/1/2000', periods=1000)
ValueError: Error parsing datetime string "1/1/2000" at position 1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/local/lib/python3.6/site-packages/pandas/tseries/index.py",
line 1923, in date_range
closed=closed, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/pandas/util/decorators.py",
line 89, in wrapper
return func(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/pandas/tseries/index.py",
line 237, in __new__
ambiguous=ambiguous)
  File "/usr/local/lib/python3.6/site-packages/pandas/tseries/index.py",
line 379, in _generate
start = Timestamp(start)
  File "pandas/tslib.pyx", line 299, in pandas.tslib.Timestamp.__new__
(pandas/tslib.c:8973)
  File "pandas/tslib.pyx", line 1163, in
pandas.tslib.convert_to_tsobject (pandas/tslib.c:22522)
  File "pandas/tslib.pyx", line 1271, in
pandas.tslib.convert_str_to_tsobject (pandas/tslib.c:24121)
  File "pandas/src/datetime.pxd", line 141, in datetime._string_to_dts
(pandas/tslib.c:80505)
SystemError:  returned a result with an error set

So it's possible you've run into something that's changed between
versions. Your original example works on my 2.7, although it doesn't
pop up a graph. So now I'm a bit lost. :)

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


Re: psss...I want to move from Perl to Python

2016-01-31 Thread Chris Angelico
On Mon, Feb 1, 2016 at 9:34 AM, Fillmore  wrote:
> On 01/30/2016 05:26 AM, wxjmfa...@gmail.com wrote:
>
>>> Python 2 vs python 3 is anything but "solved".
>>
>>
>>
>> Python 3.5.1 is still suffering from the same buggy
>> behaviour as in Python 3.0 .
>
>
>
> Can you elaborate?

This is jmf. His posts are suppressed from the mailing list, because
the only thing he ever says is that Python 3's "Unicode by default"
behaviour is fundamentally and mathematically wrong, on the basis of
microbenchmarks showing a performance regression compared to his
beloved - and buggy - narrow build of Python 2.7. (I'm not certain,
but I think the regression might even have been fixed now. Or maybe he
has other regressions to moan about.)

Here's a facts-only summary of Unicode handling in several different
CPython [0] builds.

* Python 2.7 comes in two flavours, selected at compile time. A "Wide"
build is the default on Unix-like platforms, and it uses 32-bit
Unicode characters. In other words, the string b"abc" takes up three
bytes, but the string u"abc" takes up twelve. [1] These builds are
perfectly consistent; a Unicode character *always* takes exactly 4
bytes, and indexing and subscripting are perfectly correct.

* A "Narrow" build of Python 2.7 (the default on Windows) uses 16-bit
Unicode characters. The string b"abc" still takes up three bytes, but
u"abc" takes only six - however, the same string with three astral
characters would take up twelve bytes. These builds are thus
inconsistent, but potentially more efficient - a thousand BMP
characters followed by a single SMP character would take up only 2004
bytes, rather than 4004 as a wide build would use.

* Starting with Python 3.0, a default quoted string is a Unicode
string. That doesn't change anything about these considerations, but
it does mean that "abc" suddenly takes up a lot more room than it used
to (because it's now equivalent to u"abc" rather than b"abc").

* Python 3.3 introduced a new "Flexible String Representation", which
you can read about in detail in PEP 393. Strings are now stored as
compactly as possible; u"Hello!" (all ASCII) takes up six bytes,
u"¡Hola!" (Latin-1) also takes up six bytes, u"Привет" (Basic
Multilingual Plane) takes up twelve, and u"Hi! 😀😁" (or u"Hi!
\U0001f600\U0001f601" if your mailer doesn't have those characters)
takes up twenty-four. Each string has a length of 6, as given by
len(x), but takes up differing amounts of space according to actual
needs.


The issue jmf has is with the way the FSR has to "widen" a string. If
you take a megabyte of all-ASCII text (stored one byte per character)
and append one astral character to it, the resulting string has to be
stored four bytes per character, even for the ASCII ones. This is to
make sure that indexing and slicing work correctly and efficiently,
but it does come at a cost - it takes time to copy all those
characters into the new wider string. On microbenchmarks doing exactly
this, it's clear that Python 3 is paying a price. But has it truly
suffered?

rosuav@sikorsky:~$ python -m timeit -s "s=u'a'*1048576" "len(s+u'\U0001f600')"
1 loops, best of 3: 197 usec per loop
rosuav@sikorsky:~$ python3 -m timeit -s "s=u'a'*1048576" "len(s+u'\U0001f600')"
1 loops, best of 3: 148 usec per loop
rosuav@sikorsky:~$ python -m timeit -s "s=u'a'*1048576" "len(s+u'b')"
1 loops, best of 3: 187 usec per loop
rosuav@sikorsky:~$ python3 -m timeit -s "s=u'a'*1048576" "len(s+u'b')"
1 loops, best of 3: 31.6 usec per loop
rosuav@sikorsky:~$ python -c 'import sys; print(sys.version)'
2.7.11 (default, Jan 11 2016, 21:04:40)
[GCC 5.3.1 20160101]
rosuav@sikorsky:~$ python3 -c 'import sys; print(sys.version)'
3.6.0a0 (default:5452e4b5c007, Feb  1 2016, 07:28:50)
[GCC 5.3.1 20160121]

The other consideration is that, *on Windows only*, this operation
takes more memory under 3.6 than under 2.7, because 2.7 will keep
storing the 'a' in 16 bits and then just slap a two-code-unit smiley
to the end; but on the flip side, 3.6 has been storing that all-ASCII
string in *8* bits per character. Most of your programs will be full
of ASCII strings - remember, all your variable names are string keys
into some dictionary [2], and every time you call up a built-in
function or standard library module, you'll be using an ASCII-only
name to reference it. Halving their storage space makes a significant
difference; and doubling the size of a very few strings in a very few
programs is worth the correctness we gain by not having to worry about
string index bugs.

So in summary: Take no notice of jmf; he's a crank.

ChrisA

[0] Other Python implementations may be very different, but it's
CPython that most people are looking at.
[1] If you use sys.getsizeof() on these strings, you'll find that they
actually take up a lot more space than I'm talking about. That's
because there's overheads on string objects, which dominate tiny
strings. But for large strings, where the performance difference
actually matters, the storage spa

Re: carry **arguments through different scopes/functions

2016-01-31 Thread Steven D'Aprano
On Sun, 31 Jan 2016 11:19 pm, c.bu...@posteo.jp wrote:

> I am not sure what the problem is here, so I don't really know how I
> should call the subject for that question. Please offer a better
> subject.
> 
> The code below is a extrem simplified example of the original one. But
> it reproduce the problem very nice. Please focus on the variable
> `return_code`.

The problem with return_code is that you are trying to use it as an "output
parameter" or "call by reference" parameter. You want behaviour like this:


a = 1
b = 2

def test_output_parameter(the_var):
print(the_var)
# Set the_var by reference.
the_var = 999

test_output_parameter(a)  # sets a
test_output_parameter(b)  # sets b
assert (a == 999) and (b == 999)



This cannot work in Python: Python is never call by reference. If somebody
has told you that it is call by reference, they are wrong.

You can *simulate* call by reference output parameters by using a list, but
that's the wrong way to solve this problem. The right way is for your
function to return a value, which you then assign to the variable you want
to change:


a = 1
b = 2

def test_return(the_var):
print(the_var)
return 999

a = test_return(a)
b = test_return(b)
assert (a == 999) and (b == 999)




Looking at your example code, we can replace (most of it) with these six
lines:


# Version 1: best, shortest, fastest way, with no callbacks.

ids = [1,2,3,4,5,6,8,9]  # NO 7
print(ids)
if 7 in ids:
print('7 is in')
else:
print('no 7 in it')



ignoring what looks like prints trying to debug the "return_code" problem.

If you really need to use a callback structure, then try something like
this, using a generator to return each individual callback result:



# Version 2: a mess -- don't do this unless you must!

def walker(ids, handlerFunction, **handlerArgs):
for one_id in ids:
print('handler-call for id {}\t{}'.format(one_id, handlerArgs))
yield handlerFunction(one_id=one_id, **handlerArgs)

def on_id_callback(one_id, return_code):
if return_code is False:
# Why are we skipping this case?
return False
return_code = (one_id == 7)
print('one_id: {}\treturn_code: {}'.format(one_id, return_code))

def seven_in_it(ids):
return_code = True
for return_code in walker(ids, on_id_callback, return_code=return_code):
# This loop runs for the side-effects!
pass
return return_code


ids = [1,2,3,4,5,6,8,9]  # NO 7
print(ids)

if seven_in_it(ids):
print('7 is in')
else:
print('no 7 in it')




Running that code gives this output:

[1, 2, 3, 4, 5, 6, 8, 9]
handler-call for id 1   {'return_code': True}
one_id: 1   return_code: False
handler-call for id 2   {'return_code': True}
one_id: 2   return_code: False
handler-call for id 3   {'return_code': True}
one_id: 3   return_code: False
handler-call for id 4   {'return_code': True}
one_id: 4   return_code: False
handler-call for id 5   {'return_code': True}
one_id: 5   return_code: False
handler-call for id 6   {'return_code': True}
one_id: 6   return_code: False
handler-call for id 8   {'return_code': True}
one_id: 8   return_code: False
handler-call for id 9   {'return_code': True}
one_id: 9   return_code: False
no 7 in it



But this is truly awful code. My sympathies if you are forced to use it, my
apologies for being so blunt if you wrote it. A better way to deal with
this would be something like this:


# Version 3: better.

def verbose_equals_seven(one_id):
flag = (one_id == 7)
print('one_id: {}\treturn_code: {}'.format(one_id, flag))
return flag

ids = [1,2,3,4,5,6,8,9]  # NO 7
print(ids)

if any(verbose_equals_seven(id) for id in ids):
print('7 is in')
else:
print('no 7 in it')




Running this Version 3 code gives this output:


[1, 2, 3, 4, 5, 6, 8, 9]
one_id: 1   return_code: False
one_id: 2   return_code: False
one_id: 3   return_code: False
one_id: 4   return_code: False
one_id: 5   return_code: False
one_id: 6   return_code: False
one_id: 8   return_code: False
one_id: 9   return_code: False
no 7 in it




Not very exciting. But if we try again with a list containing id=7, we see
that any() stops processing as soon as it has a success:

ids = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
print(ids)
if any(verbose_equals_seven(id) for id in ids):
print('7 is in')
else:
print('no 7 in it')



prints this output:


[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
one_id: 9   return_code: False
one_id: 8   return_code: False
one_id: 7   return_code: True
7 is in


> In the original code I will
> use some more complexe data structures with `**handlerArgs`.

Have you tried it? If this complex data structure is *mutable*, and you
change it, that change will be seen everywhere. Remember right at the start
of my post I mentioned that you can simulate "output parameters" with a
list? This is similar: because objects are not copied when you pass them to
a function, mutati

Re: psss...I want to move from Perl to Python

2016-01-31 Thread Terry Reedy

On 1/31/2016 5:34 PM, Fillmore wrote:

On 01/30/2016 05:26 AM, wxjmfa...@gmail.com wrote:


Python 2 vs python 3 is anything but "solved".

Python 3.5.1 is still suffering from the same buggy
behaviour as in Python 3.0 .


Can you elaborate?


Please do not propagate jmf's repeated trolls to python-list

--
Terry Jan Reedy

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


Re: ts.plot() pandas: No plot!

2016-01-31 Thread Paulo da Silva
Às 01:43 de 01-02-2016, Mark Lawrence escreveu:
> On 01/02/2016 00:46, Paulo da Silva wrote:
...

>>
> 
> Is it as simple as adding a call to ts.show() ?
> 
Thanks for the clue!
Not so simple however.
Needed to do
import matplotlib.pyplot as plt
plt.show()

Thank you :-)
Paulo

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


Re: psss...I want to move from Perl to Python

2016-01-31 Thread Rustom Mody
On Monday, February 1, 2016 at 5:22:22 AM UTC+5:30, Terry Reedy wrote:
> On 1/31/2016 5:34 PM, Fillmore wrote:
> > On 01/30/2016 05:26 AM, wxjmfauth wrote:
> >
> >>> Python 2 vs python 3 is anything but "solved".
> >> Python 3.5.1 is still suffering from the same buggy
> >> behaviour as in Python 3.0 .
> >
> > Can you elaborate?
> 
> Please do not propagate jmf's repeated trolls to python-list


On Saturday, January 30, 2016 at 3:57:28 PM UTC+5:30, wxjmf wrote:

> Python 3.5.1 is still suffering from the same buggy
> behaviour as in Python 3.0 .

is banned

whereas this is not:

On Saturday, January 30, 2016 at 3:01:09 AM UTC+5:30, Rick Johnson wrote:
> On Friday, January 29, 2016 at 6:21:21 AM UTC-6, Ulli Horlacher wrote:
> > I nearly gave up with Python at the very beginning before
> > I realised that OO-programming is optional in Python! :-)
> > Most tutorials I found so far makes OO mandatory.
> 
> Just more evidence that old dogs are incapable of learning
> new tricks. Either learn how to wield Neuroplasticity to
> your advantage, or go curl up into a ball and wait for death
> to come. People who are unwilling to "expanding their
> intellectual horizons" make me sick!!!


Not to mention endless screeds like this one:



On Saturday, January 30, 2016 at 4:00:12 AM UTC+5:30, Rick Johnson wrote:
> On Friday, January 29, 2016 at 2:49:24 PM UTC-6, sohca...@gmail.com wrote:
> 
> > I'm convinced that anyone who actually prefers Perl's
> > syntax over Python is suffering from Stockholm Syndrome.
> >
> > [...]
> >
> > Readability counts.  I'd say readability is one of the
> > most important features of a language, as you will read
> > your code far more than you write it.  Perl is not
> > readable.  I don't care how powerful your language is if
> > you can't read it.
> 
> EXACTLY!
> 
> Which is the same reason why natural language is bound by
> many structural rules. For instance: we utilize "syntactical
> structures" like sentences and paragraphs to create
> "comprehensible groupings", and we *NEVER* want to
> arbitrarily, or randomly, use more than one space between
> words, or more than one line between paragraphs.
> 
> STRUCTURE IS IMPORTANT!
> 
> And the only thing more important than a "self-imposed
> structure" is a steadfast adherence to the "collective style
> guides" of written communication. When we *ALL* utilize a
> familiar structure, we will *ALL* spend less time
> *CONSCIOUSLY INTERPRETING* superficial structural details,
> and more time *ABSORBING* the actual meaning of the content.
> 
> ABSORPTION IS THE GOAL, NOT ABERRATION!
> 
> The goal of written communication is no different than any
> other technology. We should strive to abstract away as much
> as possible to the sub-conscience processes of our mind as
> we can, so that we can target our mental focus purely on the
> comprehension of content, *NOT* comprehension of structure!
> When faced with an unfamiliar "syntactical structure", our
> high level focus becomes "mired in the minutiae of the
> superficial".
> 
> EVEN WHEN NECESSARY, THE SUPERFICIAL IS NOT IMPORTANT!
> 
> The goal of communication should never be (either
> intentional or not) to distract or impress our readers with
> our capacity to create "self-aggrandizing ornateness of
> structure", which will undoubtedly obfuscate the intended
> message, no, but to *STRICTLY* follow the collective
> standards and practices of "acceptable syntactical
> structuring" that will *facilitate* a smooth transition
> between: ideas that are codified into symbolic languages,
> and the translation of those linguistic symbols into concepts
> in the mind of the reader.
> 
> ABSTRACTIONS ARE VITAL TO OUR COMPREHENSION OF COMPLEX
> COMMUNICATION MEDIUMS!
> 
> For communication to function (at it's most basic level)
> these abstractions must exist simultaneously in our codified
> symbolic languages *AND* in our mental processes that
> interpret them. But whilst our mental abstractions are
> mostly unconscious, they can become disturbed when
> dissonance is injected into symbolic languages in the form
> of "poor syntactical structure". Break either link in the
> chain, and a "smooth transition of ideas" becomes untenable.

Can someone explain the policy?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: psss...I want to move from Perl to Python

2016-01-31 Thread Chris Angelico
On Mon, Feb 1, 2016 at 1:59 PM, Rustom Mody  wrote:
> On Saturday, January 30, 2016 at 3:57:28 PM UTC+5:30, wxjmf wrote:
>
>> Python 3.5.1 is still suffering from the same buggy
>> behaviour as in Python 3.0 .
>
> is banned
>
> whereas this is not:
>
> On Saturday, January 30, 2016 at 3:01:09 AM UTC+5:30, Rick Johnson wrote:
>> On Friday, January 29, 2016 at 6:21:21 AM UTC-6, Ulli Horlacher wrote:
>> > I nearly gave up with Python at the very beginning before
>> > I realised that OO-programming is optional in Python! :-)
>> > Most tutorials I found so far makes OO mandatory.
>>
>> Just more evidence that old dogs are incapable of learning
>> new tricks. Either learn how to wield Neuroplasticity to
>> your advantage, or go curl up into a ball and wait for death
>> to come. People who are unwilling to "expanding their
>> intellectual horizons" make me sick!!!
>
>
> Not to mention endless screeds like this one:
>
> [chomp more Ranting Rick]
>
> Can someone explain the policy?

Bannings for anything other than out-and-out spam are incredibly rare
(as they should be). The main difference is: Rick posts good content
veiled by poor framing, but jmf posts the same rehashed whining about
the same microbenchmarks, the same unbacked false statements about how
Python is "mathematically incorrect", and absolutely no useful
content. I tend to skim Rick's posts looking for anything that's
actually of interest, but jmf's posts never have anything.

There are a lot of people here who post good content but phrase things
poorly. And everyone has a bad day. (Terry Reedy, I'm hoping this was
just a bad day - there were several rather caustic posts from you.
Sorry to single you out, but I can't think of anyone else recently
who's done that.) So long as there's something useful being said, the
community would be worse off for their removal.

That said, though, I would GREATLY prefer Rick to post less
provocatively. But I'm not calling for his banning any more than I'd
call for Terry's, or my own, for that matter (I've posted plenty of
off-topic or otherwise useless posts).

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


Re: ts.plot() pandas: No plot!

2016-01-31 Thread Paulo da Silva
Às 01:15 de 01-02-2016, Chris Angelico escreveu:
> On Mon, Feb 1, 2016 at 11:46 AM, Paulo da Silva
>  wrote:
...

> 
> Hmm. Normally I would expect matplotlib to pop up a graph there. Are
> you running this from a terminal, or from some sort of GUI? It might
> make a difference to how it brings up the graph. Interestingly, I'm
> not able to run your example on my system:
> 
I'm running it from "konsole", the kde terminal.
The example is from here

http://pandas.pydata.org/pandas-docs/stable/10min.html#plotting


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


Re: psss...I want to move from Perl to Python

2016-01-31 Thread Rustom Mody
On Monday, February 1, 2016 at 8:45:38 AM UTC+5:30, Chris Angelico wrote:
> There are a lot of people here who post good content but phrase things
> poorly. And everyone has a bad day. (Terry Reedy, I'm hoping this was
> just a bad day - there were several rather caustic posts from you.
> Sorry to single you out, but I can't think of anyone else recently
> who's done that.) So long as there's something useful being said, the
> community would be worse off for their removal.

Good -- we're agreed on that much

> 
> That said, though, I would GREATLY prefer Rick to post less
> provocatively. But I'm not calling for his banning any more than I'd
> call for Terry's, or my own, for that matter (I've posted plenty of
> off-topic or otherwise useless posts).

Yes: Just to be clear I am not asking for Rick to be banned though he needs to
be told more often than others to stop assholing.

Likewise jmf: Old-timers out here know anyway to ignore his unicode-complaints. 
 And newcomers --eg this thread -- need to be told anyway.

So why ban?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: ts.plot() pandas: No plot!

2016-01-31 Thread Mark Lawrence

On 01/02/2016 00:46, Paulo da Silva wrote:

Hi!

I am learning pandas and following the tutorial I tried the following:
ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000',
periods=1000))
ts = ts.cumsum()
ts.plot()

No plot is produced!
Only the following output:


Any clue on what is happening?

I'm using kubuntu and python3.

Thanks.
Paulo



Is it as simple as adding a call to ts.show() ?

--
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: psss...I want to move from Perl to Python

2016-01-31 Thread Rustom Mody
On Sunday, January 31, 2016 at 11:15:50 PM UTC+5:30, Paul Rubin wrote:
> Rustom Mody  writes:
> > I would guess it needs more recoding than explicit compilation!
> > Maybe something like http://www.colm.net/open-source/ragel/
> > Unfortunately no python binding so far :-(
> 
> Neat, that looks kind of like lex/flex but with more backends.

Its much more than that. It freely allows mixing of the re and dfa paradigms.
Of course its a classic commonplace in CS that these two are isomorphic.
However structurally they are very different
Of course lex has a state management component as well -- I guess you can think
of ragel to lex in this respect as hll to assembly.

Also I believe its more efficient than lex
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: psss...I want to move from Perl to Python

2016-01-31 Thread MRAB

On 2016-02-01 03:15:10, "Chris Angelico"  wrote:

On Mon, Feb 1, 2016 at 1:59 PM, Rustom Mody  
wrote:

 On Saturday, January 30, 2016 at 3:57:28 PM UTC+5:30, wxjmf wrote:


 Python 3.5.1 is still suffering from the same buggy
 behaviour as in Python 3.0 .


 is banned

 whereas this is not:

 On Saturday, January 30, 2016 at 3:01:09 AM UTC+5:30, Rick Johnson 
wrote:
 On Friday, January 29, 2016 at 6:21:21 AM UTC-6, Ulli Horlacher 
wrote:

 > I nearly gave up with Python at the very beginning before
 > I realised that OO-programming is optional in Python! :-)
 > Most tutorials I found so far makes OO mandatory.

 Just more evidence that old dogs are incapable of learning
 new tricks. Either learn how to wield Neuroplasticity to
 your advantage, or go curl up into a ball and wait for death
 to come. People who are unwilling to "expanding their
 intellectual horizons" make me sick!!!



 Not to mention endless screeds like this one:

 [chomp more Ranting Rick]

 Can someone explain the policy?


Bannings for anything other than out-and-out spam are incredibly rare
(as they should be). The main difference is: Rick posts good content
veiled by poor framing, but jmf posts the same rehashed whining about
the same microbenchmarks, the same unbacked false statements about how
Python is "mathematically incorrect", and absolutely no useful
content. I tend to skim Rick's posts looking for anything that's
actually of interest, but jmf's posts never have anything.

In his defence, he _was_ the one who drew attention to the unexpected 
slowness of the FSR under certain circumstances in the Windows build of 
Python 3.3, albeit in a rather over-dramatised way.


That problem was gone in the next bug-fix release.


There are a lot of people here who post good content but phrase things
poorly. And everyone has a bad day. (Terry Reedy, I'm hoping this was
just a bad day - there were several rather caustic posts from you.
Sorry to single you out, but I can't think of anyone else recently
who's done that.) So long as there's something useful being said, the
community would be worse off for their removal.

That said, though, I would GREATLY prefer Rick to post less
provocatively. But I'm not calling for his banning any more than I'd
call for Terry's, or my own, for that matter (I've posted plenty of
off-topic or otherwise useless posts).



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