Re: Weighted choices

2013-09-07 Thread Peter Otten
Jason Friedman wrote:

> choices = dict()
> choices["apple"] = 10
> choices["pear"] = 20
> choices["banana"] = 15
> choices["orange"] = 25
> choices["kiwi"] = 30
> 
> I want to pick sets of fruit, three in a set, where the chance of
> selecting a given fruit is proportional to its weight.  In the example
> above, pears should appear twice as often as apples and kiwis should
> appear twice as often as bananas.
> 
> I see this solution
> (http://stackoverflow.com/questions/3679694/a-weighted-version-of-random-
choice)
> but it is for single choices at a time.  random.sample() does not
> provide a "weight" argument.
> 
> I can picture how to write this with many, many lines of code but I
> have a feeling there is an elegant solution.

Let's assume for a moment that you always have low integral weights. Given 

data = []
for weight, value in choices.items():
   data.extend([value]*weight)

picking values with

N = 3
sample = [random.choice(data) for i in range(N)]

does not give te same probabilities as

sample = random.sample(data, N)

Which one doe you want? If it's the former, i. e. picking an item does not 
affect the item's probability, just adapt Raymond Hettinger's

def weighted_choice(choices):
values, weights = zip(*choices)
total = 0
cum_weights = []
for w in weights:
total += w
cum_weights.append(total)
x = random() * total
i = bisect(cum_weights, x)
return values[i]

to (untested)

def weighted_choice_iter(choices):
values, weights = zip(*choices)
total = 0
cum_weights = []
for w in weights:
total += w
cum_weights.append(total)
while True:
x = random() * total
i = bisect(cum_weights, x)
yield values[i]

def weighted_choices(choices, N):
return list(itertools.islice(weighted_choice_iter(choices), N))

print weighted_choices(choices, 3)

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


Weighted choices

2013-09-07 Thread Jason Friedman
choices = dict()
choices["apple"] = 10
choices["pear"] = 20
choices["banana"] = 15
choices["orange"] = 25
choices["kiwi"] = 30

I want to pick sets of fruit, three in a set, where the chance of
selecting a given fruit is proportional to its weight.  In the example
above, pears should appear twice as often as apples and kiwis should
appear twice as often as bananas.

I see this solution
(http://stackoverflow.com/questions/3679694/a-weighted-version-of-random-choice)
but it is for single choices at a time.  random.sample() does not
provide a "weight" argument.

I can picture how to write this with many, many lines of code but I
have a feeling there is an elegant solution.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help - Python syntax - repeats script through subordinate folders

2013-09-07 Thread BlueFielder
Thanks guys … that too failed.

It's late here and I'm bushed.
I'll get back to this tomorrow morning.
Much appreciated.

It's probably important that I point out that I put the file ' fxp2aupreset.py 
' in the root directory (ddd) .
Just to keep things all together. Again …. I have NO idea how to do any of this.

Also …the root folder only contains the .py file and subfolders.
The subfolders contain 100's of the files that I wish to convert.

If you want to take a look at the last results ….   In my DropBox here >> 
https://dl.dropboxusercontent.com/u/65969526/Results.rtf

Good night all, and thank you again.



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


Re: Help - Python syntax - repeats script through subordinate folders

2013-09-07 Thread Chris Angelico
On Sun, Sep 8, 2013 at 1:34 PM, Michael Torrie  wrote:
> On 09/07/2013 09:09 PM, BlueFielder wrote:
>> I 'think' I did as you instructed …. but that too failed. :(
>>
>>
>> CiMac:ddd camforx$ find -type d -execdir bash -c 'cd {}; python 
>> ./fxp2aupreset.py ./ aumu Alb3 LinP vstdata' \;
>> find: illegal option -- t
>> usage: find [-H | -L | -P] [-EXdsx] [-f path] path ... [expression]
>>find [-H | -L | -P] [-EXdsx] -f path [path ...] [expression]
>
> Some versions of find require a path first.  Try this (all one line):
>
> find  . -type d -execdir bash -c 'cd {}; python ./fxp2aupreset.py ./
> aumu Alb3 LinP vstdata' \;

Thanks Michael. I'm using GNU find on Debian Linux (or on Ubuntu,
depending on which computer I test on), so I can't be sure what's
different.

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


Re: Can I trust downloading Python?

2013-09-07 Thread Dave Angel
On 7/9/2013 21:17, Aaron Martin wrote:

> Hi, I am thinking about getting a software but it requires python, so that
> brought up a few questions. Is it safe do download python, and does it come
> with spam or advertisements? If it doesn't then should I get the latest
> version? I mostly want to know if it is safe to download, because most of
> the time downloading free stuff off the internet comes with spam and all
> that, so I want to know if I can trust downloading it.
>

Python is available without ads, trojans, viruses, or other malware. 
However, the internet is a big place, and there are undoubtedly some
places which will add their own garbage to the download.

If you get Python from python.org, or from activestate.com, it'll be
safe.  Someone here will be glad to give you a link, once you identify
just what you actually need:

1) what OS are you running?  Actually, we can be pretty sure you're
running Windows, since any other common operating system would have
already included Python.  But you will need to know whether it's 32bit
or 64 bit OS.  You can run a 32bit Python on 64bit OS, but not the oter
way around.  And most people just match the bitness of Python against
the bitness of the OS.

