Re: [Tutor] code to generate my own text captchas

2012-10-25 Thread Alan Gauld

On 24/10/12 22:05, Tsila Hassine wrote:

Hello all,
I am looking for simple python code that will take a given string and
distort it, captcha like. it is for artistic purposes, so no
verification required. I just need the image q text distortion code.


A Google search for 'python captcha generator' threw up several options 
including one on pypi:


http://pypi.python.org/pypi/collective.captcha


I don't know if it does exactly what you want but there are others.

HTH



--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


[Tutor] Help on Remote File Copy Exection

2012-10-25 Thread Arumugam N
Hi All,

First of the big thanks and congrats for managing such a brilliant online
community. I am new to Python and have started getting the taste of python
on my day to day work.

I have a requirement and i am trying to solve it using python.

I am from QA. Here is what i do daily and wanted to do with python
automatically.

1. Revert the snapshot of a VM used for testing. - i have automated using
pysphere
2. Copy the build from share location to the VM - here i can have a python
script run from the VM but is it possible to run it remotely? for example.
if i run the script from Machine A, it should revert the snapshot of
machine B and copy the build to Machine B from shared location.
3. Run the installer and clikc Next Button of the installation GUI. - Any
idea how to automate this ?


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


Re: [Tutor] Help on Remote File Copy Exection

2012-10-25 Thread Oscar Benjamin
On 25 October 2012 10:26, Arumugam N aru1...@gmail.com wrote:
 Hi All,

 First of the big thanks and congrats for managing such a brilliant online
 community. I am new to Python and have started getting the taste of python
 on my day to day work.

 I have a requirement and i am trying to solve it using python.

 I am from QA. Here is what i do daily and wanted to do with python
 automatically.

 1. Revert the snapshot of a VM used for testing. - i have automated using
 pysphere
 2. Copy the build from share location to the VM - here i can have a python
 script run from the VM but is it possible to run it remotely? for example.
 if i run the script from Machine A, it should revert the snapshot of machine
 B and copy the build to Machine B from shared location.
 3. Run the installer and clikc Next Button of the installation GUI. - Any
 idea how to automate this ?

These questions are probably more suited to a different mailing list
as this one is predominantly for helping in learning the elementary
aspects of programming in Python.

If you can perhaps ask a more specific Python question it might be
appropriate here. Otherwise I suggest that you either:
a) Ask on the pysphere mailing list (I have no idea what pysphere is
so this may not be appropriate)
b) Use a search engine to find a project that already does what you want
c) Ask for general help on python-list where people may know of a good
way to do what you want in Python


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


Re: [Tutor] For - if - else loop; print selective output

2012-10-25 Thread Asokan Pichai
On Wed, Oct 24, 2012 at 09:27:30PM +0500, Saad Javed wrote:
 Hi,
 
 a = [['jimmy', '25', 'pancakes'], ['tom', '23', 'brownies'], ['harry',
 '21', 'cookies']]
 for i in a:
 if (i[1] == '25' or i[1] == '26'):
 print 'yes'
 else:
 print 'Not found'
 
 This prints:
 yes
 not found
 
 I want it to print yes for each positive match but nothing for a negative
 match. However if all matches are negative, I want it to print Not found
 once (That bit the code already does). I do I get it to print yes only in
 a mix result situation?
 
 Saad

I am sure you have some answer(s) already. I wanted to add a different solution

a = [['jimmy', '25', 'pancakes'], ['tom', '23', 'brownies'], ['harry', '21', 
'cookies']]
matches = [ i[1] in ('25', '26') for i in a ] ]
if not any(matches):
  print Not found
else:
for match in matches:
if match:
   print Yes


Asokan Pichai

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


Re: [Tutor] Help on Remote File Copy Exection

2012-10-25 Thread Alan Gauld

On 25/10/12 10:26, Arumugam N wrote:


1. Revert the snapshot of a VM used for testing. - i have automated
using pysphere
2. Copy the build from share location to the VM - here i can have a
python script run from the VM but is it possible to run it remotely? for
example. if i run the script from Machine A, it should revert the
snapshot of machine B and copy the build to Machine B from shared location.


