Re: Optimizing Small Python Code

2021-06-24 Thread jan via Python-list
If I'm not mistaken, the original output is O(n^2) in quantity of
chars, and as output time is proportional to the length of the output,
that makes the output time also O(n^2) here too.

So I guess this only looks like it's reduced to linear because the
output here is very small. For large n it would become obvious.

jan

On 24/06/2021, Avi Gross via Python-list  wrote:
> Yes, I agree that if you do not need to show your work to a human, then the
> problem specified could be solved beforeand and a simple print statement
> would suffice.
>
> Ideally you want to make a problem harder such as by specifying an N that
> varies then testing it with an arbitrary N.
>
> But I suggest the item below is not minimal. You can store the printout
> more
> compactly as the only symbols out put are tabs, newlines and seven digits.
> If your language supported some function that expanded a binary string that
> used say 4 bytes per symbol so it could be printed, or accepted a
> compressed
> form of the string and uncompressed it, you might have code like:
>
> print(unzip("n*n&&S!~se"))
>
> -Original Message-
> From: Python-list  On
> Behalf Of Michael F. Stemper
> Sent: Wednesday, June 23, 2021 10:23 AM
> To: [email protected]
> Subject: Re: Optimizing Small Python Code
>
> On 23/06/2021 08.17, Stefan Ram wrote:
>> "Avi Gross"  writes:
>>> This can be made a one-liner too! LOL!
>>
>> print( '1\n  0\n2\n  0\n  1\n3\n  0\n  1\n
>> 2\n4\n
> 0\n  1\n  2\n  3\n5\n  0\n  1\n  2\n  3\n
> 4\n6\n  0\n  1\n  2\n  3\n  4\n  5\n' )
>
> Unless I'm figuring ot wrong, you just took it from O(n^2) to O(1). That
> deserves a Turing award or something.
>
> --
> Michael F. Stemper
> You can lead a horse to water, but you can't make him talk like Mr. Ed by
> rubbing peanut butter on his gums.
> --
> https://mail.python.org/mailman/listinfo/python-list
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: creating raw AWS log event

2021-06-24 Thread Peter Otten

On 23/06/2021 19:42, Larry Martell wrote:

When an AWS cloudwatch event is passed to a consumer it looks like this:

{
 "awslogs": {
  "data": "ewogICAgIm1l..."
  }
}

To get the actual message I do this:

def _decode(data):
 compressed_payload = b64decode(data)
 json_payload = zlib.decompress(compressed_payload, 16+zlib.MAX_WBITS)
 return json.loads(json_payload)

message = _decode(json.dumps(event['awslogs']['data']))

This returns the log message as a string.

For my unit tests I need to reverse this - given a message as a string
I want to generate the compressed, encoded event structure.

I have not been able to get this to work. I have this:

message  = b'test message'
compressed= zlib.compress(message)
event['awslogs']['data'] = str(compressed)

message = _decode(json.dumps(event['awslogs']['data']))
Traceback (most recent call last):
   File "", line 1, in 
   File "", line 3, in _decode
zlib.error: Error -3 while decompressing data: incorrect header check

Anyone see how to make this work?


The json/bas64 parts are not involved in the problem:

>>> zlib.decompress(zlib.compress(b"foo"), 16 + zlib.MAX_WBITS)
Traceback (most recent call last):
  File "", line 1, in 
zlib.decompress(zlib.compress(b"foo"), 16 + zlib.MAX_WBITS)
zlib.error: Error -3 while decompressing data: incorrect header check

whereas:

>>> zlib.decompress(zlib.compress(b"foo"))
b'foo'

Unfortunately compress() doesn't accept the flags you seem to require.
However, reading around a bit in the zlib docs turns up the compressobj 
which does. So


>>> def mycompress(data):
obj = zlib.compressobj(wbits=16 + zlib.MAX_WBITS)
result = obj.compress(data)
result += obj.flush()
return result

>>> zlib.decompress(mycompress(b"foo"), 16 + zlib.MAX_WBITS)
b'foo'



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


RE: Optimizing Small Python Code

2021-06-24 Thread Avi Gross via Python-list
Jan,

As an academic discussion, yes, many enhancements only pay off when things are 
larger. 

And for most purposes, I/O is so much slower that it makes little difference. 
But on a multi-processing machine, extra CPU may impact how much else the 
machine can do at the same time.

Here is an example of what I meant. Given exactly the current output, what is 
the shorter program you can use to cheat and produce an answer.

Recall the original output has a length of 169:

a = """1
  0
2
  0
  1
3
  0
  1
 2
4
0
  1
  2
  3
5
  0
  1
  2
  3
4
6
  0
  1
  2
  3
  4
  5
  """

len(a)


Now if you had already used a compression algorithm on it, you would get this:

import zlib
a = zlib.compress(a.encode("ascii"))
len(a)

Now a has a length of 53!

It now looks like this:

b'x\x9c3\xe4R\x00\x03\x03.#8\x0bB\x1br\x19c\x88(\x18q\x99p!q\xc1\x00\xa6\xd1\x98\xcb\x14S\x03\x9a\n\x13.3\x82j
 \xb4\t\x94\x86\x99\t\x00\xdc\x87\x14\xb7'

So the shorter cheat program might now be:

>>> print(zlib.decompress(b'x\x9c3\xe4R\x00\x03\x03.#8\x0bB\x1br\x19c\x88(\x18q\x99p!q\xc1\x00\xa6\xd1\x98\xcb\x14S\x03\x9a\n\x13.3\x82j
>>>  \xb4\t\x94\x86\x99\t\x00\xdc\x87\x14\xb7').decode("ASCII"))
1
  0
2
  0
  1
3
  0
  1
 2
4
0
  1
  2
  3
5
  0
  1
  2
  3
4
6
  0
  1
  2
  3
  4
  5
  
Of course, the above has overhead as you have a line to import the library as 
well as the darn function names but for larger amounts of text, those stay 
fixed.

I note again this won't fool humans but some on-line courses that just take 
your program and run it and only compare the output will be fooled.


-Original Message-
From: jan  
Sent: Thursday, June 24, 2021 3:01 AM
To: Avi Gross 
Cc: [email protected]
Subject: Re: Optimizing Small Python Code

If I'm not mistaken, the original output is O(n^2) in quantity of chars, and as 
output time is proportional to the length of the output, that makes the output 
time also O(n^2) here too.

So I guess this only looks like it's reduced to linear because the output here 
is very small. For large n it would become obvious.

jan

On 24/06/2021, Avi Gross via Python-list  wrote:
> Yes, I agree that if you do not need to show your work to a human, 
> then the problem specified could be solved beforeand and a simple 
> print statement would suffice.
>
> Ideally you want to make a problem harder such as by specifying an N 
> that varies then testing it with an arbitrary N.
>
> But I suggest the item below is not minimal. You can store the 
> printout more compactly as the only symbols out put are tabs, newlines 
> and seven digits.
> If your language supported some function that expanded a binary string 
> that used say 4 bytes per symbol so it could be printed, or accepted a 
> compressed form of the string and uncompressed it, you might have code 
> like:
>
> print(unzip("n*n&&S!~se"))
>
> -Original Message-
> From: Python-list 
>  On Behalf Of 
> Michael F. Stemper
> Sent: Wednesday, June 23, 2021 10:23 AM
> To: [email protected]
> Subject: Re: Optimizing Small Python Code
>
> On 23/06/2021 08.17, Stefan Ram wrote:
>> "Avi Gross"  writes:
>>> This can be made a one-liner too! LOL!
>>
>> print( '1\n  0\n2\n  0\n  1\n3\n  0\n  1\n
>> 2\n4\n
> 0\n  1\n  2\n  3\n5\n  0\n  1\n  2\n  3\n
> 4\n6\n  0\n  1\n  2\n  3\n  4\n  5\n' )
>
> Unless I'm figuring ot wrong, you just took it from O(n^2) to O(1). 
> That deserves a Turing award or something.
>
> --
> Michael F. Stemper
> You can lead a horse to water, but you can't make him talk like Mr. Ed 
> by rubbing peanut butter on his gums.
> --
> https://mail.python.org/mailman/listinfo/python-list
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>

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


Re: creating raw AWS log event

2021-06-24 Thread Larry Martell
On Thu, Jun 24, 2021 at 12:20 AM Peter Otten <[email protected]> wrote:
>
> On 23/06/2021 19:42, Larry Martell wrote:
> > When an AWS cloudwatch event is passed to a consumer it looks like this:
> >
> > {
> >  "awslogs": {
> >   "data": "ewogICAgIm1l..."
> >   }
> > }
> >
> > To get the actual message I do this:
> >
> > def _decode(data):
> >  compressed_payload = b64decode(data)
> >  json_payload = zlib.decompress(compressed_payload, 16+zlib.MAX_WBITS)
> >  return json.loads(json_payload)
> >
> > message = _decode(json.dumps(event['awslogs']['data']))
> >
> > This returns the log message as a string.
> >
> > For my unit tests I need to reverse this - given a message as a string
> > I want to generate the compressed, encoded event structure.
> >
> > I have not been able to get this to work. I have this:
> >
> > message  = b'test message'
> > compressed= zlib.compress(message)
> > event['awslogs']['data'] = str(compressed)
> >
> > message = _decode(json.dumps(event['awslogs']['data']))
> > Traceback (most recent call last):
> >File "", line 1, in 
> >File "", line 3, in _decode
> > zlib.error: Error -3 while decompressing data: incorrect header check
> >
> > Anyone see how to make this work?
>
> The json/bas64 parts are not involved in the problem:
>
>  >>> zlib.decompress(zlib.compress(b"foo"), 16 + zlib.MAX_WBITS)
> Traceback (most recent call last):
>File "", line 1, in 
>  zlib.decompress(zlib.compress(b"foo"), 16 + zlib.MAX_WBITS)
> zlib.error: Error -3 while decompressing data: incorrect header check
>
> whereas:
>
>  >>> zlib.decompress(zlib.compress(b"foo"))
> b'foo'
>
> Unfortunately compress() doesn't accept the flags you seem to require.
> However, reading around a bit in the zlib docs turns up the compressobj
> which does. So
>
>  >>> def mycompress(data):
> obj = zlib.compressobj(wbits=16 + zlib.MAX_WBITS)
> result = obj.compress(data)
> result += obj.flush()
> return result
>
>  >>> zlib.decompress(mycompress(b"foo"), 16 + zlib.MAX_WBITS)
> b'foo'

Thanks. Turns out I don't need this after all.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Optimizing Small Python Code

2021-06-24 Thread jan via Python-list
You're right, I was making an academic point.

I think this whole compression question relates to Kolmogorov
complexity 

"In algorithmic information theory (a subfield of computer science and
mathematics), the Kolmogorov complexity of an object, such as a piece
of text, is the length of a shortest computer program (in a
predetermined programming language) that produces the object as
output".

cheers

jan

On 24/06/2021, Avi Gross via Python-list  wrote:
> Jan,
>
> As an academic discussion, yes, many enhancements only pay off when things
> are larger.
>
> And for most purposes, I/O is so much slower that it makes little
> difference. But on a multi-processing machine, extra CPU may impact how much
> else the machine can do at the same time.
>
> Here is an example of what I meant. Given exactly the current output, what
> is the shorter program you can use to cheat and produce an answer.
>
> Recall the original output has a length of 169:
>
> a = """1
>   0
> 2
>   0
>   1
> 3
>   0
>   1
>  2
> 4
> 0
>   1
>   2
>   3
> 5
>   0
>   1
>   2
>   3
> 4
> 6
>   0
>   1
>   2
>   3
>   4
>   5
>   """
>
> len(a)
>
>
> Now if you had already used a compression algorithm on it, you would get
> this:
>
> import zlib
> a = zlib.compress(a.encode("ascii"))
> len(a)
>
> Now a has a length of 53!
>
> It now looks like this:
>
> b'x\x9c3\xe4R\x00\x03\x03.#8\x0bB\x1br\x19c\x88(\x18q\x99p!q\xc1\x00\xa6\xd1\x98\xcb\x14S\x03\x9a\n\x13.3\x82j
> \xb4\t\x94\x86\x99\t\x00\xdc\x87\x14\xb7'
>
> So the shorter cheat program might now be:
>
 print(zlib.decompress(b'x\x9c3\xe4R\x00\x03\x03.#8\x0bB\x1br\x19c\x88(\x18q\x99p!q\xc1\x00\xa6\xd1\x98\xcb\x14S\x03\x9a\n\x13.3\x82j
 \xb4\t\x94\x86\x99\t\x00\xdc\x87\x14\xb7').decode("ASCII"))
> 1
>   0
> 2
>   0
>   1
> 3
>   0
>   1
>  2
> 4
> 0
>   1
>   2
>   3
> 5
>   0
>   1
>   2
>   3
> 4
> 6
>   0
>   1
>   2
>   3
>   4
>   5
>
> Of course, the above has overhead as you have a line to import the library
> as well as the darn function names but for larger amounts of text, those
> stay fixed.
>
> I note again this won't fool humans but some on-line courses that just take
> your program and run it and only compare the output will be fooled.
>
>
> -Original Message-
> From: jan 
> Sent: Thursday, June 24, 2021 3:01 AM
> To: Avi Gross 
> Cc: [email protected]
> Subject: Re: Optimizing Small Python Code
>
> If I'm not mistaken, the original output is O(n^2) in quantity of chars, and
> as output time is proportional to the length of the output, that makes the
> output time also O(n^2) here too.
>
> So I guess this only looks like it's reduced to linear because the output
> here is very small. For large n it would become obvious.
>
> jan
>
> On 24/06/2021, Avi Gross via Python-list  wrote:
>> Yes, I agree that if you do not need to show your work to a human,
>> then the problem specified could be solved beforeand and a simple
>> print statement would suffice.
>>
>> Ideally you want to make a problem harder such as by specifying an N
>> that varies then testing it with an arbitrary N.
>>
>> But I suggest the item below is not minimal. You can store the
>> printout more compactly as the only symbols out put are tabs, newlines
>> and seven digits.
>> If your language supported some function that expanded a binary string
>> that used say 4 bytes per symbol so it could be printed, or accepted a
>> compressed form of the string and uncompressed it, you might have code
>> like:
>>
>> print(unzip("n*n&&S!~se"))
>>
>> -Original Message-
>> From: Python-list
>>  On Behalf Of
>> Michael F. Stemper
>> Sent: Wednesday, June 23, 2021 10:23 AM
>> To: [email protected]
>> Subject: Re: Optimizing Small Python Code
>>
>> On 23/06/2021 08.17, Stefan Ram wrote:
>>> "Avi Gross"  writes:
 This can be made a one-liner too! LOL!
>>>
>>> print( '1\n  0\n2\n  0\n  1\n3\n  0\n  1\n
>>> 2\n4\n
>> 0\n  1\n  2\n  3\n5\n  0\n  1\n  2\n  3\n
>> 4\n6\n  0\n  1\n  2\n  3\n  4\n  5\n' )
>>
>> Unless I'm figuring ot wrong, you just took it from O(n^2) to O(1).
>> That deserves a Turing award or something.
>>
>> --
>> Michael F. Stemper
>> You can lead a horse to water, but you can't make him talk like Mr. Ed
>> by rubbing peanut butter on his gums.
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>>
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Optimizing Small Python Code

2021-06-24 Thread Barry Scott



> On 24 Jun 2021, at 16:58, Avi Gross via Python-list  
> wrote:
> 
> Now a has a length of 53!
> 
> It now looks like this:
> 
> b'x\x9c3\xe4R\x00\x03\x03.#8\x0bB\x1br\x19c\x88(\x18q\x99p!q\xc1\x00\xa6\xd1\x98\xcb\x14S\x03\x9a\n\x13.3\x82j
>  \xb4\t\x94\x86\x99\t\x00\xdc\x87\x14\xb7'
> 
> So the shorter cheat program might now be:

The data is smaller, but at the cost of the code that knows how to decompress 
it.

I'd expect that by using compression you have increased memory use as this
amount of data is far smaller than code to decompress it.

Barry

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


Re: creating raw AWS log event

2021-06-24 Thread Larry Martell
On Thu, Jun 24, 2021 at 10:38 AM Larry Martell  wrote:
>
> On Thu, Jun 24, 2021 at 12:20 AM Peter Otten <[email protected]> wrote:
> >
> > On 23/06/2021 19:42, Larry Martell wrote:
> > > When an AWS cloudwatch event is passed to a consumer it looks like this:
> > >
> > > {
> > >  "awslogs": {
> > >   "data": "ewogICAgIm1l..."
> > >   }
> > > }
> > >
> > > To get the actual message I do this:
> > >
> > > def _decode(data):
> > >  compressed_payload = b64decode(data)
> > >  json_payload = zlib.decompress(compressed_payload, 16+zlib.MAX_WBITS)
> > >  return json.loads(json_payload)
> > >
> > > message = _decode(json.dumps(event['awslogs']['data']))
> > >
> > > This returns the log message as a string.
> > >
> > > For my unit tests I need to reverse this - given a message as a string
> > > I want to generate the compressed, encoded event structure.
> > >
> > > I have not been able to get this to work. I have this:
> > >
> > > message  = b'test message'
> > > compressed= zlib.compress(message)
> > > event['awslogs']['data'] = str(compressed)
> > >
> > > message = _decode(json.dumps(event['awslogs']['data']))
> > > Traceback (most recent call last):
> > >File "", line 1, in 
> > >File "", line 3, in _decode
> > > zlib.error: Error -3 while decompressing data: incorrect header check
> > >
> > > Anyone see how to make this work?
> >
> > The json/bas64 parts are not involved in the problem:
> >
> >  >>> zlib.decompress(zlib.compress(b"foo"), 16 + zlib.MAX_WBITS)
> > Traceback (most recent call last):
> >File "", line 1, in 
> >  zlib.decompress(zlib.compress(b"foo"), 16 + zlib.MAX_WBITS)
> > zlib.error: Error -3 while decompressing data: incorrect header check
> >
> > whereas:
> >
> >  >>> zlib.decompress(zlib.compress(b"foo"))
> > b'foo'
> >
> > Unfortunately compress() doesn't accept the flags you seem to require.
> > However, reading around a bit in the zlib docs turns up the compressobj
> > which does. So
> >
> >  >>> def mycompress(data):
> > obj = zlib.compressobj(wbits=16 + zlib.MAX_WBITS)
> > result = obj.compress(data)
> > result += obj.flush()
> > return result
> >
> >  >>> zlib.decompress(mycompress(b"foo"), 16 + zlib.MAX_WBITS)
> > b'foo'
>
> Thanks. Turns out I don't need this after all.

Well I did need this after all and this works perfectly. Thanks so much!
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Optimizing Small Python Code

2021-06-24 Thread Avi Gross via Python-list
Barry,

 

I am not going to continue replying in this thread. I made my point and it
was focused after a while on the size of the CODE. Of course, the code can
be made ever smaller using gimmicks and yet run longer or use more memory or
CPU.

 

If you have control of a package with a short name like "x" for example, you
can write a function y() that builds in the code described or any variant
and your program becomes:

 

Import x

y()

 

and it would print out whatever you need.

 

If you can get the python interpreter to make that module standard, it can
be shorted more!

 

As mentioned, one suggested metric for how complex a program is, or maybe
how much information it contains, is the shortest program you can write to
generate the info. Something already compressed may not be easy to make
smaller. But the above idea does not care if the solution takes forever to
run as in if it uses lots of tiny re-usable functions and does lots of
recursion. 

 

Clearly, as has been pointed out, for many of the one-off programs people
write here, optimization is not necessarily worthwhile.  The program asked
for here actually has minimal purpose as it does one thing that could be
done in advance and replaced by a print statement. My "contribution" above
does look stupid but consider what it would mean if the program asked you to
print the first billion prime numbers. The program that simulated the output
would likely read in  huge file from disk and print it. The first part of
the I/O might be much faster if the data on disk was compressed and if it
could be uncompressed as it was being read and printed to the output, might
be lots faster and even use less memory. Clearly it would likely use more
CPU but far less than finding the first billion primes that hard way.

 

Final note is there is not much in the discussion that is really about
Python as much of the argument remains similar in many programming
languages.

 

So, I am done and will ignore this thread.

 

From: Barry Scott  
Sent: Thursday, June 24, 2021 3:12 PM
To: Avi Gross 
Cc: [email protected]
Subject: Re: Optimizing Small Python Code

 

 





On 24 Jun 2021, at 16:58, Avi Gross via Python-list mailto:[email protected]> > wrote:

 

Now a has a length of 53!

It now looks like this:

b'x\x9c3\xe4R\x00\x03\x03.#8\x0bB\x1br\x19c\x88(\x18q\x99p!q\xc1\x00\xa6\xd1
\x98\xcb\x14S\x03\x9a\n\x13.3\x82j \xb4\t\x94\x86\x99\t\x00\xdc\x87\x14\xb7'

So the shorter cheat program might now be:

 

The data is smaller, but at the cost of the code that knows how to
decompress it.

 

I'd expect that by using compression you have increased memory use as this

amount of data is far smaller than code to decompress it.

 

Barry

 

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


Re: creating raw AWS log event

2021-06-24 Thread Grant Edwards
On 2021-06-24, Larry Martell  wrote:
> On Wed, Jun 23, 2021 at 7:05 PM Dennis Lee Bieber  
> wrote:
>>
>> On Wed, 23 Jun 2021 10:42:42 -0700, Larry Martell 
>> declaimed the following:
>>
>> >def _decode(data):
>> >compressed_payload = b64decode(data)
>> >json_payload = zlib.decompress(compressed_payload, 16+zlib.MAX_WBITS)
>> >return json.loads(json_payload)
>> >
>>
>> >message  = b'test message'
>> >compressed= zlib.compress(message)
>> >event['awslogs']['data'] = str(compressed)
>>
>> Where do you perform a B64 ENCODE operation?
>>
>> str() doesn't do that, it just converts the argument to a string 
>> which
>> may mean using escapes for non-printable bytes. B64 converts everything to
>> printable characters.
>
> Copy/paste fail. This is actually the code I tried:
>
> message = b'test message'
> compressed= zlib.compress(message)
> encoded = b64encode(compressed)
> event['awslogs']['data'] = str(encoded)

Try printout out the result of each step:

>>> encoded
b'eJwrSS0uUchNLS5OTE8FAB8fBMY='
>>> str(encoded)
"b'eJwrSS0uUchNLS5OTE8FAB8fBMY='"

Ah, that's not what you want, is it?

>>> encoded.decode('ascii')
'eJwrSS0uUchNLS5OTE8FAB8fBMY='

That's better.

-- 
Grant Edwards   grant.b.edwardsYow! I think my career
  at   is ruined!
  gmail.com

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


Re: How to iterate through maildir messages in python 3?

2021-06-24 Thread Greg Ewing

On 25/06/21 7:06 am, Chris Green wrote:

In python 2 one can do:-

for msg in maildir:
   print msg  # or whatever you want to do with the message


However in python 3 this produces "TypeError: string argument
expected, got 'bytes'".

How should one iterate over a maildir in python3?


You're already iterating over it just fine. Your problem is
actually how to *print* a mail message.

The answer to this will depend on what your purpose is. If
you just want a rough-and-ready idea of what the message
contains, you could do this:

print(repr(msg))

However, that won't work very well if the message doesn't
consist of mostly text in an ASCII-compatible encoding.

If you want something better, Python comes with some standard
library code for dealing with mail messages. Check out the
'email' module.

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


Re: round decimal values

2021-06-24 Thread jo
Il Thu, 24 Jun 2021 18:07:07 +0200, Julio Di Egidio ha scritto:

> On 24/06/2021 16:23, jo wrote:
>> Hi,
>> 
>> this code generate 4 values with gaussian distribution (mu=5, sigma=1):
>> 
>> import numpy as np x = np.random.normal(5, 1, 4)
>> print(x)
>> 
>> Output:
>> [5.87879753 3.29162433 3.83024698 4.92997148]
>> 
>> I would like to round the output like this:
>> 
>> [6, 3, 4, 5]
>> 
>> What should I add to the code?
>> 
>> Thank you
> 
> Have a look at numpy.around:
> 
> 
> HTH,
> 
> Julio

Yes, np.around() works fine.
Thank you.

This is the new code:

import numpy as np  
 
x = np.random.normal(5, 1, 4)

y=np.around(x)

print(x)

print(y)


output:

[4.68551569 3.9610641  6.1119447  4.81012246]
[5. 4. 6. 5.]

Thank you very much,

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


round decimal values

2021-06-24 Thread jo
Hi,

this code generate 4 values with gaussian distribution (mu=5, sigma=1):

import numpy as np   
x = np.random.normal(5, 1, 4)
print(x)

Output:
[5.87879753 3.29162433 3.83024698 4.92997148]

I would like to round the output like this:

[6, 3, 4, 5]

What should I add to the code?

Thank you

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


How to iterate through maildir messages in python 3?

2021-06-24 Thread Chris Green
In python 2 one can do:-

   for msg in maildir: 
  print msg  # or whatever you want to do with the message


However in python 3 this produces "TypeError: string argument
expected, got 'bytes'".

How should one iterate over a maildir in python3?


(I've been here before but this problem is subtly different from the
one I had before and I can't find the answer)


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