random choice from dictionary

2018-12-23 Thread Abdur-Rahmaan Janhangeer
greetings,

just a check, is this:

random.choice(list(data.keys()))

the only way to get a random key from a dictionary? if not any plan to a
more convenient naming?

yours,

-- 
Abdur-Rahmaan Janhangeer
http://www.pythonmembers.club | https://github.com/Abdur-rahmaanJ
Mauritius


Garanti
sans virus. www.avast.com

<#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: random choice from dictionary

2018-12-23 Thread Chris Angelico
On Sun, Dec 23, 2018 at 7:34 PM Abdur-Rahmaan Janhangeer
 wrote:
>
> greetings,
>
> just a check, is this:
>
> random.choice(list(data.keys()))
>
> the only way to get a random key from a dictionary? if not any plan to a
> more convenient naming?

Does it have to be truly random, or are you okay with it being
arbitrary selected? Dictionaries have a popitem method that may be of
value. Otherwise, I would say what you have is fine, save that I'd
just write random.choice(list(data)) instead of using the unnecessary
keys() call.

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


Re: random choice from dictionary

2018-12-23 Thread Abdur-Rahmaan Janhangeer
random.choice(list(data)) is great. fine with arbitrary selection.

else, given data.keys(), how do i get the first key?

yours
-- 
Abdur-Rahmaan Janhangeer
http://www.pythonmembers.club | https://github.com/Abdur-rahmaanJ
Mauritius


Garanti
sans virus. www.avast.com

<#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Mask two images with python

2018-12-23 Thread Umar Yusuf
On Wednesday, 19 December 2018 06:36:01 UTC+1, Umar Yusuf  wrote:
> Hello there,
> How do I supper impose an image design on a transparent png image?
> 
> I have tried to use OpenCV's "cv2.bitwise_and" function to no success. I 
> posted the detail question here: 
> https://stackoverflow.com/questions/53791510/python-opencv-mask-and-glow
> 
> Thank you for your time.



Sorry for my bad explanation... I just don't know how to put this in image 
processing terms.

Anyway, I decided to give pillow module a look and its paste() method came 
close to what I wanted. So, have re-explain and posted it as new question here: 
https://stackoverflow.com/questions/53903607/overlay-pattern-image-on-exact-portion-of-png-image-with-python


Thank you


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


[SOLVED] Re: ah, progress...

2018-12-23 Thread ant
dieter wrote:

...

  thank you for your help.  :)  i finally worked through
the changes needed at last.


  my current package in testing PyPI is at:

  https://test.pypi.org/project/ngfp/

  which uses my code from:

  https://salsa.debian.org/ant-guest/gfpoken-in-python


  i ended up needing to do two things.  to get all the import
statements changed to include the package/module name and to
change all my image load statements to include the full path
as determined at runtime.

  this may not work on a non-linux or non-posix system from
the binary distribution but the source code distribution may
work ok.  if anyone wants to try it out.

  feedback is appreciated in any case.  :)


  now the next fun steps (figuring out the windows, other 
versions and then the Debian packaging for it).  since i
don't have windows or other machines to test on i don't 
know how that will go.

  cheers and thanks again,


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


Why am I getting Error 405 while uploading my package to https://test.pypi.org/legacy?

2018-12-23 Thread sntshkmr60
I'm trying to upload my package to PyPi, but before that I wanted to upload my 
package to TestPyPi.

I'm following https://packaging.python.org/guides/using-testpypi/

I'm issuing this command: `twine upload --repository-url 
https://test.pypi.org/legacy/ dist/*`

Here is the output:

```
Enter your username: sntshkmr60
Enter your password:
Uploading distributions to https://test.pypi.org/
Uploading mysecretpackage-0.0.1a0-py3-none-any.whl
100%|| 
16.7k/16.7k [00:06<00:00, 2.50kB/s]
NOTE: Try --verbose to see response content.
HTTPError: 405 Client Error: Method Not Allowed for url: https://test.pypi.org/
```

Here is the extra output from --verbose flag:

```
Content received from server:

 
  405 Method Not Allowed
 
 
  405 Method Not Allowed
  The method POST is not allowed for this resource. 

 

HTTPError: 405 Client Error: Method Not Allowed for url: https://test.pypi.org/
```

What am I doing wrong?
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: random choice from dictionary

2018-12-23 Thread Avi Gross
There are quite a few places the new pythonic way of doing things requires
extra steps to get an iterator to expand into a list so Abdul-Rahmann
probably is right that there is no easy way to get a random key from a
standard dictionary. Other than the expected answers to make a customized
dictionary or function that provides the functionality at the expense for
potentially generating a huge list of keys in memory, I offer the following
as an alternative for large dictionaries and especially if they have large
keys.

The following is a function that iterates over the dictionary one key at a
time and when it reaches a random one, it returns the key without further
evaluation. On the average, it takes N/2 iterations for N keys. Asking to
make a list(data) may be efficient in terms of time and indexing is fast but
space is used maximally unless it just points to existing storage.

Here is one way to do the function. Some might use an emum instead.

def rand_key(data):
"""Get a random key from a dict without using all of memory."""
import random
randy = random.randrange(len(data))
this = 0
for key in data:
if (this == randy):
return(key)
this += 1

Here is a sample showing it running:

>>> import string
   
>>> data= { key : value for (key, value) in zip(string.ascii_uppercase,
string.ascii_lowercase) }
   
>>> data
   
{'A': 'a', 'B': 'b', 'C': 'c', 'D': 'd', 'E': 'e', 'F': 'f', 'G': 'g', 'H':
'h', 'I': 'i', 'J': 'j', 'K': 'k', 'L': 'l', 'M': 'm', 'N': 'n', 'O': 'o',
'P': 'p', 'Q': 'q', 'R': 'r', 'S': 's', 'T': 't', 'U': 'u', 'V': 'v', 'W':
'w', 'X': 'x', 'Y': 'y', 'Z': 'z'}
>>> rand_key(data)
   
'X'
>>> rand_key(data)
   
'P'
>>> rand_key(data)
   
'D'
>>> rand_key(data)
   
'G'
>>> rand_key(data)
   
'H'
>>> data[rand_key(data)]
   
'x'
>>> data[rand_key(data)]
   
'w'
>>> data[rand_key(data)]
   
'n'
>>> data[rand_key(data)]
   
'h'

Of course it depends on what you want to do. If getting random keys
repeatedly, it would be best to do things like make another dictionary with
the new key being integers from 0 to the length and the new values to be the
old keys. You can then look up a random index to get a random key.

>>> dataindex = { index: key for (index, key) in enumerate(data)}
   
>>> dataindex
   
{0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'E', 5: 'F', 6: 'G', 7: 'H', 8: 'I', 9:
'J', 10: 'K', 11: 'L', 12: 'M', 13: 'N', 14: 'O', 15: 'P', 16: 'Q', 17: 'R',
18: 'S', 19: 'T', 20: 'U', 21: 'V', 22: 'W', 23: 'X', 24: 'Y', 25: 'Z'}
>>> dataindex[11]
   
'L'
>>> import random
   
>>> dataindex[random.randrange(len(dataindex))]
   
'Y'
>>> dataindex[random.randrange(len(dataindex))]
   
'O'

Again, depending on your needs, some solutions may be worth extra code, some
may not.


-Original Message-
From: Python-list  On
Behalf Of Abdur-Rahmaan Janhangeer
Sent: Sunday, December 23, 2018 1:17 AM
To: Python 
Subject: random choice from dictionary

greetings,

just a check, is this:

random.choice(list(data.keys()))

the only way to get a random key from a dictionary? if not any plan to a
more convenient naming?

yours,

--
Abdur-Rahmaan Janhangeer
http://www.pythonmembers.club | https://github.com/Abdur-rahmaanJ Mauritius


Garanti
sans virus. www.avast.com

<#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
--
https://mail.python.org/mailman/listinfo/python-list

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


Re: Why am I getting Error 405 while uploading my package to https://test.pypi.org/legacy?