This should be possible using the same commands you'd use if done from a 
command line. Or if the VM is accessible via the network you might be 
able to do it that way. But given we have no information about the 
environment - what OS, what VM etc? Its impossible to say. Its also a 
bit off the normal topics for the tutor lkist



3. Run the installer and clikc Next Button of the installation GUI. -
Any idea how to automate this ?


subprocess can probably run  the installer, clicking a GUI button will 
depend on the OS. For example in windows you could use ctypes to access 
the win32 API to send a mouse click message to the installer app... not 
trivial, but not impossible either.


No idea how you'd do it on *nix or MacOS.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


[Tutor] Reading from a seperate file

2012-10-25 Thread myles broomes

I'm trying to code a program that retrieves data from a seperate file but 
according to my program, the seperate file is empty when I know it clearly 
isn't. It's a txt file and here are its contents: 120
74
57
44
12
I thought that maybe the problem was the code I had written but even when I try 
and read from the file in an interactive session in the Python Shell, it does 
the same thing. I open it in read mode and assign to a variable like so: 
scoresFile=open('highScores.txt','r') But whenever I try to say read from it: 
scoresFile.read() It comes up with a blank string: ' ' Can anyone help me?  
Myles Broomes___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Reading from a seperate file

2012-10-25 Thread Joel Goldstick
On Thu, Oct 25, 2012 at 2:15 PM, myles broomes
mylesbroo...@hotmail.co.uk wrote:

 I'm trying to code a program that retrieves data from a seperate file but
 according to my program, the seperate file is empty when I know it clearly
 isn't. It's a txt file and here are its contents:

 120
 74
 57
 44
 12

 I thought that maybe the problem was the code I had written but even when I
 try and read from the file in an interactive session in the Python Shell, it
 does the same thing. I open it in read mode and assign to a variable like
 so:

 scoresFile=open('highScores.txt','r')

 But whenever I try to say read from it:

 scoresFile.read()

 It comes up with a blank string:

 ' '

 Can anyone help me?


 Myles Broomes

check your spelling.  check that you are in the same directory when
your program runs.


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


Re: [Tutor] Reading from a seperate file

2012-10-25 Thread Mark Lawrence

On 25/10/2012 19:15, myles broomes wrote:


I'm trying to code a program that retrieves data from a seperate file but 
according to my program, the seperate file is empty when I know it clearly 
isn't. It's a txt file and here are its contents: 120
74
57
44
12
I thought that maybe the problem was the code I had written but even when I try 
and read from the file in an interactive session in the Python Shell, it does 
the same thing. I open it in read mode and assign to a variable like so: 
scoresFile=open('highScores.txt','r') But whenever I try to say read from it: 
scoresFile.read() It comes up with a blank string: ' ' Can anyone help me?  
Myles Broomes  



Works fine for me.

c:\Users\Mark\MyPythontype highScores.txt
120
74
57
44
12
c:\Users\Mark\MyPythontype mytest.py
from __future__ import print_function, division
scoresFile=open('highScores.txt','r')
print(scoresFile.read())
c:\Users\Mark\MyPythonpy -3 mytest.py
120
74
57
44
12

So unless you show us exactly what you've tried we'll find it difficult 
if not impossible to diagnose your problem.


--
Cheers.

Mark Lawrence.

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


Re: [Tutor] Reading from a seperate file

2012-10-25 Thread Mark Lawrence

On 25/10/2012 19:26, Joel Goldstick wrote:

On Thu, Oct 25, 2012 at 2:15 PM, myles broomes
mylesbroo...@hotmail.co.uk wrote:


I'm trying to code a program that retrieves data from a seperate file but
according to my program, the seperate file is empty when I know it clearly
isn't. It's a txt file and here are its contents:

120
74
57
44
12

I thought that maybe the problem was the code I had written but even when I
try and read from the file in an interactive session in the Python Shell, it
does the same thing. I open it in read mode and assign to a variable like
so:

scoresFile=open('highScores.txt','r')

But whenever I try to say read from it:

scoresFile.read()

It comes up with a blank string:

''

Can anyone help me?


Myles Broomes


check your spelling.  check that you are in the same directory when
your program runs.




If you've misspelt the filename or you're in the wrong directory surely 
you'll get something like this depending on your OS.


c:\Users\MarkMyPython\mytest.py
Traceback (most recent call last):
  File C:\Users\Mark\MyPython\mytest.py, line 2, in module
