Re: [Tutor] A question about using stdin/out/err vs named files

2014-10-19 Thread Adam Jensen
On 10/18/2014 02:36 PM, George R Goffe wrote:
> Hi,
> 
> When you run a python program, it appears that stdin, stdout, and stderr are 
> opened automatically.
> 
> I've been trying to find out how you tell if there's data in stdin (like when 
> you pipe data to a python program) rather 
> than in a named input file. It seems like most/all the Unix/Linux 
> commands are able to figure this out. Do you know how Python programs do this 
> or might do this?
> 
> MANY thanks for any/all help/hints/tips/suggestions,
> 
> George...

Command line argument parsing aside, perhaps something like this would
be useful:

script.py
---
#!/usr/bin/env python3
import os, stat
mode = os.fstat(0).st_mode

if stat.S_ISFIFO(mode):
 print("stdin is a pipe")
elif stat.S_ISREG(mode):
 print("stdin is a redirected file")
elif stat.S_ISCHR(mode):
 print("stdin is terminal")
else:
 print("stdin is weird")
---

$ ./script.py
stdin is terminal

$ cat script.py | ./script.py
stdin is a pipe

$ ./script.py < script.py
stdin is a redirected file

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Standard Library Performance (3.4.1)

2014-10-24 Thread Adam Jensen
I'm tinkering this evening and I've noticed that math.factorial() is
much faster than my plain python implementations.


import math

def factorial(n):
temp = 1
for k in range(0,n):
temp = temp * (n - k)
return(temp)

def fac(n):
return 1 if (n == 0) else n * fac(n-1)


>From IPython:

In [21]: %timeit factorial(9)
10 loops, best of 3: 5.31 µs per loop

In [22]: %timeit fac(9)
10 loops, best of 3: 6.86 µs per loop

In [23]: %timeit math.factorial(9)
100 loops, best of 3: 416 ns per loop


Is this kind of performance difference typical of the standard library
functions (compared to plain python user implementations)?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Standard Library Performance (3.4.1)

2014-10-24 Thread Adam Jensen

On 10/24/2014 08:01 AM, Stefan Behnel wrote:
> Alan Gauld schrieb am 24.10.2014 um 13:03:
>> Not all library modules are C based however so it doesn't
>> always apply. But they are usually optimised and thoroughly
>> debugged so it is still worth using them rather than building
>> your own.
> 
> It's worth stressing this point a bit more. Lots of people have been using
> this code, and some found bugs in it that are now fixed. This means that
> whatever implementation of similar functionality you can come up with
> yourself will most likely have more bugs and be less generally versatile.
> And even if the standard library code doesn't fit your needs, start by
> taking a deep look at what the Python Package Index (PyPI) offers instead
> of writing your own.

If the standard library is thoughtfully designed, tested, tuned and
released in a stable way then I agree, it's worth spending development
time to search and explore the library for potentially useful components
then examine the component's interface, decipher the documentation and
tinker with the component to discover its characteristics. There is some
cost in doing this (developer time and energy). Sometimes it seems like
there will be less expense in just writing a few lines of pure python
but a ~10x difference in performance settles that - the library route is
probably worth the effort.

> Developer time is much better spent reusing other people's code and helping
> to squash the remaining bugs in it than having everyone write their own
> buggy code over and over.

I haven't studied the PyPI engineering process yet but my limited
experience with just installing third party packages (e.g., pip install
scipy) makes me a bit wary about naively using these packages as
foundational components.

BTW - I did manage to get all of the scipy dependencies built and
installed but damn, that was a grueling process. I'm surprised it
wasn't automated and thoroughly shocked by the number of compilation
warnings. (I once heard someone describe the current state of software
engineering as being like "building a skyscraper out of bananas").
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] subprocess.Popen basics

2014-10-27 Thread Adam Jensen
Hi, I'm exploring Popen today and I seem to be having some trouble
deciphering the [documentation][1].

[1]: docs.python.org/3/library/subprocess.html#popen-constructor