2018-12-23 Thread ant
sntshkm...@gmail.com wrote:
> I'm trying to upload my package to PyPi, but before that I wanted to upload 
> my package to TestPyPi.
>
> I'm following https://packaging.python.org/guides/using-testpypi/
>
> I'm issuing this command: `twine upload --repository-url 
> https://test.pypi.org/legacy/ dist/*`
>
> Here is the output:
>
> ```
> Enter your username: sntshkmr60
> Enter your password:
> Uploading distributions to https://test.pypi.org/
> Uploading mysecretpackage-0.0.1a0-py3-none-any.whl
> 100%||
>  16.7k/16.7k [00:06<00:00, 2.50kB/s]
> NOTE: Try --verbose to see response content.
> HTTPError: 405 Client Error: Method Not Allowed for url: 
> https://test.pypi.org/
> ```
>
> Here is the extra output from --verbose flag:
>
> ```
> Content received from server:
>
> 
>  405 Method Not Allowed
> 
> 
>  405 Method Not Allowed
>   The method POST is not allowed for this resource. 
>
> 
>
> HTTPError: 405 Client Error: Method Not Allowed for url: 
> https://test.pypi.org/
> ```
>
> What am I doing wrong?

  well, i used it earlier so here is how i have it set up
as follows.

  because this is testing repository i am not worried about
password being stored in a file:

.pypirc 
=
[distutils]
index-servers=
testpypi

[testpypi]
repository: https://test.pypi.org/legacy/
username: UserName
password: Password
=


and my upload command is:


=
#!/bin/sh
#
# upload ngfp to test pypi

NGFP_SRC_HOME="/home/me/src/salsa/ngfp"
if test ! -d "$NGFP_SRC_HOME" ; then
  echo "  directory $NGFP_SRC_HOME does not exist!"
  exit
fi

cd $NGFP_SRC_HOME

