Re: How can I debug silent failure - print no output

2016-05-28 Thread Sayth Renshaw
On Saturday, 28 May 2016 16:35:35 UTC+10, Sayth Renshaw  wrote:
> > >
> > >Ok after printing a few things i have found an error.
> > >
> > >def GetArgs():
> > >'''parse XML from command line'''
> > >parser = argparse.ArgumentParser()
> > >
> > >parser.add_argument("path", nargs="+")
> > >parser.add_argument('-e', '--extension', default='',
> > >help='File extension to filter by.')
> > >args = parser.parse_args()
> > >
> > >files = set()
> > >name_pattern = "*" + args.extension
> > >for path in args.path:
> > >files.update(glob.glob(os.path.join(path, name_pattern)))
> > >
> > >print(files)
> > >return files
> > >
> > >a = GetArgs()
> > >print(a)
> > >
> > >so printing the files or the call to the function returns set() not the 
> > >actual files.
> > 
> > Since you're constructing a set of filenames, this means it is probably 
> > returning the right kind of thing, but it is empty. That points to the glob 
> > not 
> > doing what you want or the for-loop not doing anything.
> > 
> > >[sayth@localhost pyXML]$ python3 racemeeting.py data/*.xml
> > >set()
> > >set()
> > >set()
> > 
> > So...  Add more prints!
> > 
> > Specificly, print(args) right after it is set, and put a print() _inside_ 
> > the 
> > loop before the call to files.update, probably printing "path", eg 
> > print("path 
> > =", path).
> > 
> > Then see what you learn.
> > 
> > Cheers,
> > Cameron Simpson 
> 
> Having done extra prints 
> 
> name_pattern = "*" + args.extension
> for path in args.path:
> print(args.path)
> print(path)
> files.update(glob.glob(os.path.join(path, name_pattern)))
> 
> it is getting the path and file however I think it is keeping the directory 
> so i am not getting files.
> 
> [sayth@localhost pyXML]$ python3 racemeeting.py data/*.xml
> ['data/20160528RAND0.xml']
> data/20160528RAND0.xml
> set()
> set()
> ['data/20160528RAND0.xml']
> data/20160528RAND0.xml
> 
> Sayth
Actually think I have found the cause and its really small but on way its 
called.
I was calling
python3 racemeeting.py data/*.xml
which gives the directory and file as the path 
['data/20160528RAND0.xml']

But with arguments separated by a space I actually receive what i thought I 
would get a path and extension such as

sayth@localhost pyXML]$ python3 racemeeting.py data/ *.xml
Namespace(extension='', path=['data/', '*.xml'])
Traceback (most recent call last):
  File "racemeeting.py", line 35, in 

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


Re: IndexError for using pandas dataframe values

2016-05-28 Thread Peter Otten
Peter Otten wrote:

> Daiyue Weng wrote:
> 
>> Hi, I tried to use DataFrame.values to convert a list of columns in a
>> dataframe to a numpy ndarray/matrix,
>> 
>> matrix = df.values[:, list_of_cols]
>> 
>> but got an error,
>> 
>> IndexError: only integers, slices (:), ellipsis (...), numpy.newaxis
>> (None) and integer or boolean arrays are valid indices
>> 
>> so what's the problem with the list of columns I passed in?
>> 
>> many thanks
> 
> Your suggestively named list_of_cols is probably not a list. Have your
> script print its value and type before the failing operation:
> 
>   print(type(list_of_cols), list_of_cols)
>> matrix = df.values[:, list_of_cols]

Am Do Mai 26 2016, 09:21:59 schrieb Daiyue Weng:

[If you had sent this to the list I would have seen it earlier. 
Just in case you didn't solve the problem in the meantime:]

> it prints
> 
>  ['key1', 'key2']

So my initial assumption was wrong -- list_of_cols is a list. However, 
df.values is a numpy array and therefore expects integer indices:

>>> df = pd.DataFrame([[1,2,3],[4,5,6]], columns="key1 key2 key3".split())
>>> df
   key1  key2  key3
0 1 2 3
1 4 5 6

[2 rows x 3 columns]
>>> df.values
array([[1, 2, 3],
   [4, 5, 6]])
>>> df.values[["key1", "key2"]]
Traceback (most recent call last):
  File "", line 1, in 
ValueError: invalid literal for int() with base 10: 'key1'

(I get a different error message, probably because we use different versions 
of numpy)

To fix the problem you can either use integers

>>> df.values[:,[0, 1]]
array([[1, 2],
   [4, 5]])

or select the columns in pandas:

>>> df[["key1", "key2"]].values
array([[1, 2],
   [4, 5]])



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


Re: How can I debug silent failure - print no output

2016-05-28 Thread Sayth Renshaw
So how do i get argparse to read the file arguments correctly?

Looking at the namespace it all gets pushed into path and extension remains 
empty.

[sayth@localhost pyXML]$ python3 racemeeting.py data/ *.xml
Namespace(extension='', path=['data/', '*.xml'])

This is the section I am running

parser = argparse.ArgumentParser()
parser.add_argument("path", nargs="+")
parser.add_argument('-e', '--extension', default='',
help='File extension to filter by.')

args = parser.parse_args()
name_pattern = "*" + args.extension
print(args)

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


Re: Exended ASCII and code pages [was Re: for / while else doesn't make sense]

2016-05-28 Thread Steven D'Aprano
On Sat, 28 May 2016 01:53 am, Rustom Mody wrote:

> On Friday, May 27, 2016 at 7:21:41 PM UTC+5:30, Random832 wrote:
>> On Fri, May 27, 2016, at 05:56, Steven D'Aprano wrote:
>> > On Fri, 27 May 2016 05:04 pm, Marko Rauhamaa wrote:
>> > 
>> > > They are all ASCII derivatives. Those that aren't don't exist.
>> > 
>> > *plonk*
>> 
>> That's a bit harsh, considering that this argument started ...
> 
> Is it now?
> For some reason I am reminded that when I was in junior school and we
> wanted to fight, we said "I am not talking to you!" made a certain gesture
> and smartly marched off.
> 
> I guess the gesture is culture-dependent and in these parts of the world
> it sounds like "*plonk*"

https://en.wikipedia.org/wiki/Plonk_%28Usenet%29



-- 
Steven

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


Re: How can I debug silent failure - print no output

2016-05-28 Thread Sayth Renshaw
On Saturday, 28 May 2016 18:02:06 UTC+10, Sayth Renshaw  wrote:
> So how do i get argparse to read the file arguments correctly?
> 
> Looking at the namespace it all gets pushed into path and extension remains 
> empty.
> 
> [sayth@localhost pyXML]$ python3 racemeeting.py data/ *.xml
> Namespace(extension='', path=['data/', '*.xml'])
> 
> This is the section I am running
> 
> parser = argparse.ArgumentParser()
> parser.add_argument("path", nargs="+")
> parser.add_argument('-e', '--extension', default='',
> help='File extension to filter by.')
> 
> args = parser.parse_args()
> name_pattern = "*" + args.extension
> print(args)
> 
> Sayth

Ah if only i used argparse properly

python racemeeting.py data/ -e *.xml

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


why for loop print only once after add if statement

2016-05-28 Thread jobmattcon
for item, i in enumerate(aa)
  print item

this writing, it can print all values

for item, i in enumerate(aa)
  if item == findit:
print item

this only print the first value, means it only print once then not print again,

where is wrong?

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


Re: How can I debug silent failure - print no output

2016-05-28 Thread Sayth Renshaw
On Saturday, 28 May 2016 19:44:53 UTC+10, Sayth Renshaw  wrote:
> On Saturday, 28 May 2016 18:02:06 UTC+10, Sayth Renshaw  wrote:
> > So how do i get argparse to read the file arguments correctly?
> > 
> > Looking at the namespace it all gets pushed into path and extension remains 
> > empty.
> > 
> > [sayth@localhost pyXML]$ python3 racemeeting.py data/ *.xml
> > Namespace(extension='', path=['data/', '*.xml'])
> > 
> > This is the section I am running
> > 
> > parser = argparse.ArgumentParser()
> > parser.add_argument("path", nargs="+")
> > parser.add_argument('-e', '--extension', default='',
> > help='File extension to filter by.')
> > 
> > args = parser.parse_args()
> > name_pattern = "*" + args.extension
> > print(args)
> > 
> > Sayth
> 
> Ah if only i used argparse properly
> 
> python racemeeting.py data/ -e *.xml
> 
> Sayth

Which means I can rewrite it like this.

parser = argparse.ArgumentParser()
parser.add_argument("path", type=str, nargs="+")
parser.add_argument('-e', '--extension', default='',
help='File extension to filter by.')

args = parser.parse_args()
name_pattern = "*" + args.extension
my_dir = args.path[0]

for dir_path, subdir_list, file_list in os.walk(my_dir):
for name_pattern in file_list:
full_path = os.path.join(dir_path, name_pattern)

Cheers

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


Re: why for loop print only once after add if statement

2016-05-28 Thread Sayth Renshaw
On Saturday, 28 May 2016 20:19:23 UTC+10, meInvent bbird  wrote:
> for item, i in enumerate(aa)
>   print item
> 
> this writing, it can print all values
> 
> for item, i in enumerate(aa)
>   if item == findit:
> print item
> 
> this only print the first value, means it only print once then not print 
> again,
> 
> where is wrong?

Enumerate is there to get rid of the i its not need enumerate is a generator.

list(enumerate(aa, start=1))

https://docs.python.org/3/library/functions.html#enumerate

In [1]: aa = range(10)

In [2]: list(enumerate(aa, start=1))
Out[2]: 
[(1, 0),
 (2, 1),
 (3, 2),
 (4, 3),
 (5, 4),
 (6, 5),
 (7, 6),
 (8, 7),
 (9, 8),
 (10, 9)]

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


Re: why for loop print only once after add if statement

2016-05-28 Thread Peter Otten
jobmatt...@gmail.com wrote:

> for item, i in enumerate(aa)
>   print item
> 
> this writing, it can print all values
> 
> for item, i in enumerate(aa)
>   if item == findit:
> print item
> 
> this only print the first value, means it only print once then not print
> again,

Assuming

aa = ["foo", "bar", "baz"]

and

findit = "bar"

the print statement will only be executed when findit == item.

First iteration of your for loop:

item = "foo"
if "foo" == "bar": # False
   print "foo" # not executed, nothing printed

Second iteration:

item = "bar"
if "bar" == "bar": # True
print "bar"# prints bar

Third iteration:

item = "baz"
if "baz" == "bar": # False
print "baz"# not executed, nothing printed

> where is wrong?

This depends on what you want to achieve. Can you tell us?

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


Re: why for loop print only once after add if statement

2016-05-28 Thread Steven D'Aprano
On Sat, 28 May 2016 08:19 pm, jobmatt...@gmail.com wrote:

> for item, i in enumerate(aa)
>   print item

Wrong way around. It should be:

for i, item in enumerate(aa):
print item

For example:

py> for i, item in enumerate(aa):
... print i, item
...
0 c
1 h
2 e
3 e
4 s
5 e


> this writing, it can print all values
> 
> for item, i in enumerate(aa)
>   if item == findit:
> print item
> 
> this only print the first value, means it only print once then not print
> again,

No, it doesn't not print the first value, it prints any value that equals
findit, whatever that is. If nothing equals findit, nothing will be
printed. Only items which equal findit will be printed.


py> findit = 'z'
py> for i, item in enumerate(aa):
... if item == findit:
... print i, item
...
py> findit = 'e'
py> for i, item in enumerate(aa):
... if item == findit:
... print i, item
...
2 e
3 e
5 e


-- 
Steven

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


Anonymous hyperlinks in restructuredtext

2016-05-28 Thread Sergio Spina
>From the "restructuredtext markup specification":

>Anonymous hyperlink targets consist of an explicit markup
>start (".. "), two underscores, a colon, whitespace, and a
>link block; there is no reference name:
>
>.. __: anonymous-hyperlink-target-link-block
>
>An alternate syntax for anonymous hyperlinks consists of two
>underscores, a space, and a link block:
>
>__ anonymous-hyperlink-target-link-block

Ok. I got the file "test.rst"

==file test.rst==

Nunc ante nulla, porttitor vitae massa eu, mollis tristique
urna. Duis bibendum arcu est, elementum.

.. __: link1

Curabitur sed ultrices ex. Integer.

.. __: link2

Quisque ornare mollis risus eu fringilla. Sed eros mauris,
auctor.

Now I put a reference to link1_ and another to link2_

==end of file=

Let's try to pass it to docutils:

$ rst2latex test.rst > tests.tex
  test.rst:11: (ERROR/3) Anonymous hyperlink mismatch: 0 references
but 2 targets.
  See "backrefs" attribute for IDs.
  test.rst:10: (ERROR/3) Unknown target name: "link1".
  test.rst:10: (ERROR/3) Unknown target name: "link2".

Ok, I retry with the alternate syntax:

==file test.rst==

Nunc ante nulla, porttitor vitae massa eu, mollis tristique
urna. Duis bibendum arcu est, elementum.

__ link1

Curabitur sed ultrices ex. Integer.

__ link2

Quisque ornare mollis risus eu fringilla. Sed eros mauris,
auctor.

Now I put a reference to link1_ and another to link2_

==end of file=

Same error

$ rst2latex test.rst > test.tex
  test.rst:14: (ERROR/3) Anonymous hyperlink mismatch: 0 references
but 2 targets.
  See "backrefs" attribute for IDs.
  test.rst:13: (ERROR/3) Unknown target name: "link1".
  test.rst:13: (ERROR/3) Unknown target name: "link2".

Now, where is the error? How can I correctly set an internal
anonymous kyperlink?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: why for loop print only once after add if statement

2016-05-28 Thread meInvent bbird
thanks, i discover that i misunderstand i and item,

they should be swapped

On Saturday, May 28, 2016 at 6:19:23 PM UTC+8, meInvent bbird wrote:
> for item, i in enumerate(aa)
>   print item
> 
> this writing, it can print all values
> 
> for item, i in enumerate(aa)
>   if item == findit:
> print item
> 
> this only print the first value, means it only print once then not print 
> again,
> 
> where is wrong?

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


how to write code generator for Isabelle by using pygments?

2016-05-28 Thread meInvent bbird
how to write code generator for Isabelle by using pygments?

i am thinking to write a machine learning code to generate code 
by learning example from Isabelle code

however, after google, not much information about this.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: why for loop print only once after add if statement

2016-05-28 Thread meInvent bbird
when read my code again, i discover this error i fixed before

but forget to do for another branch of if statement

now fixed

On Saturday, May 28, 2016 at 6:19:23 PM UTC+8, meInvent bbird wrote:
> for item, i in enumerate(aa)
>   print item
> 
> this writing, it can print all values
> 
> for item, i in enumerate(aa)
>   if item == findit:
> print item
> 
> this only print the first value, means it only print once then not print 
> again,
> 
> where is wrong?

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


Re: Anonymous hyperlinks in restructuredtext

2016-05-28 Thread Ned Batchelder
On Saturday, May 28, 2016 at 8:28:35 AM UTC-4, Sergio Spina wrote:
> From the "restructuredtext markup specification":
> 
> >Anonymous hyperlink targets consist of an explicit markup
> >start (".. "), two underscores, a colon, whitespace, and a
> >link block; there is no reference name:
> >
> >.. __: anonymous-hyperlink-target-link-block
> >
> >An alternate syntax for anonymous hyperlinks consists of two
> >underscores, a space, and a link block:
> >
> >__ anonymous-hyperlink-target-link-block
> 
> Ok. I got the file "test.rst"
> 
> ...
> 
> Now I put a reference to link1_ and another to link2_

The docs say, "there is no reference name", so you cannot put a reference
to them.  You can use an anonymous syntax:

Jump to `my blog`__.

__ http://myblog.com

or you can use a named syntax:

Jump to `my blog`_.

.. _my blog: http://myblog.com

The ReST syntax can be arcane...

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


Re: Anonymous hyperlinks in restructuredtext

2016-05-28 Thread Sergio Spina
Il giorno sabato 28 maggio 2016 14:37:21 UTC+2, Ned Batchelder ha scritto:
> On Saturday, May 28, 2016 at 8:28:35 AM UTC-4, Sergio Spina wrote:
> > From the "restructuredtext markup specification":
> > 
> > >Anonymous hyperlink targets consist of an explicit markup
> > >start (".. "), two underscores, a colon, whitespace, and a
> > >link block; there is no reference name:
> > >
> > >.. __: anonymous-hyperlink-target-link-block
> > >
> > >An alternate syntax for anonymous hyperlinks consists of two
> > >underscores, a space, and a link block:
> > >
> > >__ anonymous-hyperlink-target-link-block
> > 
> > Ok. I got the file "test.rst"
> > 
> > ...
> > 
> > Now I put a reference to link1_ and another to link2_
> 
> The docs say, "there is no reference name", so you cannot put a reference
> to them.  You can use an anonymous syntax:
> 
> Jump to `my blog`__.
> 
> __ http://myblog.com
> 
> or you can use a named syntax:
> 
> Jump to `my blog`_.
> 
> .. _my blog: http://myblog.com
> 
> The ReST syntax can be arcane...
> 
> --Ned.

I need INTERNAL hyperlink targets, not external...
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Anonymous hyperlinks in restructuredtext

2016-05-28 Thread Lele Gaifax
Sergio Spina  writes:

> I need INTERNAL hyperlink targets, not external...

The difference is in how you use the two kinds of references: a named
hyperlink is referenced with the name followed by a single underscore, while
an anonymous one needs *two* underscores. So you have for example::

  Sample document
  ===
  
  See reST_ markup reference or `this section`__ below

  .. _rest:  
http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html#hyperlink-targets
  __ `Explain how it works`_

  Explain how it works
  

  This is an example...

Hope this helps,
ciao, lele.
-- 
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
l...@metapensiero.it  | -- Fortunato Depero, 1929.

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


Re: Anonymous hyperlinks in restructuredtext

2016-05-28 Thread Ned Batchelder
On Saturday, May 28, 2016 at 8:58:41 AM UTC-4, Sergio Spina wrote:
> Il giorno sabato 28 maggio 2016 14:37:21 UTC+2, Ned Batchelder ha scritto:
> > On Saturday, May 28, 2016 at 8:28:35 AM UTC-4, Sergio Spina wrote:
> > > From the "restructuredtext markup specification":
> > > 
> > > >Anonymous hyperlink targets consist of an explicit markup
> > > >start (".. "), two underscores, a colon, whitespace, and a
> > > >link block; there is no reference name:
> > > >
> > > >.. __: anonymous-hyperlink-target-link-block
> > > >
> > > >An alternate syntax for anonymous hyperlinks consists of two
> > > >underscores, a space, and a link block:
> > > >
> > > >__ anonymous-hyperlink-target-link-block
> > > 
> > > Ok. I got the file "test.rst"
> > > 
> > > ...
> > > 
> > > Now I put a reference to link1_ and another to link2_
> > 
> > The docs say, "there is no reference name", so you cannot put a reference
> > to them.  You can use an anonymous syntax:
> > 
> > Jump to `my blog`__.
> > 
> > __ http://myblog.com
> > 
> > or you can use a named syntax:
> > 
> > Jump to `my blog`_.
> > 
> > .. _my blog: http://myblog.com
> > 
> > The ReST syntax can be arcane...
> > 
> > --Ned.
> 
> I need INTERNAL hyperlink targets, not external...

Can explain more about what kind of internal targets you need? Are these
sections with titles already?  Why are you interested in anonymous rather
than named links?

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


Re: Coding systems are political (was Exended ASCII and code pages)

2016-05-28 Thread Ned Batchelder
On Saturday, May 28, 2016 at 11:16:39 AM UTC-4, wxjm...@gmail.com wrote:
> Le samedi 28 mai 2016 06:47:11 UTC+2, Rustom Mody a écrit :
> 
> > ...
> > [which AIUI is jmf's principal error]
> > 
> > ...
> 
> I'm very confident. It's only a question of time until
> the rest of the world dive into this mathematical
> absurdity.
> 
> With your math knowledge, it should not be too
> difficult to show it with a sheet of paper
> and a pencil. Hint: forget "bytes" and think
> "sets" and operators.

Let's please not re-open this debate.
-- 
https://mail.python.org/mailman/listinfo/python-list


Package setup best practice style question

2016-05-28 Thread Gerald Britton
suppose I have a simple python project setup like this:

Project diectory
 prog.py
 pkg directory
  __init__.py
  mod1.py
   class A:

In order to have class A (unqualified) available from prog.py, there are a
few options that I know about.  I'm currently considering two of them and
would like some feedback on best practices.

1. in pkg.__init__.py add:

   from pkg.mod1 import A

in prog.py add:

   from pkg import A

2. leave __init__.py empty
in prog.py add:

 from pkg.mod1 import A


Is there a preference or best practice that would indicate to prefer method
1 or method 2?  Are there methods 3, 4, 5, ... that I should consider that
are even better?
-- 
Gerald Britton, MCSE-DP, MVP
LinkedIn Profile: http://ca.linkedin.com/in/geraldbritton
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Package setup best practice style question

2016-05-28 Thread Steven D'Aprano
On Sun, 29 May 2016 02:15 am, Gerald Britton wrote:

> suppose I have a simple python project setup like this:
> 
> Project diectory
>  prog.py
>  pkg directory
>   __init__.py
>   mod1.py
>class A:

If this is a single project, why do you set it up like this? Is there a
reason why you don't just have:

prog.py  # contains class A

or even:

prog.py
mod1.py  # contains class A


Or if the project is so big it needs to be a package, make it executable:

prog/
__init__.py
mod1.py
__main__.py


This layout is now executable using:

python -m prog

which automatically calls __main__.py.


But if you don't have a good reason for using a package, don't use a
package. Use the simplest project layout that solves your problem.

But for the rest of my comments, I'm going to assume that you DO have a good
reason.



> In order to have class A (unqualified) available from prog.py, there are a
> few options that I know about.  I'm currently considering two of them and
> would like some feedback on best practices.
> 
> 1. in pkg.__init__.py add:
> 
>from pkg.mod1 import A

This kinda-sorta defeats the purpose, or at least one purpose, of using a
package. Which is fine, so long as you understand that's what you are
doing.

Generally speaking, one of the purposes of using a package layout is to
allow unused sub-modules to be left unloaded unless needed. So pkg.mod1
doesn't get loaded unless you need it, in which case you explicitly call 

import pkg.mod1

or 

from pkg.mod1 import A

as you prefer. By putting that import in the pkg __init__ file, you're
making it automatically occur as soon as pkg is imported. Is that what you
intend? If so, then it is fine, and feel free to do it.

But in that case, why not just move class A into pkg/__init__.py?


> in prog.py add:
> 
>from pkg import A

*shrug* This entirely depends on the purpose and use of pkg. When you give
generic, meaningless names, I can only respond with wishy-washy, sit-on-
the-fence generic advice.

If the appropriate API is for callers to say:

from pkg import A

then this is the right way to design your package. But if the right API is:

from pkg.mod1 import A

then don't do it this way, instead do it as follows.


> 2. leave __init__.py empty
> in prog.py add:
> 
>  from pkg.mod1 import A
> 
> 
> Is there a preference or best practice that would indicate to prefer
> method
> 1 or method 2?  Are there methods 3, 4, 5, ... that I should consider that
> are even better?

One purpose of using a package is to have alternative implementations. For
instance I have a package which has to support Python 2.4 where the "with"
statement is not available, so I design it like this:

package/
__init__.py
module.py
module24.py

Since the user doesn't choose which implementation to use, I have this in
the init file:


try:
from module import thing
except SyntaxError:
from module24 import thing


and then the caller uses:

from package import thing

and is none the wiser.

On the other hand, if the user was supposed to choose, then I would have
then call:

from package.redblack import Tree
from package.avl import Tree
from package.scapegoat import Tree

and let the user choose which implementation they wanted.


-- 
Steven

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


Re: Coding systems are political (was Exended ASCII and code pages)

2016-05-28 Thread Random832
On Sat, May 28, 2016, at 00:46, Rustom Mody wrote:
> Which also means that if the Chinese were to have more say in the
> design of Unicode/ UTF-8 they would likely not waste swathes of prime
> real-estate for almost never used control characters just in the name
> of ASCII compliance

There are only 128 code points in the single-byte range of UTF-8. Only
32 of which are used for, almost-never-used or otherwise, control
characters. What do you imagine they would have put there instead?

At least Unicode doesn't do as badly as the first-draft ISO-UCS, which
didn't allow a C0/C1 control value in *any* position in UCS-2 or UCS-4,
therefore UCS-2 would encode only 192*192=36,864 codepoints as two bytes
(and 64 control characters as one byte), as opposed to UTF-16's 63,488
(including all control characters) two-byte characters.

For completeness, I'll note that conventional East Asian character
coding systems do have a higher information density compared to UTF-8,
but at a cost of not being self-synchronizing. And their single-byte
characters are in fact ASCII and C0/C1 controls, with only Japanese
Shift-
JIS encodings additionally having Katakana as single-byte characters.
-- 
https://mail.python.org/mailman/listinfo/python-list


re.search - Pattern matching review

2016-05-28 Thread Ganesh Pal
Dear Python friends,

I am  on Python 2.7 and Linux . I am trying to extract the address
"1,5,147456:8192" from the below stdout using re.search

(Pdb) stdout
'linux-host-machine-1: Block Address for 1,5,27320320:8192 (block
1,5,147456:8192) --\nlinux-host-machine-1: magic
0xdeaff2fe mark_cookie 0x300a\n'
(Pdb) type(stdout)


Here is the code I have come up with, this looks buggy please review
the same and suggest any better ways  to code the same

the same may be splitlines() , re.complie() etc , my intention is to
just match 1,5,147456:8192 and return the same.


#Sample code

import re
import subprocess_run

def get_block():
try:
cmd = "get_block_info -l"
# stdout is the output retrieved by subprocess.Popen()
stdout, stderr, exitcode = subprocess_run(cmd)
search_pat = 'Block Address.* \(block (\d+),(\d+),(\d+):(\d+)'
matched = re.search(search_pat, stdout)
block = (int(matched.group(1)),
   int(matched.group(2)),
   int(matched.group(3)),
   int(matched.group(4)),
  )
except IOError, e:
logging.warning('Error reading lines from "%s" (%s).'
% (cmd, e))

if block is None:
   logging.error("block not found")
   return False
logging.info("block not found")
return block

Regards,

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


re.search - Pattern matching review ( Apologies re sending)

2016-05-28 Thread Ganesh Pal
Dear Python friends,

I am  on Python 2.7 and Linux . I am trying to extract the address
"1,5,147456:8192" from the below stdout using re.search

(Pdb) stdout
'linux-host-machine-1: Block Address for 1,5,27320320:8192 (block
1,5,147456:8192) --\nlinux-host-machine-1: magic
0xdeaff2fe mark_cookie 0x300a\n'
(Pdb) type(stdout)


Here is the code I have come up with, this looks buggy please review
the same and suggest any better ways  to code.

Could we use splitlines() or re.complie() etc , my intention is to
match 1,5,147456:8192 and return the same.


#Sample code

import re
import subprocess_run

def get_block():
try:
cmd = "get_block_info -l"
# stdout is the output retrieved by subprocess.Popen()
stdout, stderr, exitcode = subprocess_run(cmd)
search_pat = 'Block Address.* \(block (\d+),(\d+),(\d+):(\d+)'
matched = re.search(search_pat, stdout)
block = (int(matched.group(1)),
   int(matched.group(2)),
   int(matched.group(3)),
   int(matched.group(4)),
  )
except IOError, e:
logging.warning('Error reading lines from "%s" (%s).'
% (cmd, e))

if block is None:
   logging.error("block not found")
   return False
logging.info("block not found")
return block

Regards,

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


RE: re.search - Pattern matching review ( Apologies re sending)

2016-05-28 Thread Albert-Jan Roskam
> Date: Sat, 28 May 2016 23:48:16 +0530
> Subject: re.search - Pattern matching review ( Apologies re sending)
> From: ganesh1...@gmail.com
> To: python-list@python.org
> 
> Dear Python friends,
> 
> I am  on Python 2.7 and Linux . I am trying to extract the address
> "1,5,147456:8192" from the below stdout using re.search
> 
> (Pdb) stdout
> 'linux-host-machine-1: Block Address for 1,5,27320320:8192 (block
> 1,5,147456:8192) --\nlinux-host-machine-1: magic
> 0xdeaff2fe mark_cookie 0x300a\n'
> (Pdb) type(stdout)
> 
> 
> Here is the code I have come up with, this looks buggy please review
> the same and suggest any better ways  to code.
> 
> Could we use splitlines() or re.complie() etc , my intention is to
> match 1,5,147456:8192 and return the same.
> 
> 
> #Sample code
> 
> import re
> import subprocess_run
> 
> def get_block():
> try:
> cmd = "get_block_info -l"
> # stdout is the output retrieved by subprocess.Popen()
> stdout, stderr, exitcode = subprocess_run(cmd)
> search_pat = 'Block Address.* \(block (\d+),(\d+),(\d+):(\d+)'
> matched = re.search(search_pat, stdout)
> block = (int(matched.group(1)),
>int(matched.group(2)),
>int(matched.group(3)),
>int(matched.group(4)),
>   )
Perhaps:map(int,  re.search(search_pat, stdout).groups())
Or re.findall
> except IOError, e:
> logging.warning('Error reading lines from "%s" (%s).'
> % (cmd, e))
> 
> if block is None:
>logging.error("block not found")
>return False
> logging.info("block not found")
> return block
> 
> Regards,
> 
> Ganesh
> -- 
> https://mail.python.org/mailman/listinfo/python-list
  
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How can I debug silent failure - print no output

2016-05-28 Thread cs

On 28May2016 03:32, Sayth Renshaw  wrote:

On Saturday, 28 May 2016 19:44:53 UTC+10, Sayth Renshaw  wrote:

On Saturday, 28 May 2016 18:02:06 UTC+10, Sayth Renshaw  wrote:
> So how do i get argparse to read the file arguments correctly?
>
> Looking at the namespace it all gets pushed into path and extension remains 
empty.
>
> [sayth@localhost pyXML]$ python3 racemeeting.py data/ *.xml
> Namespace(extension='', path=['data/', '*.xml'])
> This is the section I am running
>
> parser = argparse.ArgumentParser()
> parser.add_argument("path", nargs="+")
> parser.add_argument('-e', '--extension', default='',
> help='File extension to filter by.')
>
> args = parser.parse_args()
> name_pattern = "*" + args.extension
> print(args)

Ah if only i used argparse properly

python racemeeting.py data/ -e *.xml


There are a couple of things here.

First is that a normal UNIX command puts all the options first, so you should 
be desiging your command line to run like this:


 python racemeeting.py -e *.xml data

or perhaps:

 python racemeeting.py -d data -e *.xml

It is traditional to stop parsing options such as -e when you reach the first 
non-option, because that lets one put whatever is necessary safely _after_ the 
options without fear that one of the arguments will resemble an option. For 
example, suppoing you have a file with the name "-e" and said:


 somecommand -f foo dir *

intending to use all the local filenames after "dir". In your current scheme 
(accepting options after "dir") a "-e" appearing later would be misinterpreted.


The second is to be aware that the shell expands globs _before_ invoking the 
command. This is extremely useful because it means that (a) commands usually 
don't need to do their own glob expansion and (b) all commands end up using the 
same glob syntax because it is common to the shell, not a command-specific 
special syntax. Which makes everything easier to use.


The upshot of that is that you should _either_ be quoting "*.xml" on your 
command line to prevent expansion, _or_ you should not be bothering with he 
asterisk, instead passing the argument ".xml" or even just "xml".


As an experiment, make two dummy XML files in your current directory (where 
your script is):


 >>dummy1.xml
 >>dummy2.xml

and run your script passing *.xml as you currently do, and see what your print 
statements say. This should make the effect obvious.


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


Re: Python-list Digest, Vol 152, Issue 43

2016-05-28 Thread Noah Fleiszig
Thank you and ok

-- 
Sent from Gmail Mobile
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Package setup best practice style question

2016-05-28 Thread Steven D'Aprano
On Sun, 29 May 2016 03:00 am, Steven D'Aprano wrote:

> On Sun, 29 May 2016 02:15 am, Gerald Britton wrote:
> 
>> suppose I have a simple python project setup like this:
[...]

To which I responded:

> If this is a single project, why do you set it up like this? Is there a
> reason why you don't just have:
[...]


I'm sorry, on re-reading my post I realise that it might come across as a
bit confrontational. Sorry Gerald, it wasn't intended that way. I was just
intending to point out that we aren't forced to use a package structure at
all, unless it brings actual advantages to the project.



-- 
Steven

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


Re: Coding systems are political (was Exended ASCII and code pages)

2016-05-28 Thread Steven D'Aprano
On Sat, 28 May 2016 02:46 pm, Rustom Mody wrote:

[...]
> In idealized, simplified models like Turing models where
> 3 is 111
> 7 is 111
> 100, 8364 etc I wont try to write but you get the idea!
> its quite clear that bigger numbers cost more than smaller ones

I'm not sure that a tally (base-1, unary) is a good model for memory usage
in any computing system available today. And I thought that the Turing
model was based on binary: the machine could both mark a cell and erase the
mark, which corresponds to a bit.



> With current hardware it would seem to be a flat characteristic for
> everything < 2³² (or even 2⁶⁴)
> 
> But thats only an optical illusion because after that the characteristic
> will rise jaggedly, slowly but monotonically, typically log-linearly
> [which AIUI is jmf's principal error]

Can you be more specific at what you are trying to say? You seem to think
that you're saying something profound here, but I don't know what it is.



> Which also means that if the Chinese were to have more say in the design
> of Unicode/ UTF-8 they would likely not waste swathes of prime real-estate
> for almost never used control characters just in the name of ASCII
> compliance

There is this meme going around that Unicode is a Western imperialistic
conspiracy against Asians. For example, there was a blog post a year or so
ago by somebody bitterly complaining that he could draw a pile of poo in
Unicode but not write his own name, blaming Westerners for this horrible
state of affairs.

But like most outrage on the Internet, his complaint was nonsense. He *can*
write his name -- he just has to use a combining character to add an
accent(?) to a base character. (Or possibly a better analogy is that of a
ligature.) His complaint came down to the fact that because his name
included a character which was unusual even in his own language (Bengali),
he had to use two Unicode code points rather than one to represent it. This
is, of course, the second worst[1] kind of discrimination.

https://news.ycombinator.com/item?id=9219162

Likewise the hoo-har over CJK unification. Some people believe that this is
the evil Western imperialists forcing their ignorant views on the Chinese,
Japanese and Koreans, but the reality is that the Unicode Consortium merely
follows the decisions made by the Ideographic Rapporteur Group (IRG),
originally the CJK-JRG group. That is a multinational group set up by the
Chinese and Japanese, now including other East Asians (both Koreas,
Singapore, Vietnam) to decide on a common set of Han characters.

Anyway, I digress.

Given that there are tens of thousands of Han characters (with unification),
more than will fit in 16 bits, the 64 control characters in Unicode is not
going to make any practical difference. In some hypothetical world where
Han speakers got to claim code points U+-001F and U+0080-009F for
ideographs, pushing the control characters out into the astral planes, all
they would gain is *sixty four* code points. They would still need multiple
thousands of astral characters.

Besides, some level of ASCII compatibility is useful even for Han speakers.
Their own native-designed standard encodings like Big5 and Shift-JIS (which
predate Unicode) keep byte-compatibility with the 32 ASCII control
characters. (I'm not sure about the 32 "C1" control characters.) Since the
Chinese and Japanese national standards pre-dating Unicode choose to keep
compatibility with the ASCII control characters, I don't think that there
is any good reason to think they would have made a different decision when
it came to Unicode had they had more of a say than they already did.

Which was, and still is, considerable. Both China and Japan are very
influential in the Unicode Consortium, driving the addition of many new Han
characters and emoji. The idea that a bunch of Western corporations and
academics are pushing them around is laughable.




[1] The worst being that my US English keyboard doesn't have a proper curly
apostrophe, forcing me to use a straight ' mark in my name like some sort
of animal.

-- 
Steven

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


Re: Coding systems are political (was Exended ASCII and code pages)

2016-05-28 Thread Rustom Mody
On Sunday, May 29, 2016 at 11:07:51 AM UTC+5:30, Steven D'Aprano wrote:
> On Sat, 28 May 2016 02:46 pm, Rustom Mody wrote:
> 
> [...]
> > In idealized, simplified models like Turing models where
> > 3 is 111
> > 7 is 111
> > 100, 8364 etc I wont try to write but you get the idea!
> > its quite clear that bigger numbers cost more than smaller ones
> 
> I'm not sure that a tally (base-1, unary) is a good model for memory usage
> in any computing system available today. And I thought that the Turing
> model was based on binary: the machine could both mark a cell and erase the
> mark, which corresponds to a bit.

Well you can take your pick
See unary here http://jeapostrophe.github.io/2013-10-29-tmadd-post.html

> 
> 
> 
> > With current hardware it would seem to be a flat characteristic for
> > everything < 2³² (or even 2⁶⁴)
> > 
> > But thats only an optical illusion because after that the characteristic
> > will rise jaggedly, slowly but monotonically, typically log-linearly
> > [which AIUI is jmf's principal error]
> 
> Can you be more specific at what you are trying to say? You seem to think
> that you're saying something profound here, but I don't know what it is.

I think that you seem to think that you know what I seem to think... but I 
digress.

Big numbers are big ie expensive
Small numbers are cheap
Easy so far??

Then there is technology... making arbitrary decisions eg a word is 32 bits
This just muddies the discussion but does not change the speed of light -- aka
properties of the universe are invariant in the face of committee decisions
-- even international consortiums

So it SEEMS (to ppl like jmf) that a million is no more costly than ten

However consider an 8 bit machine (eg 8088)
the natural size 
- for fitting 25 is a byte
- for 1000 is 2 bytes
- for a million is 3 or 4 bytes depending on what we mean by 'natural'

In short that a € costs more than a $ is a combination of the factors
- a natural cause -- there are a million chars to encode (lets assume that the
million of Unicode is somehow God-given AS A SET)
- an artificial political one -- out of the million-factorial permutations of 
that million, the one that the Unicode consortium chose is towards satisfying 
the
equation: Keep ASCII users undisturbed and happy
> 
> 
> 
> > Which also means that if the Chinese were to have more say in the design
> > of Unicode/ UTF-8 they would likely not waste swathes of prime real-estate
> > for almost never used control characters just in the name of ASCII
> > compliance
> 
> There is this meme going around that Unicode is a Western imperialistic
> conspiracy against Asians. For example, there was a blog post a year or so
> ago by somebody bitterly complaining that he could draw a pile of poo in
> Unicode but not write his own name, blaming Westerners for this horrible
> state of affairs.
> 
> But like most outrage on the Internet, his complaint was nonsense. He *can*
> write his name -- he just has to use a combining character to add an
> accent(?) to a base character. (Or possibly a better analogy is that of a
> ligature.) His complaint came down to the fact that because his name
> included a character which was unusual even in his own language (Bengali),
> he had to use two Unicode code points rather than one to represent it. This
> is, of course, the second worst[1] kind of discrimination.
> 
> https://news.ycombinator.com/item?id=9219162
> 
> Likewise the hoo-har over CJK unification. Some people believe that this is
> the evil Western imperialists forcing their ignorant views on the Chinese,
> Japanese and Koreans, but the reality is that the Unicode Consortium merely
> follows the decisions made by the Ideographic Rapporteur Group (IRG),
> originally the CJK-JRG group. That is a multinational group set up by the
> Chinese and Japanese, now including other East Asians (both Koreas,
> Singapore, Vietnam) to decide on a common set of Han characters.
> 
> Anyway, I digress.
> 
> Given that there are tens of thousands of Han characters (with unification),
> more than will fit in 16 bits, the 64 control characters in Unicode is not
> going to make any practical difference. In some hypothetical world where
> Han speakers got to claim code points U+-001F and U+0080-009F for
> ideographs, pushing the control characters out into the astral planes, all
> they would gain is *sixty four* code points. They would still need multiple
> thousands of astral characters.
> 
> Besides, some level of ASCII compatibility is useful even for Han speakers.
> Their own native-designed standard encodings like Big5 and Shift-JIS (which
> predate Unicode) keep byte-compatibility with the 32 ASCII control
> characters. (I'm not sure about the 32 "C1" control characters.) Since the
> Chinese and Japanese national standards pre-dating Unicode choose to keep
> compatibility with the ASCII control characters, I don't think that there
> is any good reason to think they would have made a different decision when
> it 

Re: Coding systems are political (was Exended ASCII and code pages)

2016-05-28 Thread alister
On Sun, 29 May 2016 15:37:35 +1000, Steven D'Aprano wrote:

> 
> [1] The worst being that my US English keyboard doesn't have a proper
> curly apostrophe, forcing me to use a straight ' mark in my name like
> some sort of animal.

What do you expect after all US is standard engineering speak for Un-
Serviceable ;-)





-- 
The other line moves faster.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: re.search - Pattern matching review ( Apologies re sending)

2016-05-28 Thread Ganesh Pal
> Perhaps:
> map(int,  re.search(search_pat, stdout).groups())
>
>
>
Thanks Albert map saved me many lines of code  but map returns a list I
will have to convert  the list to string again
Below is how Iam planning to teh conversion
>>> block = map(int,  re.search(search_pat, stdout).groups())
>>> print block
['1,2:122']
>>> s1 = ','.join(str(n) for n in block)
>>> print s1
1,2:122
>>> str(s1)
'1,2:122'

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