In this example (below), I expect to start a shell script as a separate
process, send a line of text through a pipe (to the shell script's
stdin) then read the shell script's response (a line of text) from
another pipe (attached to the shell script's stdout).

-
#!/usr/bin/env python3.4

import subprocess as sp

parrot = sp.Popen(['./parrot.sh'],
stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE,
universal_newlines=True, start_new_session=True)

parrot.stdin.write('Pushing up daisies.\n')
print('Parrot said: ', parrot.stdout.readline())
parrot.kill()
-

Where "parrot.sh" is:
-
#!/bin/bash
while true
do
read foo
echo "Squawk: $foo"
done
-

It hangs at the print statement and, from the sound of the fans in the
computer, I suspect it spirals off into an infinite loop somewhere /
somehow. Does anyone have any ideas about what it is that I might be
misunderstanding?

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] subprocess.Popen basics

2014-10-27 Thread Adam Jensen

On 10/27/2014 03:40 PM, David Abbott wrote:
>> It hangs at the print statement and, from the sound of the fans in the
>> computer, I suspect it spirals off into an infinite loop somewhere /
>> somehow. Does anyone have any ideas about what it is that I might be
>> misunderstanding?
> 
> Works here.
> 
> david@heater ~/python_practice $ ./subprocess_pipe.py
> Parrot said:  Squawk: Pushing up daisies.
> 
> david@heater ~/python_practice $ python
> Python 3.3.5 (default, Oct  2 2014, 07:55:01)
> [GCC 4.7.3] on linux
> Type "help", "copyright", "credits" or "license" for more information.
 quit()
> 

That's a bit bizarre. I too have the execution bit set for both the
python script and the shell script but the same (no joy) behavior occurs
on both:

Python 3.4.1 (default, Jun 20 2014, 01:51:12)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux

Python 3.4.1 |Anaconda 2.1.0 (32-bit)| (default, Sep 10 2014, 17:21:42)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux

$ ./subprocess_pipe.py
# It hangs here until Ctrl-c
^CTraceback (most recent call last):
  File "./subprocess_pipe.py", line 10, in 
print('Parrot said: ', parrot.stdout.readline())
KeyboardInterrupt

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] subprocess.Popen basics

2014-10-27 Thread Adam Jensen
On 10/27/2014 07:12 PM, Alan Gauld wrote:
> On 27/10/14 18:24, Adam Jensen wrote:
>> It hangs at the print statement and, from the sound of the fans in the
>> computer, I suspect it spirals off into an infinite loop somewhere
> 
> It works fine on my Lubuntu 14 with Python3.4.
> 
> How exactly are you running it? If I don't make parrot.sh executable
> I get a permission error. But definitely no hang-ups.
> 
> Are you using an IDE by any chance? That can often do strange things.
> 

Thanks for giving it a try. The mailing list moderation delay is making
an interactive conversation a bit difficult. My current theory is that
there is something wrong with the python3 installation on my CentOS-6.5
machine. I'm rebuilding from source now and I will definitely have a
close look at the build logs this time and, of course, the test suite logs.

When writing scripts, I've been using gedit or vi (depending on the
machine I'm using), or I interact directly with the python interpreter
(through a terminal). I've tinkered with IPython and Spyder but for now
(while learning) they mostly just seem to introduce an unnecessary layer
of complexity and obfuscation.

Summary: The python and shell scripts that I posted seem to be okay (in
the sense that they do what I expected them to do). I got them to run on
OpenBSD without any problems. I don't know why my python3.4 build on
CentOS6.5 is so wonky. After getting a clean python build from source
(sometime tonight or tomorrow), hopefully the problem will go away. If
not, I guess it will become a bug hunt.




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] subprocess.Popen basics

2014-10-27 Thread Adam Jensen
On 10/27/2014 03:40 PM, David Abbott wrote:
>> It hangs at the print statement and, from the sound of the fans in the
>> computer, I suspect it spirals off into an infinite loop somewhere /
>> somehow. Does anyone have any ideas about what it is that I might be
>> misunderstanding?
> 
> Works here.
> 
> david@heater ~/python_practice $ ./subprocess_pipe.py
> Parrot said:  Squawk: Pushing up daisies.
> 
> david@heater ~/python_practice $ python
> Python 3.3.5 (default, Oct  2 2014, 07:55:01)
> [GCC 4.7.3] on linux
> Type "help", "copyright", "credits" or "license" for more information.
 quit()
> 

It works on OpenBSD.

Python 3.3.2 (default, Mar  4 2014, 09:37:25)
[GCC 4.2.1 20070719 ] on openbsd5

$ ./subprocess_pipe.py
Parrot said:  Squawk: Pushing up daisies.