twine upload --repository testpypi dist/*
=


  note: it may take a while for what you uploaded to be made
available for download again even if it reflected in your
project page.  sometimes it has been as long as a half hour
or more before it comes down.  other times it has only been
a few minutes.

  hope this helps...


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


Re: Why am I getting Error 405 while uploading my package to https://test.pypi.org/legacy?

2018-12-23 Thread ant
ant wrote:

...

> .pypirc 
>=
> [distutils]
> index-servers=
> testpypi
>
> [testpypi]
> repository: https://test.pypi.org/legacy/
> username: UserName
> password: Password
>=
>
>
> and my upload command is:
>
>
>=
> #!/bin/sh
> #
> # upload ngfp to test pypi
>
> NGFP_SRC_HOME="/home/me/src/salsa/ngfp"
> if test ! -d "$NGFP_SRC_HOME" ; then
>   echo "  directory $NGFP_SRC_HOME does not exist!"
>   exit
> fi
>
> cd $NGFP_SRC_HOME
>
> twine upload --repository testpypi dist/*
>=


  since i just went through this myself i thought i'd
add a bit more.

  when you change over to post to the main pypi.org
you need to do it to upload.pypi.org like:

=
[pypi]
repository: https://upload.pypi.org/legacy/
=

  as i was getting 405 errors when using just pypi.org
i suspect your error above is that you're not using the
/legacy/ part at the end...

  good luck!


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


Re: Why am I getting Error 405 while uploading my package to https://test.pypi.org/legacy?

2018-12-23 Thread sntshkmr60
> .pypirc 
>=
> [distutils]
> index-servers=
> testpypi
>
> [testpypi]
> repository: https://test.pypi.org/legacy/
> username: UserName
> password: Password


> twine upload --repository testpypi dist/*

Tried your suggestion for .pypirc file. Still the same error.

Has something changed and is not reflected in the documentation?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: random choice from dictionary

2018-12-23 Thread MRAB

On 2018-12-23 19:52, Avi Gross wrote:

There are quite a few places the new pythonic way of doing things requires
extra steps to get an iterator to expand into a list so Abdul-Rahmann
probably is right that there is no easy way to get a random key from a
standard dictionary. Other than the expected answers to make a customized
dictionary or function that provides the functionality at the expense for
potentially generating a huge list of keys in memory, I offer the following
as an alternative for large dictionaries and especially if they have large
keys.

The following is a function that iterates over the dictionary one key at a
time and when it reaches a random one, it returns the key without further
evaluation. On the average, it takes N/2 iterations for N keys. Asking to
make a list(data) may be efficient in terms of time and indexing is fast but
space is used maximally unless it just points to existing storage.

Here is one way to do the function. Some might use an emum instead.

def rand_key(data):
 """Get a random key from a dict without using all of memory."""
 import random
 randy = random.randrange(len(data))
 this = 0
 for key in data:
 if (this == randy):
 return(key)
 this += 1


[snip]
You might as well use 'enumerate' there:

def rand_key(data):
"""Get a random key from a dict without using all of memory."""
import random
randy = random.randrange(len(data))
for this, key in enumerate(data):
if this == randy:
return key

Or you could use 'islice':

def rand_key(data):
"""Get a random key from a dict without using all of memory."""
import itertools
import random
randy = random.randrange(len(data))
return list(itertools.islice(data, randy, randy + 1))[0]
--
https://mail.python.org/mailman/listinfo/python-list


Re: Why am I getting Error 405 while uploading my package to https://test.pypi.org/legacy?

2018-12-23 Thread ant
sntshkm...@gmail.com wrote:
>> .pypirc 
>>=
>> [distutils]
>> index-servers=
>> testpypi
>>
>> [testpypi]
>> repository: https://test.pypi.org/legacy/
>> username: UserName
>> password: Password
>
>
>> twine upload --repository testpypi dist/*
>
> Tried your suggestion for .pypirc file. Still the same error.
>
> Has something changed and is not reflected in the documentation?


  did you check your dists via twine?


  $ twine check --verbose dist/*

  also make sure you are using up to date versions of
setuptools, wheel and twine


  $ pip install --upgrade setuptools
  $ pip install --upgrade wheel
  $ pip install --upgrade twine


  i just used it earlier today...  worked fine for me.
is your account there and verified?


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


Re: Why am I getting Error 405 while uploading my package to https://test.pypi.org/legacy?

2018-12-23 Thread sntshkmr60
>   did you check your dists via twine?
> 
>   $ twine check --verbose dist/*

I checked with `twine check dist/*` (there's no --verbose option though).
I only get warning about markdown

>   also make sure you are using up to date versions of
> setuptools, wheel and twine
> 
> 
>   $ pip install --upgrade setuptools
>   $ pip install --upgrade wheel
>   $ pip install --upgrade twine

[65]> import setuptools, wheel, twine
[66]> print(setuptools.__version__)
40.6.3
[67]> print(wheel.__version__)
0.32.3
[68]> print(twine.__version__)
1.12.1


>   i just used it earlier today...  worked fine for me.
> is your account there and verified?

Yes, I have an account on TestPyPI and is verified. 

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


Re: Why am I getting Error 405 while uploading my package to https://test.pypi.org/legacy?

2018-12-23 Thread ant
sntshkm...@gmail.com wrote:
>>   did you check your dists via twine?
>> 
>>   $ twine check --verbose dist/*
>
> I checked with `twine check dist/*` (there's no --verbose option though).
> I only get warning about markdown

  right, i see later in my history i used the twine
check without the verbose option.

  you have to fix that for it to upload...

  $ pip install readme_renderer[md]


>>   also make sure you are using up to date versions of
>> setuptools, wheel and twine
>> 
>> 
>>   $ pip install --upgrade setuptools
>>   $ pip install --upgrade wheel
>>   $ pip install --upgrade twine
>
> [65]> import setuptools, wheel, twine
> [66]> print(setuptools.__version__)
> 40.6.3
> [67]> print(wheel.__version__)
> 0.32.3
> [68]> print(twine.__version__)
> 1.12.1
>
>
>>   i just used it earlier today...  worked fine for me.
>> is your account there and verified?
>
> Yes, I have an account on TestPyPI and is verified. 

  try the above...


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


Re: random choice from dictionary

2018-12-23 Thread Ben Bacarisse
"Avi Gross"  writes:

> The following is a function that iterates over the dictionary one key at a
> time and when it reaches a random one, it returns the key without further
> evaluation. On the average, it takes N/2 iterations for N keys. Asking to
> make a list(data) may be efficient in terms of time and indexing is fast but
> space is used maximally unless it just points to existing storage.
>
> Here is one way to do the function. Some might use an emum instead.
>
> def rand_key(data):
> """Get a random key from a dict without using all of memory."""
> import random
> randy = random.randrange(len(data))
> this = 0
> for key in data:
> if (this == randy):
> return(key)
> this += 1

There is beautiful variation on this that can select k random keys:

def sample_keys(data, k = 1):
"""Return k random keys from a dict."""
import random
items = len(data)
keys = []
for key in data:
r = random.random()
if r < k/items:
keys.append(key)
k -= 1
items -= 1
return keys

And there is a variation on /that/ will use fewer random numbers --
essentially picking a number of "skip" distances as you pick one.  But
it's late, and I need to get ready to eat lots...


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


Re: random choice from dictionary

2018-12-23 Thread sarah123ed


Maybe something like this?

import random
k = list(data.keys())
random.shuffle(k)
selected = f[k[0]]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: random choice from dictionary

2018-12-23 Thread sarah123ed



Maybe something like this:

import random
k = list(data.keys())
random.shuffle(k)
result = data[k[0]]



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


RE: random choice from dictionary

2018-12-23 Thread Avi Gross
I did say some would use enumerate but I was rushing to go to my nephews 
wedding and my enum became emum which is meaningless 😊

If the goal is to make a relatively efficient algorithm and the index made by 
enumerate is not needed, then I judged that the overhead of enumerate(data) 
returning two values to be unpacked each time through the loop might be more 
than having a single counter. Of course enumerate likely is considered more 
pythonic 😊

Similarly, the suggestion to use islice (although the name is also 
unfortunately reminiscent of is lice) may be a good one. But it basically does 
what my function did, perhaps as fast and perhaps not. My impression is it 
iterates using next() a number of times then stops. It is so simple, there 
might be no need for my function. The request was to find an nth item and you 
can feed (the random) n directly into islice.

Just for amusement, I already get a random word a day in my email from two 
dictionary sources in English as well as quite a few in other languages. I not 
only get the word as a key, but also a translation and even an example sentence 
with translation.

But I doubt the words are chosen randomly and they are not likely to be from a 
python dictionary.😊

-Original Message-
From: Python-list  On 
Behalf Of MRAB
Sent: Sunday, December 23, 2018 4:21 PM
To: python-list@python.org
Subject: Re: random choice from dictionary

On 2018-12-23 19:52, Avi Gross wrote:
> There are quite a few places the new pythonic way of doing things 
> requires extra steps to get an iterator to expand into a list so 
> Abdul-Rahmann probably is right that there is no easy way to get a 
> random key from a standard dictionary. Other than the expected answers 
> to make a customized dictionary or function that provides the 
> functionality at the expense for potentially generating a huge list of 
> keys in memory, I offer the following as an alternative for large 
> dictionaries and especially if they have large keys.
> 
> The following is a function that iterates over the dictionary one key 
> at a time and when it reaches a random one, it returns the key without 
> further evaluation. On the average, it takes N/2 iterations for N 
> keys. Asking to make a list(data) may be efficient in terms of time 
> and indexing is fast but space is used maximally unless it just points to 
> existing storage.
> 
> Here is one way to do the function. Some might use an emum instead.
> 
> def rand_key(data):
>  """Get a random key from a dict without using all of memory."""
>  import random
>  randy = random.randrange(len(data))
>  this = 0
>  for key in data:
>  if (this == randy):
>  return(key)
>  this += 1
> 
[snip]
You might as well use 'enumerate' there:

def rand_key(data):
 """Get a random key from a dict without using all of memory."""
 import random
 randy = random.randrange(len(data))
 for this, key in enumerate(data):
 if this == randy:
 return key

Or you could use 'islice':

def rand_key(data):
 """Get a random key from a dict without using all of memory."""
 import itertools
 import random
 randy = random.randrange(len(data))
 return list(itertools.islice(data, randy, randy + 1))[0]
--
https://mail.python.org/mailman/listinfo/python-list

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