Re: Enumerating all 3-tuples

2018-03-21 Thread Robin Becker

On 21/03/2018 05:14, Steven D'Aprano wrote:

On Tue, 13 Mar 2018 23:56:37 +0100, Denis Kasak wrote:

[...]

The triples can be viewed as a pair of a pair and a natural number:

(1,1),1 (1,1),2 (1,1),3 ...
(2,1),1 (2,1),2 (2,1),3 ...
(1,2),1 (1,2),2 (1,2),3 ...


[...]

This leads fairly naturally to the implementation.

  from itertools import accumulate, count

  def c(i):
  """

...

That looks interesting, I haven't had a chance to play with it in a lot
of detail yet, but it looks to me like every time you generate a triple,
it starts counting up from 1.

So iterating over c3(1), c3(2), c3(4), ... c3(something big) is going to
have O(N**2) performance. It's like the old joke about the Polish painter:


This cantor inverse is much faster; it uses the integer sqrt from here

http://code.activestate.com/recipes/577821-integer-square-root-function/

so far as I can tell it works for the cantor below

def cantor(k1,k2):
return (((k1+k2)*(k1+k2+1))>>1) + k2

def isqrt(x):
if x < 0:
raise ValueError('square root not defined for negative numbers')
n = int(x)
if n == 0:
return 0
a, b = divmod(n.bit_length(), 2)
x = 2**(a+b)
while True:
y = (x + n//x)//2
if y >= x:
return x
x = y

def inverse_cantor(z):
w = int((isqrt(8*z+1)-1)//2)
t = (w*(w+1))>>1
j = z - t
i = w - j
return i, j

but it doesn't agree with Denis' inverse I guess because this is defined on non-negative integers, but his functions are defined 
on positive integers.



http://global.joelonsoftware.com/English/Articles/BacktoBasics.html

Since that's exactly what I need to do, that might be a problem.

On the other hand, I doubt I'll need to generate more than a few thousand
of these, so it might be fast enough. I guess I have to run some
benchmarks to find out.

But however they turn out, I appreciate your time and code. Thanks heaps!






--
Robin Becker

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


Want to convert the msg file in html file so that i can read the tables emebedded in msg file using python

2018-03-21 Thread gurpreetsinghluky
Please help me
-- 
https://mail.python.org/mailman/listinfo/python-list


lire du son en format natif avec python

2018-03-21 Thread asphjt--- via Python-list
Bonjour à tous,
En utilisant le module pyaudio pour enregistrer du son, j'ai une chaine de 
caractères de la forme 
b'\x01\x00\n\x00\x04\x00\xfe\xff\x04\x00\x0b\x00\n\x00\x07\x00'b'\x01\x00\xff\xff\x00\x00\xff\xff\x01\x00\n\x00\n\x00\n\x00
 qui correspond aux valeurs hexadécimales (je pense) du son brut, et j'aimerai 
pouvoir le lire sans passer par un encodage ou format de compression comme mp3, 
wav,...
J'ai lu que l'on pouvait le faire avec ossaudiodev, mais je n'arrive pas à le 
trouver, ni à l'installer depuis pyzo.
Si quelqu'un peut m'aider, merci à lui d'avance.
-- 
https://mail.python.org/mailman/listinfo/python-list


Reg python regexp

2018-03-21 Thread sankarramanv
Hi,

I have a requirement.

cmd="cat |grep -c 'if [ -t 1 ]; then mesg n 2>/dev/null; fi'"

I need to escape only the square brackets in above variable since its not 
grepping without escaping the brackets.

Please help.

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


Re: Reg python regexp

2018-03-21 Thread Paul Moore
Hi,
You don't need a regexp for this, the "replace" method on a string
will do what you want:

>>> s = 'this is a [string'
>>> print(s.replace('[', '\\['))
this is a \[string

Paul


On 21 March 2018 at 10:44,   wrote:
> Hi,
>
> I have a requirement.
>
> cmd="cat |grep -c 'if [ -t 1 ]; then mesg n 2>/dev/null; fi'"
>
> I need to escape only the square brackets in above variable since its not 
> grepping without escaping the brackets.
>
> Please help.
>
> Thanks.
> --
> https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Reg python regexp

2018-03-21 Thread Chris Angelico
On Wed, Mar 21, 2018 at 9:44 PM,   wrote:
> Hi,
>
> I have a requirement.
>
> cmd="cat |grep -c 'if [ -t 1 ]; then mesg n 2>/dev/null; fi'"
>
> I need to escape only the square brackets in above variable since its not 
> grepping without escaping the brackets.
>
> Please help.

You're putting this into a Python script. Why not use Python to search
the file instead of grep? That'd also eliminate the superfluous "cat
file |" at the start.

Python is not a shell language. You don't have to, and shouldn't,
write everything by invoking other programs.

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


Re: Want to convert the msg file in html file so that i can read the tables emebedded in msg file using python

2018-03-21 Thread Steven D'Aprano
On Wed, 21 Mar 2018 03:15:30 -0700, gurpreetsinghluky wrote:

> Please help me

We'd love to help if only we knew what you wanted.

Can you explain what you want? Give an example of the data you are 
working with and the results you expect?


-- 
Steve

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


Re: Reg python regexp

2018-03-21 Thread Rhodri James

On 21/03/18 10:44, [email protected] wrote:

Hi,

I have a requirement.

cmd="cat |grep -c 'if [ -t 1 ]; then mesg n 2>/dev/null; fi'"

I need to escape only the square brackets in above variable since its not 
grepping without escaping the brackets.


You need to escape the square brackets as you normally would for your 
shell, with backslashes I presume.  Then you need to escape the 
backslashes so they aren't interpreted specially by Python, with more 
backslashes.


--
Rhodri James *-* Kynesim Ltd
--
https://mail.python.org/mailman/listinfo/python-list


Re: lire du son en format natif avec python

2018-03-21 Thread Thomas Jollans
On 2018-03-21 11:25, asphjt--- via Python-list wrote:
> Bonjour à tous,
> En utilisant le module pyaudio pour enregistrer du son, j'ai une chaine de 
> caractères de la forme 
> b'\x01\x00\n\x00\x04\x00\xfe\xff\x04\x00\x0b\x00\n\x00\x07\x00'b'\x01\x00\xff\xff\x00\x00\xff\xff\x01\x00\n\x00\n\x00\n\x00
>  qui correspond aux valeurs hexadécimales (je pense) du son brut, et 
> j'aimerai pouvoir le lire sans passer par un encodage ou format de 
> compression comme mp3, wav,...
> J'ai lu que l'on pouvait le faire avec ossaudiodev, mais je n'arrive pas à le 
> trouver, ni à l'installer depuis pyzo.
> Si quelqu'un peut m'aider, merci à lui d'avance.
> 

Bonjour,

dans cette groupe nous ne parlons qu'anglais. Il y a des forums Pythons
francophones [1], par exemples les forums des l'association francophone
Python (afpy) [2]. Ici, écrivez en anglais la prochaine fois s.v.p.

[1] https://wiki.python.org/moin/FrenchLanguage
[2] https://www.afpy.org/forums

Au sujet de votre question: vous pouvez aussi lire et passer ce que vous
avez enregistré avec PyAudio. Je pense qu'il faut le passer directement
à la méthode write() du 'stream' PyAudio.


-- Thomas


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


Re: Multiprocessing on a remote host

2018-03-21 Thread Larry Martell
On Tue, Mar 20, 2018 at 11:15 PM, Steven D'Aprano
 wrote:
> On Wed, 21 Mar 2018 02:20:16 +, Larry Martell wrote:
>
>> Is there a way to use the multiprocessing lib to run a job on a remote
>> host?
>
> Don't try to re-invent the wheel. This is a solved problem.
>
> https://stackoverflow.com/questions/1879971/what-is-the-current-choice-for-doing-rpc-in-python
>
> I've used both rpyc and pyro, they work fine and are easy to learn.

Yeah, I saw that and I wasn't trying to reinvent the wheel. On this
page https://docs.python.org/2/library/multiprocessing.html it says
this:

The multiprocessing package offers both local and remote concurrency,
effectively side-stepping the Global Interpreter Lock by using
subprocesses instead of threads. Due to this, the multiprocessing
module allows the programmer to fully leverage multiple processors on
a given machine.

I took 'remote concurrency' to mean I could use it run a process on
another host. But I don't see how to do that, and I was asking if it
was possible or am I misinterpreting the docs.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how do I retry a command only for a specific exception / error

2018-03-21 Thread Ganesh Pal
Please ensure quoted text is quoted, and new text you write is unquoted.
> That way you are more likely to get useful
>

Sorry , Steve I didn't realize but thanks for pointing out I will take care
I was on a mobile phone and messed the quoted text

>Something like this should do it. It gives up immediately on fatal >errors
>and tries again on temporary ones. (You have to specify what you >consider
>fatal or temporary, of course.

This is a good suggestion ,  I like the way the code is  written , but what
I have failed to understand is how to translate the
possible  TemporaryFailureErrors to a different exception class/type  and
retry .


 In my case ,  every  command is executed using a run() function  that
calls out to subprocess.Popen().   Which will return  stdout, stderr,
exit_code and we would need to retry only for a specific
TemporaryFailureError .



Example : Say , If we are not able to  SSH  to the host  , and I get
“connection refused” error I would want to retry only for this specific case
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Reg python regexp

2018-03-21 Thread Youta TAKAOKA
sankarramanv,

It seems for me that this task does not need both python AND shell. Only
python does it, as well as only shell.

Of course, there can be some restrictions let you use both. (the real world
is filled up with such troublesome matters !)
If you *really* need to use `lgrep`, try `-f` option.
`lgrep -f` uses pattern as just a fixed text, not regexp.

2018年3月21日(水) 20:35 Rhodri James :

> On 21/03/18 10:44, [email protected] wrote:
> > Hi,
> >
> > I have a requirement.
> >
> > cmd="cat |grep -c 'if [ -t 1 ]; then mesg n 2>/dev/null; fi'"
> >
> > I need to escape only the square brackets in above variable since its
> not grepping without escaping the brackets.
>
> You need to escape the square brackets as you normally would for your
> shell, with backslashes I presume.  Then you need to escape the
> backslashes so they aren't interpreted specially by Python, with more
> backslashes.
>
> --
> Rhodri James *-* Kynesim Ltd
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: lire du son en format natif avec python

2018-03-21 Thread Vincent Vande Vyvre

Le 21/03/18 à 11:25, asphjt--- via Python-list a écrit :

Bonjour à tous,
En utilisant le module pyaudio pour enregistrer du son, j'ai une chaine de 
caractères de la forme 
b'\x01\x00\n\x00\x04\x00\xfe\xff\x04\x00\x0b\x00\n\x00\x07\x00'b'\x01\x00\xff\xff\x00\x00\xff\xff\x01\x00\n\x00\n\x00\n\x00
 qui correspond aux valeurs hexadécimales (je pense) du son brut, et j'aimerai 
pouvoir le lire sans passer par un encodage ou format de compression comme mp3, 
wav,...
J'ai lu que l'on pouvait le faire avec ossaudiodev, mais je n'arrive pas à le 
trouver, ni à l'installer depuis pyzo.
Si quelqu'un peut m'aider, merci à lui d'avance.


Hi,


This is an English mailing list.


If you prefer talk in French, I recommend you this forum:

https://www.developpez.net/forums/f96/autres-langages/python-zope/


Best


Vincent

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


PyDev 6.3.2 released

2018-03-21 Thread Fabio Zadrozny
 PyDev 6.3.2 Release Highlights

PyDev changes:

   -

   Type inference
   - PyDev can now uses information on .pyi files (when along the typed .py
  file) for type inference.
   -

   Fixed issue opening code completion preferences page.

About PyDev

PyDev is an open-source Python IDE on top of Eclipse for Python, Jython and
IronPython development, now also available for Python on Visual Studio Code.

It comes with goodies such as code completion, syntax highlighting, syntax
analysis, code analysis, refactor, debug, interactive console, etc.

It is also available as a standalone through LiClipse with goodies such as
multiple cursors, theming and support for many other languages, such as
Django Templates, Jinja2, Html, JavaScript, etc.

Links:

PyDev: http://pydev.org
PyDev Blog: http://pydev.blogspot.com
PyDev on VSCode: http://pydev.org/vscode
LiClipse: http://www.liclipse.com
PyVmMonitor - Python Profiler: http://www.pyvmmonitor.com/

Cheers,

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


Modules

2018-03-21 Thread Jacques Bikoundou

Hi, I am writing because I use a Python 3 kernel on my notebook that I access 
through the Microsoft Azure ML. I experience error messages when using modules 
such as 'speedml' or 'xgboost'. The error messagessay: no such module. How can 
I solve this?Thanks, Jacques 

Sent from Samsung tablet
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Modules

2018-03-21 Thread Rick Johnson
Hmm, let's try a little interactive session, shall we? Did
your error message look something like this?

>>> import spam

Traceback (most recent call last):
  File "", line 1, in 
import spam
ImportError: No module named spam

>>> import eggs

Traceback (most recent call last):
  File "", line 1, in 
import eggs
ImportError: No module named eggs

>>> from green.eggs import ham

Traceback (most recent call last):
  File "", line 1, in 
from green.eggs import ham
ImportError: No module named green.eggs

Hmm. It seems that python cannot find a module, then there
is no way to import it. What to do? Ah ha! We check the
search path!

>>> import sys
>>> sys.path
[...List of directories...]

Q: Are your modules inside any of the listed directories?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Want to convert the msg file in html file so that i can read the tables emebedded in msg file using python

2018-03-21 Thread Rick Johnson
On Wednesday, March 21, 2018 at 5:15:47 AM UTC-5, [email protected] wrote:
> TITLE: "Want to convert the msg file in html file so that i
> can read the tables emebedded in msg file using python"

My guess that the OP meant to say: "msg file *INTO* html
file" -- where "msg file" is a file holding email or text
message data in MBOX format, and the OP wants to display
this data using HTML table[s]. But that's only a guess. 

*scratches head*

> Please help me

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


Re: Modules

2018-03-21 Thread Jacques Bikoundou
It said: ImportError: no module named 'speedml'

On Wed, Mar 21, 2018, 19:57 Rick Johnson 
wrote:

> Hmm, let's try a little interactive session, shall we? Did
> your error message look something like this?
>
> >>> import spam
>
> Traceback (most recent call last):
>   File "", line 1, in 
> import spam
> ImportError: No module named spam
>
> >>> import eggs
>
> Traceback (most recent call last):
>   File "", line 1, in 
> import eggs
> ImportError: No module named eggs
>
> >>> from green.eggs import ham
>
> Traceback (most recent call last):
>   File "", line 1, in 
> from green.eggs import ham
> ImportError: No module named green.eggs
>
> Hmm. It seems that python cannot find a module, then there
> is no way to import it. What to do? Ah ha! We check the
> search path!
>
> >>> import sys
> >>> sys.path
> [...List of directories...]
>
> Q: Are your modules inside any of the listed directories?
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Modules

2018-03-21 Thread Rick Johnson
On Wednesday, March 21, 2018 at 7:49:42 PM UTC-5, Jacques Bikoundou wrote:
> It said: ImportError: no module named 'speedml'

I see. And did you check the search path[1] to ensure that
the modules you want to import are indeed located in a
directory which python normally searches?

As an academic excercise, and to better understand the the
Python import mechanism, let us imagine that Python is house
keeping robot. And we shall dub her "Rosie5000".

And you, being the master programmer of "Rosie5000, the
housekeeping robot", you have (wisely) programed Rosie to
search in specific locations of your home for items that you
request (aka: "import"). And being that you knew ahead of
time that you would be asking for food and drinks on a
regular basis, the first place you programmed Rosie to
search is in the kitchen (aka: "stdlib"). But then one day,
an old college roomie drops by for an unexpected visit, and
of course, his car is running low on oil (big surprise,
right?). So you ask Rosie to fetch a can of oil: "import
motoroil" you say, and immediately -- being that Rosie is a
mindless bucket of bolts; and being that she has only been
programmed to search in the kitchen; and simultaneously,
being, that she is oblivious to the fact that kitchens are
hardly ever the proper storage location for cans of nasty
motor oil -- Rosie goes rummaging through the cupboards,
systematically searching for a can of "dead-dino-goo", only to
come back empty handed ("Bad Rosie!" you say, "Bad!"). But
then compassion smacks you, and you say: "Oops, i forgot to
program Rosie to search in the garage, my Bad!"

Yes, "You bad" is correct!

The moral of our little story is that Python, much like your
beloved Rosie5000, will only look where you tell it to look.

Now, the Python gods have been kind enough to preprogram
some default search locations, but not all scripts are
automatically installed in these default locations (nor
should they be!). So if your script exists *OUTSIDE* of the
Python search path, you have two options (and possibly
more) to choose from:

(1) Move the script into a directory that is _already_
included in the search path (see: sys.path)

(2) Add the script's containing directory to the Python
search path manually (but don't do it by appending to
sys.path, that's bad juju!)

The first case is just a matter of cutting and pasting the
script in your file browser (aka: points and clicks). But
the second is a little more complicated and can be achieved
in many ways, some of which are OS specific (requiring
voodoo on the command line!). See the following links for
more detail (not exhaustive).

(NOTE: These links point to Python2.x documentation, but if
you're using a different version, be sure to choose your
version from the little drop- down box at the top of the
page so that you see the most relevent info)

https://docs.python.org/2/library/sys.html#sys.path
https://docs.python.org/2/using/cmdline.html#envvar-PYTHONPATH
https://docs.python.org/2/library/site.html?highlight=pth

--
[1] import sys; print(sys.path)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Modules

2018-03-21 Thread MRAB

On 2018-03-21 22:59, Jacques Bikoundou wrote:


Hi, I am writing because I use a Python 3 kernel on my notebook that I access 
through the Microsoft Azure ML. I experience error messages when using modules 
such as 'speedml' or 'xgboost'. The error messagessay: no such module. How can 
I solve this?Thanks, Jacques

Sent from Samsung tablet


A silly question, but have you installed those modules?
--
https://mail.python.org/mailman/listinfo/python-list


Re: Style Q: Instance variables defined outside of __init__

2018-03-21 Thread Rustom Mody
On Wednesday, March 21, 2018 at 5:37:48 AM UTC+5:30, Paul Rubin wrote:
> Ben Finney  writes:
> > Any program which needs to interact with systems outside itself – which
> > is to say, any program which performs useful work, ultimately – must
> > have side effects. So it's absurd to advocate removing *all* side
> > effects.
> 
> The way it (conceptually) works in Haskell is you write a purely
> functional program that returns a pure value of type "I/O action".  

Saw this coincidentally adjacent to this thread

https://youtu.be/ROor6_NGIWU

Its a view 'from the other side': a big name in functional programming
(not haskell) musing on how the world of IO, more generally world interaction,
could be cleaned up
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Writing a C extension - borrowed references

2018-03-21 Thread Stefan Behnel
Tom Evans via Python-list schrieb am 20.03.2018 um 18:03:
> On Tue, Mar 20, 2018 at 4:38 PM, Chris Angelico wrote:
>> BTW, have you looked into Cython? It's smart enough to take care of a
>> lot of this sort of thing for you.
> 
> I did a bit; this work is to replace our old python 2 SAML client,
> which used python-lasso and python-libxml2, both packages that are
> built as part of building the C library and thus an utter PITA to
> package for different versions of python (something our Infra team
> were extremely reluctant to do). When the latest (PITA to build)
> version of python-lasso started interfering with python-xmlsec
> (validating an invalid signature was causing segfaults), I got fed up
> of fighting it.

If rewriting is an option (just mentioning it ;) ), something like this
would probably be based on lxml these days. It also comes with a
C/Cython-level API, in case you need to a) do some kind of low-level tree
processing or b) integrate with some external library.


> I actually also maintain a C version of the same code, using the same
> libraries, so porting those few segments of code to Python/C seemed
> more expedient than rewriting in Cython. I'm not writing an API to
> these libraries, just a few functions.

Happy if that works for you, but let me still add a quick note that, from
my experience, rewriting C-API code in Cython tends to be a surprisingly
quick thing to do (it's basically just reverse engineering the Python code
from the C-API code), but it's a one-time investment that can reduce the
long-term maintenance cost a lot, often improves the performance, and
usually also leads to nicer Python APIs, as what you get in the end is more
obvious from the code, so you can focus on the handling more easily.

Stefan

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