Thanks for the sanity check, David. I guess there is something wrong
with my python3 build on CentOS.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] subprocess.Popen basics

2014-10-27 Thread Adam Jensen

On 10/27/2014 09:31 PM, Alan Gauld wrote:
> On 27/10/14 20:26, Adam Jensen wrote:
> 
>> That's a bit bizarre. I too have the execution bit set for both the
>> python script and the shell script but the same (no joy) behavior occurs
>> on both:
> 
>> $ ./subprocess_pipe.py
> 
> Its a long shot but try explicitly invoking the interpreter:
> 
> $ python3 ./subprocess_pipe.py
> 
> The shebang line should make in unnecessary but you
> never know...
> 

What's weird is that I have two different python3.4 installations on
this CentOS-6.5 machine and both have the same behavior (script hangs
until Ctrl+C).

I built this one (/opt/bin/python3.4) from source:

$ /opt/bin/python3.4 subprocess_pipe.py
^CTraceback (most recent call last):
  File "subprocess_pipe.py", line 10, in 
print('Parrot said: ', parrot.stdout.readline())
KeyboardInterrupt


But this one (~/anaconda3/bin/python3.4) was a binary distribution (if I
recall correctly):

$ ~/anaconda3/bin/python3.4 subprocess_pipe.py
^CTraceback (most recent call last):
  File "subprocess_pipe.py", line 10, in 
print('Parrot said: ', parrot.stdout.readline())
KeyboardInterrupt

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] subprocess.Popen basics

2014-10-28 Thread Adam Jensen
Update:

On 10/27/2014 09:50 PM, Adam Jensen wrote:
> What's weird is that I have two different python3.4 installations on
> this CentOS-6.5 machine and both have the same behavior (script hangs
> until Ctrl+C).
> 
> I built this one (/opt/bin/python3.4) from source:
...
> But this one (~/anaconda3/bin/python3.4) was a binary distribution (if I
> recall correctly):

This ^ undermines the "build problem" theory. I found a little blurb in
the documentation about bufsize:

-
bufsize will be supplied as the corresponding argument to the open()
function when creating the stdin/stdout/stderr pipe file objects:

* 0 means unbuffered (read and write are one system call and can return
short)
* 1 means line buffered (only usable if universal_newlines=True i.e., in
a text mode)
* any other positive value means use a buffer of approximately that size
* negative bufsize (the default) means the system default of
io.DEFAULT_BUFFER_SIZE will be used.
-

Some tinkering:

-
#!/usr/bin/env python3.4

import subprocess as sp

parrot = sp.Popen(['./parrot.sh'], bufsize=0,
stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE,
universal_newlines=True, start_new_session=True)

parrot.stdin.write('Pushing up daisies.\n')
print('Parrot said: ', parrot.stdout.readline())
parrot.kill()
-

And I get these results (on CentOS-6.5-x86):

| bufsize | results |
|-+-|
| default | hangs   |
|  -1 | hangs   |
|   0 | works   |
|   1 | hangs   |
| >=2 | works   |

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] subprocess.Popen basics

2014-10-28 Thread Adam Jensen

On 10/28/2014 02:32 PM, Alan Gauld wrote:
> I tried -1 and 1 on my Lubuntu and it still works fine.
> Definitely weird, it begins to look like a CentOS build issue
> but what is CentOS doing different to Lubuntu/Suse/OpenBSD etc?
> 
> From memory CentOS is basically a free version of Red Hat
> Enterprise Linux? I can't see why that would be significantly
> different in stdin/out behaviour.
> 
> weirder and weirder

Yes, CentOS is a RHEL clone. Scientific-Linux is another RHEL clone
(assembled and maintained by several of the national labs). I'm fairly
certain that Python is in heavy use at the labs so it seems especially
weird that there is something so wonky going on with it on that
platform. This is what I've discovered so far:

| | CentOS-6.5   | OpenBSD-5.5  | DragonFly-3.8.2 |
| bufsize | Python-3.4.1 | Python-3.3.2 | Python-3.3.3|
|-+--+--+-|
| default | hangs| works| works   |
| -1  | hangs| works| works   |
| 0   | works| works| works   |
| 1   | hangs| works| works   |
| >=2 & <(# of bytes) | works| works| works   |
| >=(# of bytes)  | hangs| works| works   |

(The number of bytes in 'Pushing up daisies.\n' is 20).

io.DEFAULT_BUFFER_SIZE == 8192 on all three platforms.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] subprocess.Popen basics

2014-10-28 Thread Adam Jensen

On 10/28/2014 04:27 PM, Todd wrote:
> Centos has SELinux enabled by default.  I dont know if SELinux is
> causing your problem, but it is always worth looking at.
> 
> SELinux can keep a process from accessing files or executing another
> process. 
> 
> Try temporarily disabling  SELinux by running setenforce=0 as root. 
> Then see if python does what you expect.

Yep, that occurred to me too. Earlier today I set 'SELINUX=disabled' in
/etc/selinux/config and did a 'sudo touch /.autorelabel' then rebooted.
No Joy, same behavior from subprocess.Popen().



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Dividing a float derived from a string

2014-11-20 Thread Adam Jensen
#!/usr/bin/env python3.4
good = False
s = input('Enter a number: ')
a = s.split('.')
n = len(a)
if n <= 2:
for y in a:
if y.isdigit():
good = True
else:
good = False
exit
else:
good = False
if good:
num = float(s)
print(num * 12)
else:
print('neener neener')


On Thu, 20 Nov 2014 21:20:27 +
Stephanie Morrow  wrote:

> Hi there,
> 
> I have been posed with the following challenge:
> 
> "Create a script that will ask for a number. Check if their input is a
> legitimate number. If it is, multiply it by 12 and print out the result."
> 
> I was able to do this with the following code:
> 
> input = raw_input("Insert a number: ")
> if input.isdigit():
> print int(input) * 12
> else:
> print False
> 
> *However*, a colleague of mine pointed out that a decimal will return as
> False.  As such, we have tried numerous methods to allow it to divide by a
> decimal, all of which have failed.  Do you have any suggestions?
> Additionally, we are using 2.7, so that might change your answer.
> 
> Thank you in advance for any help you can provide!
> 
> -Stephanie
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Data chart

2014-11-20 Thread Adam Jensen
import fileinput

def parseLine(a):
x = a.split('/')
b = x[1].split(':')
c = b[0].split('.')
y = c[0]
z = int(b[2])
return x[0], y, z

print('{:>4}{:>10}{:>8}{:>8}'.format('','canofica','lnvd','msd'))

data = [0, 0, 0]
prevDate = "None"

for line in fileinput.input():
thisDate, name, value = parseLine(line)
if (thisDate != prevDate): 
if (prevDate != "None"):
print('{:>4}{:>10}{:>8}{:>8}'.format(prevDate,
  data[0],data[1],data[2]))
prevDate = thisDate
if name == "canofica":
data[0] = value
elif name == "lnvd":
data[1] = value
elif name == "msd":
data[2] = value
print('{:>4}{:>10}{:>8}{:>8}'.format(prevDate,data[0],data[1],data[2]))
fileinput.close()

From: Tutor [mailto:tutor-bounces+hanzer=riseup@python.org] On Behalf Of 
Mamy Rivo DIANZINGA
Sent: Wednesday, November 19, 2014 11:42 AM
To: tutor@python.org
Subject: [Tutor] Data chart

Good morning Sir. Excuse me to bother you but i was wondering if you can help 
me, please Sir. 
I am looking for a script (in python or fortran...) which can turn the data 
from Iter.dat, that i joined for you, into a chart like this:

   canofica   lnvdmsd
10_2 ...  ...
 9_1 ...  ...


I hope i do not exagerate, and i will be very grateful to you if you can help 
me, for any script in python or fortran. Thank in advance.
Best regards.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Dividing a float derived from a string

2014-11-20 Thread Adam Jensen
On Thu, 20 Nov 2014 21:20:27 +
Stephanie Morrow  wrote:

> Hi there,
> 
> I have been posed with the following challenge:
> 
> "Create a script that will ask for a number. Check if their input is a
> legitimate number. If it is, multiply it by 12 and print out the result."
> 
> I was able to do this with the following code:
> 
> input = raw_input("Insert a number: ")
> if input.isdigit():
> print int(input) * 12
> else:
> print False
> 
> *However*, a colleague of mine pointed out that a decimal will return as
> False.  As such, we have tried numerous methods to allow it to divide by a
> decimal, all of which have failed.  Do you have any suggestions?
> Additionally, we are using 2.7, so that might change your answer.
> 
> Thank you in advance for any help you can provide!
> 
> -Stephanie

How about using a floating point type cast to convert the string to a number 
within a try/except block? Maybe something like this:

try:
x = float(input("Enter a number: "))
print(x * 12)
except ValueError:
print("Not a valid number.")

A little enhancement might involve removing any spaces from the string so 
something like '5.6 e -3' will also be valid input. Like this:

x = float(input("Enter a number: ").replace(' ',''))
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Dividing a float derived from a string

2014-11-21 Thread Adam Jensen
> -Original Message-
> From: Tutor [mailto:tutor-bounces+hanzer=riseup@python.org] On
> Behalf Of Alan Gauld
> Sent: Thursday, November 20, 2014 7:24 PM

> But that's considered bad practice, it's better to put the valid errors
only in
> the except line like this:
> 
> try:
>  print float(input)*12
> except TypeError, ValueError:
>  print False
> 
> So now Python will look out for any ValueErrors and TypeErrors during the
> first print operation and if it finds one will instead print False. Any
other kind
> of error will produce the usual Python error messages.
> 
> You may not have come across try/except yet, but its a powerful technique
> for dealing with these kinds of issues.

I'm browsing the built-in
[exceptions](docs.python.org/3.4/library/exceptions.html) and reading about
the [float cast](docs.python.org/3.4/library/functions.html#float) and it
seemed like it might be a good idea to catch overflows, so:

try:
print(float(input('Enter a number: ').replace(' ','')) * 12)
except ValueError:
print('Not a valid number.')
except OverflowError:
print('Too big!')
except EOFError:
print('\nGoodbye.')

I guess TypeError isn't necessary since input() returns a string and that's
a valid type for float(). EOFError is there to handle ^D gracefully.
Curiously, a float cast on a very large number represented as a string
returns 'inf' rather than raising an overflow error. I didn't expect that. 




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Memory management in Python

2014-12-09 Thread Adam Jensen
On Wed, 26 Nov 2014 14:08:53 +
Raúl Cumplido  wrote:

> This web is quite useful to visualize what is happening:
> http://www.pythontutor.com/visualize.html#mode=edit
> 

Very nifty web app, thanks for the link!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need help!

2014-12-13 Thread Adam Jensen
On Fri, 12 Dec 2014 07:46:05 -0500
Jagannath Ramanan  wrote:

> My name is jag. I need little bit of help understanding something. I have a
> vncserver running at the background in redhat. My client is lubuntu where
> im using python.
> 
> For some reason the communication is only possible between them is to send
> custom TCP/IP messages. Im not a hardcore developers. I can do scripts and
> i have used gtk python for front ends etc.
> 
> *The TCP /IP message length is:*
> 
> TCP/IP: 37 bytes of message + 16 bytes of header = 53 bytes Raw Serial: 37
> bytes message + 16 bytes of header + 2 bytes of trailer = 55 bytes
> I have no idea how i would code something like that in python to make it
> talk to the server.

When you hear hoofbeats, think of horses not zebras.

It's possible you only need to open a TCP port in the RedHat machine's firewall:

/sbin/iptables -I INPUT 1 -p tcp -d ${ip_of_lubuntu} --dport ${port} -j ACCEPT

For experimentation and development, it might be best to select a port number 
within the range 49152–65535.

http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] lottery problem (Was Re: (no subject))

2014-12-18 Thread Adam Jensen
On Fri, 19 Dec 2014 00:55:49 +
Alan Gauld  wrote:

> You could have used a list instead of all the
> individual variables
> 
> line[0] = ...
> line[1] = ...
> 
> But then you could get clever and use a loop:
> 
> while lines != 0:
> start = 1
> period = 7
> for lineNum in range(7):
> line[lineNum] = random(start,period)
> start += period
> period += period
> print (*line)
> lines -=1
> 

A list comprehension might be fun. 
https://docs.python.org/3.4/tutorial/datastructures.html#list-comprehensions

For example:

>>> [random.randint(x,x+6) for x in range(1,50,7)]
[4, 9, 15, 27, 33, 36, 49]

And to build the 'lines' list (although, this is getting rather ugly):

>>> lines = [[random.randint(x,x+6) for x in range(1,50,7)] for i in range(7)]
>>> lines
[[2, 13, 18, 27, 35, 37, 47], [1, 11, 21, 24, 34, 37, 49], [7, 12, 16, 24, 29, 
36, 44], [4, 9, 16, 22, 32, 37, 46], [2, 13, 20, 22, 29, 40, 46], [7, 14, 19, 
26, 35, 42, 43], [4, 12, 16, 22, 34, 40, 46]]

It might also be a good idea to execute random.seed() before calling randint() 
- https://docs.python.org/3.4/library/random.html#random.seed
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] lottery problem (Was Re: (no subject))