2) What version of Python does that software you're talking about
require?  The two most lkely candidates are 2.7 or 3.3  There are
packages out there that haven't yet ported to 3.x, so you may be stuck
with 2.7.  But if the package is older, you might even need 2.6


-- 
DaveA

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


Re: Help - Python syntax - repeats script through subordinate folders

2013-09-07 Thread Michael Torrie
On 09/07/2013 09:09 PM, BlueFielder wrote:
> I 'think' I did as you instructed …. but that too failed. :( 
> 
> 
> CiMac:ddd camforx$ find -type d -execdir bash -c 'cd {}; python 
> ./fxp2aupreset.py ./ aumu Alb3 LinP vstdata' \;
> find: illegal option -- t
> usage: find [-H | -L | -P] [-EXdsx] [-f path] path ... [expression]
>find [-H | -L | -P] [-EXdsx] -f path [path ...] [expression]

Some versions of find require a path first.  Try this (all one line):

find  . -type d -execdir bash -c 'cd {}; python ./fxp2aupreset.py ./
aumu Alb3 LinP vstdata' \;
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help - Python syntax - repeats script through subordinate folders

2013-09-07 Thread BlueFielder
I 'think' I did as you instructed …. but that too failed. :( 


CiMac:ddd camforx$ find -type d -execdir bash -c 'cd {}; python 
./fxp2aupreset.py ./ aumu Alb3 LinP vstdata' \;
find: illegal option -- t
usage: find [-H | -L | -P] [-EXdsx] [-f path] path ... [expression]
   find [-H | -L | -P] [-EXdsx] -f path [path ...] [expression]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Can I trust downloading Python?

2013-09-07 Thread Michael Torrie
On 09/07/2013 07:17 PM, Aaron Martin wrote:
> Hi, I am thinking about getting a software but it requires python, so that
> brought up a few questions. Is it safe do download python, and does it come
> with spam or advertisements? If it doesn't then should I get the latest
> version? I mostly want to know if it is safe to download, because most of
> the time downloading free stuff off the internet comes with spam and all
> that, so I want to know if I can trust downloading it.

Yes if you download binaries from the official sources (python.org web
site) there is a reasonable assumption that the binary is free from
malware or viruses.  I've never heard of programmings coming with spam
before ;).  Usually that arrives unbidden in my inbox.

As for trusting python in general, I do trust the python developers, but
recent NSA revelations call just about all aspects of computing, trust,
and privacy into doubt.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Can I trust downloading Python?

2013-09-07 Thread Ben Finney
Aaron Martin  writes:

> Hi, I am thinking about getting a software but it requires python, so
> that brought up a few questions. Is it safe do download python, and
> does it come with spam or advertisements?

Python is free software, meaning that every recipient is free to improve
it and redistribute the result.

https://en.wikipedia.org/wiki/Free_software>

Free software rarely has the problems you describe – spam and
advertisements – and never has them for long, because those problems are
quickly improved (by eradicating the annoying problem), and the improved
version becomes what people share.

> If it doesn't then should I get the latest version?

The latest stable version is Python 3.3, and this version is strongly
recommended for people who will be developing with Python.

But you say that you are getting Python because you have some other
program that requires Python. Which version of Python does it require?
Download and install the latest version that is supported for the
program you are wanting to use.

> I mostly want to know if it is safe to download, because most of the
> time downloading free stuff off the internet comes with spam and all
> that, so I want to know if I can trust downloading it.

Ah, your experience is with zero-cost non-free software. Non-free
software is prone to have spam and advertisements, and many other
problems that arise from disrespect for the recipient's freedom. So your
caution is well advised.

Know that free software respects your freedom, and Python is free
software.

https://www.fsf.org/about/what-is-free-software>

Welcome, and good fortune to you in using Python!

-- 
 \ “I was trying to daydream, but my mind kept wandering.” —Steven |
  `\Wright |
_o__)  |
Ben Finney

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


Re: Help - Python syntax - repeats script through subordinate folders

2013-09-07 Thread Chris Angelico
On Sun, Sep 8, 2013 at 12:59 PM, BlueFielder  wrote:
>
> Failed:
>
> Here is the command with the results:
> 
>
> CiMac:ddd camforx$ $ find -type d -execdir bash -c 'cd {}; python 
> ./fxp2aupreset.py ./ aumu Alb3 LinP vstdata' \;
> -bash: $: command not found

Oh! Omit the dollar sign, that was representing your prompt :)

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


Re: Help - Python syntax - repeats script through subordinate folders

2013-09-07 Thread BlueFielder

Failed:

Here is the command with the results:


CiMac:ddd camforx$ $ find -type d -execdir bash -c 'cd {}; python 
./fxp2aupreset.py ./ aumu Alb3 LinP vstdata' \;
-bash: $: command not found

--

With CiMac = HD
ddd = folder on the desktop of user camforx
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help - Python syntax - repeats script through subordinate folders

2013-09-07 Thread BlueFielder
On Saturday, September 7, 2013 9:56:25 PM UTC-4, Chris Angelico wrote:

> Your initial command and path suggest you're on a Unix-like system
> 
> (these days that most likely means either Linux or Mac OS), but the
> 
> FOR command at the end is a Windows command, so that's not going to
> 
> work. However, Unix does have a find command, so that should work 

Hi Chris.
Thank you so much for your rely.
Yes…. I am doing this on a Mac OS

> 
> Do you need to run your script once for each file, or once for each
> 
> directory? Based on your use of "for /r", I'm thinking once per
> 
> directory.

I wish to run the script just once on the parent folder…. and have it run 
through all the 86 subordinate folders that reside in that parent folder. 


> $ find -type d -execdir bash -c 'cd {}; python ./fxp2aupreset.py ./
> 
> aumu Alb3 LinP vstdata' \;
> 
> 
> I'm sure there's a tidier way to do it, but this should work!

OK  I will try that as soon as I can and post the results.
FWIW: I'm not convened with 'tidy' … as I will only being coin this once.

So very kind of you to help. 
HT to you sir.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help - Python syntax - repeats script through subordinate folders

2013-09-07 Thread Chris Angelico
On Sun, Sep 8, 2013 at 11:41 AM, BlueFielder  wrote:
> 3. Then I run the script from terminal : python ./fxp2aupreset.py ./ aumu 
> Alb3 LinP vstdata
>
> It executes fine and does it's job.
>
> BUT… I have per 7600 files that are segregated into 86 folders.
> I want to keep this folder structure.
>
> So, What I need the script to do is to start at the parent folder (ddd) and 
> then go through each subordinate (child) folder and perform its task on all 
> files.
>
> I found this syntax on the web that is somehow supposed to do what I need:
>
> for /r %a in (*.fxp) do example.py "%a"
>
> BUT … I have no idea how to combine the syntax.

Your initial command and path suggest you're on a Unix-like system
(these days that most likely means either Linux or Mac OS), but the
FOR command at the end is a Windows command, so that's not going to
work. However, Unix does have a find command, so that should work for
you.

Do you need to run your script once for each file, or once for each
directory? Based on your use of "for /r", I'm thinking once per
directory.

$ find -type d -execdir bash -c 'cd {}; python ./fxp2aupreset.py ./
aumu Alb3 LinP vstdata' \;

I'm sure there's a tidier way to do it, but this should work!

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


Help - Python syntax - repeats script through subordinate folders

2013-09-07 Thread BlueFielder
I'm NOT a programmer…. but I have a little Python script that converts files, 
that are in a folder, from one format to another. 

Script = fxp2aupreset.py 

I can successfully run it from the command line:

1st, I placed the folder that I named 'ddd' on the desktop so that it's easy to 
get to.

2. Terminal : cd ~/Desktop/ddd

3. Then I run the script from terminal : python ./fxp2aupreset.py ./ aumu Alb3 
LinP vstdata

It executes fine and does it's job.

BUT… I have per 7600 files that are segregated into 86 folders.
I want to keep this folder structure.

So, What I need the script to do is to start at the parent folder (ddd) and 
then go through each subordinate (child) folder and perform its task on all 
files.

I found this syntax on the web that is somehow supposed to do what I need:

for /r %a in (*.fxp) do example.py "%a"

BUT … I have no idea how to combine the syntax. 

Could someone put this together for me please.
Something that I could just copy and paste into Terminal.
I would be most grateful. 

Thank you very much!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Can I trust downloading Python?

2013-09-07 Thread Chris Angelico
On Sun, Sep 8, 2013 at 11:17 AM, Aaron Martin
 wrote:
> Hi, I am thinking about getting a software but it requires python, so that
> brought up a few questions. Is it safe do download python, and does it come
> with spam or advertisements? If it doesn't then should I get the latest
> version? I mostly want to know if it is safe to download, because most of
> the time downloading free stuff off the internet comes with spam and all
> that, so I want to know if I can trust downloading it.

Yes, you can trust Python. If you don't like the precompiled versions,
you can simply download the source code (plain text files) and build
your own, so any advertising in it could be removed very easily - and
would thus be worthless, so nobody bothers to put any there. Open
Source is different from ad-funded software; both of them cost you no
money, but there's a complete difference in philosophy.

Definitely get the latest version (currently 3.3, soon 3.4). Python
keeps getting new features and improvements.

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


Can I trust downloading Python?

2013-09-07 Thread Aaron Martin
Hi, I am thinking about getting a software but it requires python, so that
brought up a few questions. Is it safe do download python, and does it come
with spam or advertisements? If it doesn't then should I get the latest
version? I mostly want to know if it is safe to download, because most of
the time downloading free stuff off the internet comes with spam and all
that, so I want to know if I can trust downloading it.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: file handling issues

2013-09-07 Thread Piet van Oostrum
Leo Carnovale  writes:

> Ah and one other thing!
> What is this crypto algorithm you speak of? I desperately need some sort of 
> encryption as at the moment anyone can simply open the text file and change 
> the numbers to numbers that work! 
> Where can I learn more about it?

You can google for cryptography or look in Wikipedia. Now the question
is: will this game be played through the internet or on someone's
computer? If it is run on a computer then both the program and the
keyfile will be on that computer, so whatever algorithm you use the user
can use it either to detect the password derived from the key or choose
his own password and generate the corresponding key and put it in the
file. Or even change the program and take the check out. You could use
some obfuscation to make it difficult, like they do in DRM but basically
you cannot hode it from the user. If you use an internet connection then
of course you can keep the relevant data outside of the reach of the
user.
-- 
Piet van Oostrum 
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: PEP8 79 char max

2013-09-07 Thread Serhiy Storchaka

06.09.13 06:40, Steven D'Aprano написав(ла):

PEP 8 certainly is a collection of rules. They are mandatory for new code
added to the standard library, and optional but recommended for third
party libraries.


No. They are optional but recommended for new code added to the standard 
library, and indifferent for third party libraries.


But generally there are no much reasons to not obey PEP 8.


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


Re: python script hangs when run from subprocess

2013-09-07 Thread larry.mart...@gmail.com
On Saturday, September 7, 2013 9:47:47 AM UTC-6, Nobody wrote:
> On Sat, 07 Sep 2013 03:55:02 -0700, larry.mart...@gmail.com wrote:
> 
> 
> 
> > I have a python script and when I run it directly from the command line
> 
> > it runs to completion. But I need to run it from another script. I do
> 
> > that like this: 
> 
> > 
> 
> > p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
> 
> > rv = p.wait()
> 
> > out_buf = p.stdout.read()
> 
> > 
> 
> > When I do this, wait never returns.
> 
> 
> 
> The last two statements are the wrong way around. If you're reading a
> 
> process' output via a pipe, you shouldn't wait() for it until it has
> 
> closed its end of the pipe.
> 
> 
> 
> As it stands, you have a potential deadlock. If the subprocess tries to
> 
> write more data than will fit into the pipe, it will block until the
> 
> parent reads from the pipe. But the parent won't read from the pipe until
> 
> after the subprocess has terminated, which won't happen because the
> 
> subprocess is blocked waiting for the parent to read from the pipe ...

Thanks. I reversed the order of the wait and read calls, and it no longer 
hangs. 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python script hangs when run from subprocess

2013-09-07 Thread larry.mart...@gmail.com
On Saturday, September 7, 2013 5:19:25 AM UTC-6, Peter Otten wrote:
> larry.mart...@gmail.com wrote:
> 
> 
> 
> > I have a python script and when I run it directly from the command line it
> 
> > runs to completion. But I need to run it from another script. I do that
> 
> > like this:
> 
> > 
> 
> > p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
> 
> > stderr=subprocess.STDOUT) rv = p.wait()
> 
> > out_buf = p.stdout.read()
> 
> > 
> 
> > When I do this, wait never returns. If I trace the underlying script it's
> 
> > always in the same write to stderr that never seems to complete:
> 
> > 
> 
> > write(2, "/KA22/05Feb12/Images/12063LBO003"..., 24457
> 
> > 
> 
> > I run many other scripts and commands in the same manner, and they all
> 
> > complete, it's just this one. Anyone have any ideas why this is happening,
> 
> > and how I can further debug or fix this?
> 
> 
> 
> The script writes to an OS buffer, and when that buffer is full it blocks 
> 
> forever. p.wait() in turn then waits forever for the script to terminate...
> 
> 
> 
> As a fix try
> 
> 
> 
> out_buf = subprocess.Popen(...).communicate()[0]
> 
> 
> 
> 
> 
> This uses threads or select (depending on the OS) to avoid the problem -- 
> 
> and is prominently mentionend in the documentation:
> 
> 
> 
> http://docs.python.org/2/library/subprocess.html#subprocess.Popen.wait

Thanks. I hadn't seen this. I'll check it out. 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python script hangs when run from subprocess

2013-09-07 Thread Nobody
On Sat, 07 Sep 2013 03:55:02 -0700, larry.mart...@gmail.com wrote:

> I have a python script and when I run it directly from the command line
> it runs to completion. But I need to run it from another script. I do
> that like this: 
> 
> p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
> rv = p.wait()
> out_buf = p.stdout.read()
> 
> When I do this, wait never returns.

The last two statements are the wrong way around. If you're reading a
process' output via a pipe, you shouldn't wait() for it until it has
closed its end of the pipe.

As it stands, you have a potential deadlock. If the subprocess tries to
write more data than will fit into the pipe, it will block until the
parent reads from the pipe. But the parent won't read from the pipe until
after the subprocess has terminated, which won't happen because the
subprocess is blocked waiting for the parent to read from the pipe ...


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


Re: python script hangs when run from subprocess

2013-09-07 Thread Peter Otten
larry.mart...@gmail.com wrote:

> I have a python script and when I run it directly from the command line it
> runs to completion. But I need to run it from another script. I do that
> like this:
> 
> p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
> stderr=subprocess.STDOUT) rv = p.wait()
> out_buf = p.stdout.read()
> 
> When I do this, wait never returns. If I trace the underlying script it's
> always in the same write to stderr that never seems to complete:
> 
> write(2, "/KA22/05Feb12/Images/12063LBO003"..., 24457
> 
> I run many other scripts and commands in the same manner, and they all
> complete, it's just this one. Anyone have any ideas why this is happening,
> and how I can further debug or fix this?

The script writes to an OS buffer, and when that buffer is full it blocks 
forever. p.wait() in turn then waits forever for the script to terminate...

As a fix try

out_buf = subprocess.Popen(...).communicate()[0]


This uses threads or select (depending on the OS) to avoid the problem -- 
and is prominently mentionend in the documentation:

http://docs.python.org/2/library/subprocess.html#subprocess.Popen.wait

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


python script hangs when run from subprocess

2013-09-07 Thread larry.mart...@gmail.com
I have a python script and when I run it directly from the command line it runs 
to completion. But I need to run it from another script. I do that like this:

p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
rv = p.wait()
out_buf = p.stdout.read()

When I do this, wait never returns. If I trace the underlying script it's 
always in the same write to stderr that never seems to complete:

write(2, "/KA22/05Feb12/Images/12063LBO003"..., 24457

I run many other scripts and commands in the same manner, and they all 
complete, it's just this one. Anyone have any ideas why this is happening, and 
how I can further debug or fix this?

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