scoresFile=open('highScores.txt','r')
IOError: [Errno 2] No such file or directory: 'highScores.txt'

--
Cheers.

Mark Lawrence.

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


Re: [Tutor] Help on Remote File Copy Exection

2012-10-25 Thread Prasad, Ramit
Arumugam N wrote:
 Hi All,
 
 First of the big thanks and congrats for managing such a brilliant online 
 community. I am new to Python and have
 started getting the taste of python on my day to day work.
 
 I have a requirement and i am trying to solve it using python.
 
 I am from QA. Here is what i do daily and wanted to do with python 
 automatically.
 
 1. Revert the snapshot of a VM used for testing. - i have automated using 
 pysphere

I would ask this on the pysphere discussion group: 
https://groups.google.com/forum/?fromgroups#!forum/pysphere

 2. Copy the build from share location to the VM - here i can have a python 
 script run from the VM but is it
 possible to run it remotely? for example. if i run the script from Machine A, 
 it should revert the snapshot of
 machine B and copy the build to Machine B from shared location.

You should be able to handle file transfer through some libraries,
but it depends on the OSes involved and how they are setup. You can
always transfer via FTP or SSH. If Machine B is a *nix box, you can
SSH in with Paramiko and then use scp/sftp/rsync to transfer and
run commands to restart the VM on machine B. 

 3. Run the installer and clikc Next Button of the installation GUI. - Any 
 idea how to automate this ?

No idea, sorry.

 
 
 Regards,
 Aru

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] For - if - else loop; print selective output

2012-10-25 Thread Prasad, Ramit
eryksun wrote:
 On Wed, Oct 24, 2012 at 3:24 PM, Alan Gauld alan.ga...@btinternet.com wrote:
  On 24/10/12 18:49, eryksun wrote:
 
  Using in ['25', '26'] also checks a compiled tuple constant:
 
compile(x in ['25', '26'], '', 'eval').co_consts
   ('25', '26', ('25', '26'))
 
  3.x adds frozenset support:
 
compile(x in {'25', '26'}, '', 'eval').co_consts
   ('25', '26', frozenset({'25', '26'}))
 
 
  I confess I don't know what that means!
  And if I don't I doubt if the OP will either.
  Can you explain the above in English please?
 
 Sorry, I was showing that the compiler (at least for CPython) special
 cases in and not in comparisons when the right-hand operand is a
 list literal of constants. Instead of going to the trouble of building
 a list every time the code runs, it uses a tuple that's stored in the
 code object's co_consts attribute.
 
 In Python 3, this optimization was extended to set literals constants.
 I guess I should show that in more detail by disassembling a function
 instead of manually compiling code.
 
 First in Python 2.7.3:
 
  def func(x):
 ... return x in {1, 2, 3}
 
  dis.dis(func)
   2   0 LOAD_FAST0 (x)
   3 LOAD_CONST   1 (1)
   6 LOAD_CONST   2 (2)
   9 LOAD_CONST   3 (3)
  12 BUILD_SET3
  15 COMPARE_OP   6 (in)
  18 RETURN_VALUE
 
 Each time the function is called it has to build a set (BUILD_SET)
 from the constants 1, 2, and 3. Compare that to Python 3.2.3:
 
  dis.dis(func)
   2   0 LOAD_FAST0 (x)
   3 LOAD_CONST   4 (frozenset({1, 2, 3}))
   6 COMPARE_OP   6 (in)
   9 RETURN_VALUE
 
 Here the compiler stored a frozenset in the code object. So it only
 has to load the frozenset on the stack instead of building a new set
 each time the function is called.
 
 compile:
 http://docs.python.org/py3k/library/functions.html#compile
 
 frozenset:
 http://docs.python.org/py3k/library/functions.html#func-frozenset
 
 code objects:
 http://docs.python.org/py3k/reference/datamodel.html#index-51
 
 The optimization is in peephole.c, PyCode_Optimize/tuple_of_constants:
 
 http://hg.python.org/cpython/file/3d0686d90f55/Python/peephole.c#l469
 http://hg.python.org/cpython/file/3d0686d90f55/Python/peephole.c#l25

Thank you for the very detailed (and referenced) clarification. 
I understood the gist of what you were trying to say in your
initial post, but this certainly helps fill in the details.

Do you happen to know offhand if there is a difference between 
`in list` vs. `in tuple` vs. `in set`? 

Ramit Prasad


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Reading from a seperate file

2012-10-25 Thread bob gailer

On 10/25/2012 2:15 PM, myles broomes wrote:
[snip]

Try open('highScores.txt, 'rb').

--
Bob Gailer
919-636-4239
Chapel Hill NC

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


Re: [Tutor] For - if - else loop; print selective output

2012-10-25 Thread eryksun
On Thu, Oct 25, 2012 at 3:46 PM, Prasad, Ramit
ramit.pra...@jpmorgan.com wrote:

 Do you happen to know offhand if there is a difference between
 `in list` vs. `in tuple` vs. `in set`?

The in comparison (__contains__ method) is equivalent for list and
tuple. It has to search through the sequence item by item, which makes
it an O(n) operation. On the other hand, a set/dict uses the hash() of
an object to map it to a known location in a table (performance
degrades if there are many collisions). On average, you can check if a
set/dict contains an item in constant time, i.e. O(1). The amortized
worst case is O(n).
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] For - if - else loop; print selective output

2012-10-25 Thread Oscar Benjamin
On 25 October 2012 21:16, eryksun eryk...@gmail.com wrote:
 On Thu, Oct 25, 2012 at 3:46 PM, Prasad, Ramit
 ramit.pra...@jpmorgan.com wrote:

 Do you happen to know offhand if there is a difference between
 `in list` vs. `in tuple` vs. `in set`?

 The in comparison (__contains__ method) is equivalent for list and
 tuple. It has to search through the sequence item by item, which makes
 it an O(n) operation. On the other hand, a set/dict uses the hash() of
 an object to map it to a known location in a table (performance
 degrades if there are many collisions). On average, you can check if a
 set/dict contains an item in constant time, i.e. O(1). The amortized
 worst case is O(n).

Why do you say *amortized* worst case? Is there an occasional worse
than O(n) operation that is insignificant when amortised?

At first I assumed that was a slip of the tongue but you generally
seem to know an incredible amount about the implementation of CPython,
so now I'm curious.


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


Re: [Tutor] For - if - else loop; print selective output

2012-10-25 Thread Prasad, Ramit
eryksun wrote:
 On Thu, Oct 25, 2012 at 3:46 PM, Prasad, Ramit
 ramit.pra...@jpmorgan.com wrote:
 
  Do you happen to know offhand if there is a difference between
  `in list` vs. `in tuple` vs. `in set`?
 
 The in comparison (__contains__ method) is equivalent for list and
 tuple. It has to search through the sequence item by item, which makes
 it an O(n) operation. On the other hand, a set/dict uses the hash() of
 an object to map it to a known location in a table (performance
 degrades if there are many collisions). On average, you can check if a
 set/dict contains an item in constant time, i.e. O(1). The amortized
 worst case is O(n).

Sorry, I should have stated that I meant with regards to the 
optimization that loads these as a literal constant.

Ramit Prasad



This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] For - if - else loop; print selective output

2012-10-25 Thread Steven D'Aprano

On 26/10/12 07:16, eryksun wrote:

On Thu, Oct 25, 2012 at 3:46 PM, Prasad, Ramit
ramit.pra...@jpmorgan.com  wrote:


Do you happen to know offhand if there is a difference between
`inlist` vs. `intuple` vs. `inset`?


The in comparison (__contains__ method) is equivalent for list and
tuple. It has to search through the sequence item by item, which makes
it an O(n) operation. On the other hand, a set/dict uses the hash() of
an object to map it to a known location in a table (performance
degrades if there are many collisions). On average, you can check if a
set/dict contains an item in constant time, i.e. O(1). The amortized
worst case is O(n).


To be precise: dict lookup, deletion and insertion are amortized O(1)
(on average, constant time) assuming there are no hash collisions.

When there are collisions, they are O(k) where k is the number of
colliding items. So in the worst case where k=n (the number of items
in the dict), dicts perform a little worse than lists, but in the
typical case, they are usually much, much faster.

Amortized strictly only refers to deletion and insertion. Since
lookups don't modify the dict, a lookup always takes the same time.
But deletions and insertions will occasionally trigger a resize of
the dict, which obviously takes a lot longer than the non-resizing
cases. But if you spread the average cost of the resize over many
insertions or deletions, you get amortized O(1) time.



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


Re: [Tutor] Reading from a seperate file

2012-10-25 Thread Steven D'Aprano

On 26/10/12 05:15, myles broomes wrote:


I'm trying to code a program that retrieves data from a seperate
file but according to my program, the seperate file is empty when
I know it clearly isn't. It's a txt file and here are its contents:
 120
74
57
44
12
I thought that maybe the problem was the code I had written but
even when I try and read from the file in an interactive session
in the Python Shell, it does the same thing. I open it in read mode
and assign to a variable like so: scoresFile=open('highScores.txt','r')
But whenever I try to say read from it: scoresFile.read() It comes
up with a blank string: ' ' Can anyone help me?
Myles Broomes



The symptoms you describe suggest that you are reading from the file
twice without closing the file first, or resetting the file pointer.
Once the file pointer reaches the end of the file, there's nothing
left to read and you get an empty string.

Example:

py count = open(demo, w).write(some text)
py f = open(demo, r)
py f.read()
'some text'
py f.read()
''
py f.read()
''
py f.seek(0)  # move the file pointer back to the start
0
py f.read()
'some text'



--
Steven

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


Re: [Tutor] For - if - else loop; print selective output

2012-10-25 Thread Oscar Benjamin
On 26 October 2012 00:08, Steven D'Aprano st...@pearwood.info wrote:
 On 26/10/12 07:16, eryksun wrote:

 On Thu, Oct 25, 2012 at 3:46 PM, Prasad, Ramit
 ramit.pra...@jpmorgan.com  wrote:


 Do you happen to know offhand if there is a difference between
 `inlist` vs. `intuple` vs. `inset`?


 The in comparison (__contains__ method) is equivalent for list and
 tuple. It has to search through the sequence item by item, which makes
 it an O(n) operation. On the other hand, a set/dict uses the hash() of
 an object to map it to a known location in a table (performance
 degrades if there are many collisions). On average, you can check if a
 set/dict contains an item in constant time, i.e. O(1). The amortized
 worst case is O(n).


 To be precise: dict lookup, deletion and insertion are amortized O(1)
 (on average, constant time) assuming there are no hash collisions.

 When there are collisions, they are O(k) where k is the number of
 colliding items. So in the worst case where k=n (the number of items
 in the dict), dicts perform a little worse than lists, but in the
 typical case, they are usually much, much faster.

The use of big-O notation above seems strange to me. The average value
of k is (assuming non-pathological inputs) a constant that is
independent of n (for large n). So O(k) really means average case
O(1).

 Amortized strictly only refers to deletion and insertion. Since
 lookups don't modify the dict, a lookup always takes the same time.
 But deletions and insertions will occasionally trigger a resize of
 the dict, which obviously takes a lot longer than the non-resizing
 cases. But if you spread the average cost of the resize over many
 insertions or deletions, you get amortized O(1) time.

You mean that the cost is only considered to be amortised when
averaging over the cost of the qualitatively distinct operation of
resizing the dict among the cost of the non-resizing insertions (does
deletion ever trigger a resize?). When averaging over the cases where
there are more or less hash collisions but no possibility of resizes
we refer to the cost as average case but not amortised. Is that
what you mean?


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


Re: [Tutor] Reading from a seperate file

2012-10-25 Thread Alan Gauld

On 25/10/12 19:15, myles broomes wrote:


but according to my program, the seperate file is empty when I know it
clearly isn't.


OK, So show us the program! Otherwise we are just guessing.



Python Shell, it does the same thing. I open it in read mode and assign
to a variable like so:

scoresFile=open('highScores.txt','r')

But whenever I try to say read from it:

scoresFile.read()

It comes up with a blank string:

' '


How do you get a blank string?
Calling read() without assigning it to a variable means the content of 
your file will be lost. But then what are you looking at to say its a 
blank string?


We need to see the actual code you are running not just random fragments.


Can anyone help me?


Sure, but you've got to help us first. Show us your code.
And if you get an error message show us that too - all of it.
And for good measure tell us the Python version and the OS.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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