2014-12-18 Thread Adam Jensen
On Thu, 18 Dec 2014 20:27:03 -0500
Adam Jensen  wrote:

> And to build the 'lines' list (although, this is getting rather ugly):
> 
> >>> lines = [[random.randint(x,x+6) for x in range(1,50,7)] for i in range(7)]

Oops, in the context of the original program this might make more sense if 
written as:

data = [[random.randint(x,x+6) for x in range(1,50,7)] for i in range(lines)]

Side note: if one were to only import specific functions from a module, would 
the load time and memory consumption be smaller? Example, is:

from random import randint, seed

smaller and faster than:

import random


Side side note: since 'i' isn't being used, is there a way to loop (within the 
list comprehension) without the 'i'? For example, to generate three random 
numbers:

[randint(1,10) for i in range(3)]  # This works. 
[randint(1,10) for range(3)]  # This does not work. 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] lottery problem (Was Re: (no subject))

2014-12-19 Thread Adam Jensen
On Fri, 19 Dec 2014 10:32:15 +0100
Peter Otten <__pete...@web.de> wrote:

> Basically
> 
> from random import randint, seed
> 
> is equivalent to 
> 
> import random
> randint = random.randint
> seed = random.seed
> del random
> 
> From that you can deduce that the whole random module is loaded into memory 
> in both cases. A small speed advantage may be caused when the attribute 
> lookup is avoided in a tight loop

Thanks for the clarification, that's really helpful. So I guess the import 
style is more about name space management than controlling the details of what 
gets loaded...

> or -- if you go back to the original problem -- with some effort:
> 
> >>> N = 3
> >>> numpy.random.randint(1, 10, N) + numpy.arange(0, N*10, 10)
> array([ 5, 11, 27])
> 
> In return the latter is likely significantly more efficient for large N than 
> the generic list comprehension.

Ha! That's fun. Seven seems to be a magic number in the original problem, so 
maybe a little tweak:

import numpy as np
N=7
(np.random.randint(1,N,N) + np.arange(0,N*N,N)).tolist()

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Learning to program, not code.

2014-12-21 Thread Adam Jensen
On Sun, 21 Dec 2014 21:15:43 +1100
Ben Finney  wrote:

> Use a distributed version control system
> (Mercurial is good and is written in Python)


I'm beginning to really appreciate [fossil](http://fossil-scm.org/). 

Re: "Learning to program, not code". Is that like learning to think rather than 
(or before) learning to express your thoughts? Hmm, I'll have to think about 
that...

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] My Query - How to implement multi threading with remote execution capability in python to achieve parallel processing

2014-12-23 Thread Adam Jensen
This thread is hilarious. Thanks for the chuckle. 

http://www.ignyte.ms/whitepapers/LayersOf%20HumanValuesInStrategy.pdf
http://www.principiadiscordia.com/downloads/04%20Prometheus%20Rising.pdf


On Mon, 22 Dec 2014 11:27:01 +
Vishwas Pathak  wrote:

> I am working building on developing an automation framework for my product . 
> I need to implement multi-threading having a pool of thread where each thread 
> will be executed different set of test cases on remote windows machines. 

Perhaps you could use, scavenge from, or find inspiration in one of these 
projects?

[xdist: pytest distributed testing plugin](http://pytest.org/latest/xdist.html)
[distributed-nose](https://pypi.python.org/pypi/distributed-nose/0.1.2)

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] print() invalid syntax error

2014-12-23 Thread Adam Jensen
On Tue, 23 Dec 2014 18:27:10 +
stuart kurutac  wrote:

> finish = (int(input("Finish: "))

The parenthesis aren't balanced. I would have written it as:

finish = int(input("Finish: "))

but something like this should also work:

finish = (int(input("Finish: ")))

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor