Re: SMTP-Email Help

2013-04-29 Thread Steven D'Aprano
On Sun, 28 Apr 2013 23:39:53 -0700, root.phidgets wrote:

> Hi Guys. I need some help with the coding for my program.
> 
> This coding is suppose to sort text file according to the latest date
> and send the latest file. Attach it to my email and sent to another
> email account. But somehow the program is unable to send email.

I believe that Google has locked you out of your account.

Are you aware that you've just made your username and password visible to 
the entire Internet? If you hadn't already triggered Google's anti-spam 
detection, you surely have now.


> gmail_user = "@gmail.com"
> gmail_pwd = "**"

(details redacted, to shut the barn door now that the horse has already 
run away)


When I log into your gmail account with those credentials, I see:



Verify your account

We've detected unusual activity on your account. To immediately
restore access to your account, choose how to verify your account.



so I believe Google have locked the account because they think you're 
spamming. You'll need to fix that before anything else.



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [pyxl] Re: xlrd 0.9.2 released!

2013-04-29 Thread Chris Withers

On 26/04/2013 20:37, Ondrej Ján wrote:

   If users have python-xlrd installed, package maintainer should release
only updates, which are API compatible with older versions.


Right, the only API change between 0.6 and 0.9 is the removal of the 
'pickleable' parameter to open_workbook. However, this was removed 
because it hadn't worked in a long long time.



   Both Fedora and CentOS has python>=2.6, python3 is available for Fedora
too (no plans to release python3-xlrd now).


xlrd 0.9 and above works with both python2 and python3 as-is.


   So can I release an update for all my Linux distributions with backward
compatibility of this package? xlrd-0.8 was requested for xlsx support.


I would suggest just going for 0.9.2.


   Are there some howtos describing upgrading applications using xlrd
to newer versions? These documents can help me to choose, if I can release
an update.


There aren't any, because there are no changes. Applications written for 
0.6.x will still work without change when using 0.9.2.


cheers,

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
- http://www.simplistix.co.uk
--
http://mail.python.org/mailman/listinfo/python-list


How do I encode and decode this data to write to a file?

2013-04-29 Thread cl
I am debugging some code that creates a static HTML gallery from a
directory hierarchy full of images. It's this package:-
https://pypi.python.org/pypi/Gallery2.py/2.0


It's basically working and does pretty much what I want so I'm happy to
put some effort into it and fix things.

The problem I'm currently chasing is that it can't cope with directory
names that have accented characters in them, it fails when it tries to
write the HTML that creates the page with the thumbnails on.

The code that's failing is:-

raw = os.path.join(directory, self.getNameNoExtension()) + ".html"
file = open(raw, "w")
file.write("".join(html).encode('utf-8'))
file.close()

The variable html is a list containing the lines of HTML to write to the
file.  It fails when it contains accented characters (an é in this
case).  Here's the traceback:-

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/gallery/galleries.py", line 41, 
in run self._recurse()
  File "/usr/local/lib/python2.7/dist-packages/gallery/galleries.py", line 272, 
in _recurse os.path.walk(self.props["sourcedir"], self.processDir, None)
  File "/usr/lib/python2.7/posixpath.py", line 246, in walk walk(name, func, 
arg) File "/usr/lib/python2.7/posixpath.py", line 246, in walk walk(name, func, 
arg)
  File "/usr/lib/python2.7/posixpath.py", line 246, in walk walk(name, func, 
arg) File "/usr/lib/python2.7/posixpath.py", line 238, in walk func(arg, top, 
names)
  File "/usr/local/lib/python2.7/dist-packages/gallery/galleries.py", line 263, 
in processDir self.createGallery()
  File "/usr/local/lib/python2.7/dist-packages/gallery/galleries.py", line 215, 
in createGallery self.picturemanager.createPictureHTMLs(self.footer)
  File "/usr/local/lib/python2.7/dist-packages/gallery/picturemanager.py", line 
84, in createPictureHTMLs curPic.createPictureHTML(self.galleryDirectory, 
self.getStylesheet(), self.fullsize, footer)
  File "/usr/local/lib/python2.7/dist-packages/gallery/picture.py", line 361, 
in createPictureHTML file.write("".join(html).encode('utf-8')) 
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 783: 
ordinal not in range(128)



If I understand correctly the encode() is saying that it can't
understand the data in the html because there's a character 0xc3 in it.
I *think* this means that the é is encoded in UTF-8 already in the
incoming data stream (should be as my system is wholly UTF-8 as far as I
know and I created the directory name).

So how do I change the code so I don't get the error?  Do I just
decode() the data first and then encode() it?

-- 
Chris Green
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I encode and decode this data to write to a file?

2013-04-29 Thread Andrew Berg
On 2013.04.29 04:47, c...@isbd.net wrote:
> If I understand correctly the encode() is saying that it can't
> understand the data in the html because there's a character 0xc3 in it.
> I *think* this means that the é is encoded in UTF-8 already in the
> incoming data stream (should be as my system is wholly UTF-8 as far as I
> know and I created the directory name).
You can verify that your filesystem is set to use UTF-8 with 
sys.getfilesystemencoding(). If it returns 'ascii', then your locale settings
are incorrect.

-- 
CPython 3.3.1 | Windows NT 6.2.9200 / FreeBSD 9.1
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: xml.etree.ElementTree if element does not exist?

2013-04-29 Thread Ombongi Moraa Fe
Good Afternoon,

Among other elements values that my script finds is value for sepid

sepid = content.find(".//{http://www.huawei.com.cn/schema/common/v2_1}sepid
").text


however, if i pass xml data that DOES NOT contain sepid element, i get an
error:

Traceback (most recent call last):
  File "/usr/local/bin/receive.py", line 21, in 
sepid = content.find(".//{
http://www.huawei.com.cn/schema/common/v2_1}sepid";).text
AttributeError: 'NoneType' object has no attribute 'text'


some messages i receive will have the sepid parameter, other will not have
this parameter. How can i cater for this? kinda like an if .. else
implementation for xml.etree.ElementTree  ?



Thanks in advance.

Saludos

Ombongi Moraa Faith
-- 
http://mail.python.org/mailman/listinfo/python-list


Drag and drop in Windows

2013-04-29 Thread Robert Flintham
Hello all,

Sorry to post such a generic question, but after searching the interwebs I'm 
not really any wiser about how to start with this.

I'm currently on:
Windows XP
Python 2.7

I'm trying to create a small window in Python 2.7, that when you drop a file 
onto it from Windows explorer returns the file's path so that I can then go on 
to open the file and do whatever with it.  I was planning on using Tkinter 
because that's what I've used before for GUI's, but I can be swayed from this 
if needs be.

I've found this (TkDND):
http://wiki.tcl.tk/2768

But I don't know how to implement this in Python.  The Windows binary for it 
comes as a set of ".tcl" files and a single ".dll" file.

The two options I've stumbled across seem to be

1.  a Python wrapper for the DLL (I think to wrap C code??), which can then be 
imported like you'd import a Python package

2.  direct implementation of the Tcl file [tk.eval('source ...')], but I don't 
reallu understand what's going on with this - can you only execute a "main" bit 
of Tcl files rather than implementing individual functions?

Any input (however minimal) is definitely appreciated!  I've included what I 
think are probably the relevant functions from the Tcl files at the bottom of 
the email, but I don't really understand the nuts and bolts of the code.

All the best,
Rob

[From "tkdnd.tcl"...]

# 
#  Command tkdnd::drag_source
# 
proc tkdnd::drag_source { mode path { types {} } { event 1 } } {
  set tags [bindtags $path]
  set idx  [lsearch $tags "TkDND_Drag*"]
  switch -- $mode {
register {
  if { $idx != -1 } {
bindtags $path [lreplace $tags $idx $idx TkDND_Drag$event]
  } else {
bindtags $path [concat $tags TkDND_Drag$event]
  }
  set types [platform_specific_types $types]
  set old_types [bind $path <>]
  foreach type $types {
if {[lsearch $old_types $type] < 0} {lappend old_types $type}
  }
  bind $path <> $old_types
}
unregister {
  if { $idx != -1 } {
bindtags $path [lreplace $tags $idx $idx]
  }
}
  }
};# tkdnd::drag_source


[From "tkdnd_windows.tcl"...]

# 
#  Command olednd::_GetDragSource
# 
proc olednd::_GetDragSource {  } {
  variable _drag_source
  return $_drag_source
};# olednd::_GetDragSource
DISCLAIMER:
This email and any attachments hereto contains proprietary information, some or 
all of which may be confidential or legally privileged. It is for the exclusive 
use of the intended recipient(s) only. If an addressing or transmission error 
has misdirected this e-mail and you are not the intended recipient(s), please 
notify the author by replying to this e-mail. If you are not the intended 
recipient you must not use, disclose, distribute, copy, print, or rely on this 
e-mail or any attachments, as this may be unlawful.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I encode and decode this data to write to a file?

2013-04-29 Thread Peter Otten
c...@isbd.net wrote:

> I am debugging some code that creates a static HTML gallery from a
> directory hierarchy full of images. It's this package:-
> https://pypi.python.org/pypi/Gallery2.py/2.0
> 
> 
> It's basically working and does pretty much what I want so I'm happy to
> put some effort into it and fix things.
> 
> The problem I'm currently chasing is that it can't cope with directory
> names that have accented characters in them, it fails when it tries to
> write the HTML that creates the page with the thumbnails on.
> 
> The code that's failing is:-
> 
> raw = os.path.join(directory, self.getNameNoExtension()) + ".html"
> file = open(raw, "w")
> file.write("".join(html).encode('utf-8'))
> file.close()
> 
> The variable html is a list containing the lines of HTML to write to the
> file.  It fails when it contains accented characters (an é in this
> case).  Here's the traceback:-
> 
> Traceback (most recent call last):
>   File "/usr/local/lib/python2.7/dist-packages/gallery/galleries.py", line
>   41, in run self._recurse() File
>   "/usr/local/lib/python2.7/dist-packages/gallery/galleries.py", line 272,
>   in _recurse os.path.walk(self.props["sourcedir"], self.processDir, None)
>   File "/usr/lib/python2.7/posixpath.py", line 246, in walk walk(name,
>   func, arg) File "/usr/lib/python2.7/posixpath.py", line 246, in walk
>   walk(name, func, arg) File "/usr/lib/python2.7/posixpath.py", line 246,
>   in walk walk(name, func, arg) File "/usr/lib/python2.7/posixpath.py",
>   line 238, in walk func(arg, top, names) File
>   "/usr/local/lib/python2.7/dist-packages/gallery/galleries.py", line 263,
>   in processDir self.createGallery() File
>   "/usr/local/lib/python2.7/dist-packages/gallery/galleries.py", line 215,
>   in createGallery self.picturemanager.createPictureHTMLs(self.footer)
>   File "/usr/local/lib/python2.7/dist-packages/gallery/picturemanager.py",
>   line 84, in createPictureHTMLs
>   curPic.createPictureHTML(self.galleryDirectory, self.getStylesheet(),
>   self.fullsize, footer) File
>   "/usr/local/lib/python2.7/dist-packages/gallery/picture.py", line 361,
>   in createPictureHTML file.write("".join(html).encode('utf-8'))
>   UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position
>   783: ordinal not in range(128)
> 
> 
> 
> If I understand correctly the encode() is saying that it can't
> understand the data in the html because there's a character 0xc3 in it.
> I *think* this means that the é is encoded in UTF-8 already in the
> incoming data stream (should be as my system is wholly UTF-8 as far as I
> know and I created the directory name).
> 
> So how do I change the code so I don't get the error?  Do I just
> decode() the data first and then encode() it?
> 

Note that you are getting a *UnicodeDecodeError*, not a UnicodeEncodeError. 
Try omitting the encode() step, i. e. instead of

> file.write("".join(html).encode('utf-8'))

use

file.write(""join(html))

Background (applies to Python 2 only): the str type deals with bytes, not 
code points. The right thing to do is to use .decode(...) to convert from 
str to unicode and .encode(...) to convert from unicode to str. In Python 2 
however the str type has an encode(...) method which is basically equivalent 
to

class str:
   # imaginary python implementation of python2's str
   ...
   def encode(self, encoding):
   return self.decode("ascii").encode(encoding)

and is almost never called intentionally.

PS Python3 has relabeled unicode to str and thus uses unicode by default. 
str was renamed to bytes and the annoying bytes.encode() method is gone.

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


Re: xml.etree.ElementTree if element does not exist?

2013-04-29 Thread Stefan Holdermans
Ombongi,

> however, if i pass xml data that DOES NOT contain sepid element, i get an 
> error:
> 
> Traceback (most recent call last):
>   File "/usr/local/bin/receive.py", line 21, in 
> sepid = 
> content.find(".//{http://www.huawei.com.cn/schema/common/v2_1}sepid";).text
> AttributeError: 'NoneType' object has no attribute 'text'
> 
> 
> some messages i receive will have the sepid parameter, other will not have 
> this parameter. How can i cater for this? kinda like an if .. else 
> implementation for xml.etree.ElementTree  ?

What about simply testing whether the value returned by find is None? For 
example:

  $ cat test.py
  from xml.etree import ElementTree

  myTree = ElementTree.fromstring('')
  myElement = myTree.find('orange')

  if myElement is None:
  print 'tree does not contain a child element "orange"'
  else:
  print myElement.text


  $ python test.py
  tree does not contain a child element "orange"

HTH,

  Stefan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I encode and decode this data to write to a file?

2013-04-29 Thread Dave Angel

On 04/29/2013 05:47 AM, c...@isbd.net wrote:

A couple of generic comments:  your email program made a mess of the 
traceback by appending each source line to the location information.


Please mention your Python version & OS.  Apparently you're running 2.7 
on Linux or similar.



I am debugging some code that creates a static HTML gallery from a
directory hierarchy full of images. It's this package:-
 https://pypi.python.org/pypi/Gallery2.py/2.0


It's basically working and does pretty much what I want so I'm happy to
put some effort into it and fix things.

The problem I'm currently chasing is that it can't cope with directory
names that have accented characters in them, it fails when it tries to
write the HTML that creates the page with the thumbnails on.

The code that's failing is:-

 raw = os.path.join(directory, self.getNameNoExtension()) + ".html"
 file = open(raw, "w")
 file.write("".join(html).encode('utf-8'))


You can't encode byte data, it's already encoded. So you're forcing the 
Python system to implicitly decode it (using ASCII codec) before letting 
you encode it to utf-8.  If you think it's already in utf-8, then omit 
the encode() call there.


Additionally, you can debug things with some simple print statements, at 
least if you decompose your 3-function line so you can get at the 
intermediate data.  Split the line into three parts;

temp1 = "".join(html) #temp1 is byte data
temp2 = temp1.decode()#temp2 is unicode data
temp3 = temp2.encode("utf-8")  #temp3 is byte data again
file.write(temp3)

Now, you'll presumably get the error on the second line, so examine the 
bytes around byte 783.  Make sure it's really in utf-8, and if it is, 
then skip the decode and the encode.  If it's not, then Andrew's advice 
is pertinent.


I would also look at the variable html.  It's a list, but what are the 
types of the elements in it?



 file.close()

The variable html is a list containing the lines of HTML to write to the
file.  It fails when it contains accented characters (an é in this
case).  Here's the traceback:-

Traceback (most recent call last):
   File "/usr/local/lib/python2.7/dist-packages/gallery/galleries.py", line 41, 
in run self._recurse()
   File "/usr/local/lib/python2.7/dist-packages/gallery/galleries.py", line 272, in 
_recurse os.path.walk(self.props["sourcedir"], self.processDir, None)
   File "/usr/lib/python2.7/posixpath.py", line 246, in walk walk(name, func, arg) File 
"/usr/lib/python2.7/posixpath.py", line 246, in walk walk(name, func, arg)
   File "/usr/lib/python2.7/posixpath.py", line 246, in walk walk(name, func, arg) File 
"/usr/lib/python2.7/posixpath.py", line 238, in walk func(arg, top, names)
   File "/usr/local/lib/python2.7/dist-packages/gallery/galleries.py", line 
263, in processDir self.createGallery()
   File "/usr/local/lib/python2.7/dist-packages/gallery/galleries.py", line 
215, in createGallery self.picturemanager.createPictureHTMLs(self.footer)
   File "/usr/local/lib/python2.7/dist-packages/gallery/picturemanager.py", 
line 84, in createPictureHTMLs curPic.createPictureHTML(self.galleryDirectory, 
self.getStylesheet(), self.fullsize, footer)
   File "/usr/local/lib/python2.7/dist-packages/gallery/picture.py", line 361, in 
createPictureHTML file.write("".join(html).encode('utf-8')) UnicodeDecodeError: 'ascii' 
codec can't decode byte 0xc3 in position 783: ordinal not in range(128)



If I understand correctly the encode() is saying that it can't
understand the data in the html because there's a character 0xc3 in it.
I *think* this means that the é is encoded in UTF-8 already in the
incoming data stream (should be as my system is wholly UTF-8 as far as I
know and I created the directory name).

So how do I change the code so I don't get the error?  Do I just
decode() the data first and then encode() it?




--
DaveA
--
http://mail.python.org/mailman/listinfo/python-list


Re: xml.etree.ElementTree if element does not exist?

2013-04-29 Thread Ombongi Moraa Fe
Hi Stefan, Group

Thanks for the thumbs up.

Worked perfectly.

Saludos

Ombongi Moraa Faith


On 29 April 2013 14:22, Stefan Holdermans  wrote:

> Ombongi,
>
> > however, if i pass xml data that DOES NOT contain sepid element, i get
> an error:
> >
> > Traceback (most recent call last):
> >   File "/usr/local/bin/receive.py", line 21, in 
> > sepid = content.find(".//{
> http://www.huawei.com.cn/schema/common/v2_1}sepid";).text
> > AttributeError: 'NoneType' object has no attribute 'text'
> >
> >
> > some messages i receive will have the sepid parameter, other will not
> have this parameter. How can i cater for this? kinda like an if .. else
> implementation for xml.etree.ElementTree  ?
>
> What about simply testing whether the value returned by find is None? For
> example:
>
>   $ cat test.py
>   from xml.etree import ElementTree
>
>   myTree = ElementTree.fromstring('')
>   myElement = myTree.find('orange')
>
>   if myElement is None:
>   print 'tree does not contain a child element "orange"'
>   else:
>   print myElement.text
>
>
>   $ python test.py
>   tree does not contain a child element "orange"
>
> HTH,
>
>   Stefan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Shear image (numpy.ndarray)

2013-04-29 Thread Radek Machulka
Yes, I already find that scipy.ndimage.interpolation.affine_transform
will solve this (but thank you Robert anyway!).

Just for others (if somebody will find this thread):
The 'matrix' parameter is transformation matrix for shear
transformation. For more detailes see:
http://en.wikipedia.org/wiki/Shear_matrix

Radek
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: xml.etree.ElementTree if element does not exist?

2013-04-29 Thread Neil Cerutti
On 2013-04-29, Ombongi Moraa Fe  wrote:
>
> Good Afternoon,
>
> Among other elements values that my script finds is value for sepid
>
> sepid = content.find(".//{http://www.huawei.com.cn/schema/common/v2_1}sepid
> ").text
>
>
> however, if i pass xml data that DOES NOT contain sepid element, i get an
> error:
>
> Traceback (most recent call last):
>   File "/usr/local/bin/receive.py", line 21, in 
> sepid = content.find(".//{
> http://www.huawei.com.cn/schema/common/v2_1}sepid";).text
> AttributeError: 'NoneType' object has no attribute 'text'

find returns None when it doesn't find what you asked for. So you
can't check the .text attribute right away unless you want an
exception thrown. I deal with these annoyances like this:

sepelem = content.find(".//{http://www.huawei.com.cn/schema/common/v2_1}sepid";)
if sepelem is not None:
   sepid = sepid.text
else:
   sepid = ''

The empty string works for my purposes. Your script might need
something else.

-- 
Neil Cerutti
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [pyxl] Re: xlrd 0.9.2 released!

2013-04-29 Thread Neil Cerutti
On 2013-04-29, Chris Withers  wrote:
> There aren't any, because there are no changes. Applications
> written for 0.6.x will still work without change when using
> 0.9.2.

The Python 3 support is greatly appreciated. I've started using
the xlrd package recently, and it's made the life of a few of my
coworkers incrementally easier.

-- 
Neil Cerutti
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: xml.etree.ElementTree if element does not exist?

2013-04-29 Thread Neil Cerutti
On 2013-04-29, Neil Cerutti  wrote:
> find returns None when it doesn't find what you asked for. So you
> can't check the .text attribute right away unless you want an
> exception thrown. I deal with these annoyances like this:
>
> sepelem = 
> content.find(".//{http://www.huawei.com.cn/schema/common/v2_1}sepid";)
> if sepelem is not None:
>sepid = sepid.text

Oops. One edit too fiew. That line should of course be

   sepid = sepelem.text

> else:
>sepid = ''
>
> The empty string works for my purposes. Your script might need
> something else.


-- 
Neil Cerutti
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I encode and decode this data to write to a file?

2013-04-29 Thread cl
Andrew Berg  wrote:
> On 2013.04.29 04:47, c...@isbd.net wrote:
> > If I understand correctly the encode() is saying that it can't
> > understand the data in the html because there's a character 0xc3 in it.
> > I *think* this means that the é is encoded in UTF-8 already in the
> > incoming data stream (should be as my system is wholly UTF-8 as far as I
> > know and I created the directory name).
> You can verify that your filesystem is set to use UTF-8 with 
> sys.getfilesystemencoding(). 
> If it returns 'ascii', then your locale settings 
> are incorrect.
> 

chris$ python
Python 2.7.3 (default, Sep 26 2012, 21:51:14) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.getfilesystemencoding()
'UTF-8'
>>> 

So I am set up right for UTF-8.

-- 
Chris Green
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I encode and decode this data to write to a file?

2013-04-29 Thread cl
Dave Angel  wrote:
> On 04/29/2013 05:47 AM, c...@isbd.net wrote:
> 
> A couple of generic comments:  your email program made a mess of the 
> traceback by appending each source line to the location information.
> 
What's me email program got to do with it?  :-)   I'm using a dedicated
newsreader (tin) as I posted via the gmane/usenet interface.  The posting
looks perfectly OK to me when I read it back from usenet.


> Please mention your Python version & OS.  Apparently you're running 2.7 
> on Linux or similar.
> 
Sorry, yes you're spot on.


> > I am debugging some code that creates a static HTML gallery from a
> > directory hierarchy full of images. It's this package:-
> >  https://pypi.python.org/pypi/Gallery2.py/2.0
> >
> >
> > It's basically working and does pretty much what I want so I'm happy to
> > put some effort into it and fix things.
> >
> > The problem I'm currently chasing is that it can't cope with directory
> > names that have accented characters in them, it fails when it tries to
> > write the HTML that creates the page with the thumbnails on.
> >
> > The code that's failing is:-
> >
> >  raw = os.path.join(directory, self.getNameNoExtension()) + ".html"
> >  file = open(raw, "w")
> >  file.write("".join(html).encode('utf-8'))
> 
> You can't encode byte data, it's already encoded. So you're forcing the 
> Python system to implicitly decode it (using ASCII codec) before letting 
> you encode it to utf-8.  If you think it's already in utf-8, then omit 
> the encode() call there.
> 
It's the way the code was as I installed it from pypi.  What you say
makes a lot of sense though, I'll remove the encode().


> Additionally, you can debug things with some simple print statements, at 
> least if you decompose your 3-function line so you can get at the 
> intermediate data.  Split the line into three parts;
>  temp1 = "".join(html) #temp1 is byte data
>  temp2 = temp1.decode()#temp2 is unicode data
>  temp3 = temp2.encode("utf-8")  #temp3 is byte data again
>  file.write(temp3)
> 
OK, thanks for this and all the other advice on this thread.

-- 
Chris Green
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I encode and decode this data to write to a file?

2013-04-29 Thread Robert Kern

On 2013-04-29 13:59, c...@isbd.net wrote:

Dave Angel  wrote:

On 04/29/2013 05:47 AM, c...@isbd.net wrote:

A couple of generic comments:  your email program made a mess of the
traceback by appending each source line to the location information.


What's me email program got to do with it?  :-)   I'm using a dedicated
newsreader (tin) as I posted via the gmane/usenet interface.  The posting
looks perfectly OK to me when I read it back from usenet.


FWIW, I see the same problem Dave sees. I'm using gmane via Thunderbird.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

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


Re: How do I encode and decode this data to write to a file?

2013-04-29 Thread cl
Robert Kern  wrote:
> On 2013-04-29 13:59, c...@isbd.net wrote:
> > Dave Angel  wrote:
> >> On 04/29/2013 05:47 AM, c...@isbd.net wrote:
> >>
> >> A couple of generic comments:  your email program made a mess of the
> >> traceback by appending each source line to the location information.
> >>
> > What's me email program got to do with it?  :-)   I'm using a dedicated
> > newsreader (tin) as I posted via the gmane/usenet interface.  The posting
> > looks perfectly OK to me when I read it back from usenet.
> 
> FWIW, I see the same problem Dave sees. I'm using gmane via Thunderbird.
> 
How strange.  I think it must be something to do with the gmane
interface between news and mail then.

-- 
Chris Green
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I encode and decode this data to write to a file?

2013-04-29 Thread Skip Montanaro
> How strange.  I think it must be something to do with the gmane
> interface between news and mail then.

Probably.  It was borked in Gmail as well...

Skip
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unwanted window spawns when using Tkinter with multiprocessing.

2013-04-29 Thread alternative00
My full code is : 


#Import
from tkinter import *
import wave
import winsound
import multiprocessing

#Initialisation
fenetre=Tk()
frame = Frame(fenetre, width=200, height=100)
instance = 'Instance'


#Fonctions

def key(event):
   
instance = 'Instance'
hitkey = event.char
instance = multiprocessing.Process(target=player, args=(hitkey,))
instance.start()



def player(hitkey):

  
winsound.PlaySound(hitkey + '.wav', 
winsound.SND_FILENAME|winsound.SND_NOWAIT|winsound.SND_ASYNC)


 
#TK
frame.focus_set()
frame.bind("", key)
frame.pack()
fenetre.mainloop()

The problem is that I don't know where to put that clause.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unwanted window spawns when using Tkinter with multiprocessing.

2013-04-29 Thread MRAB

On 29/04/2013 16:31, alternativ...@rocketmail.com wrote:

My full code is :


#Import
from tkinter import *
import wave
import winsound
import multiprocessing

#Initialisation
fenetre=Tk()
frame = Frame(fenetre, width=200, height=100)
instance = 'Instance'


#Fonctions

def key(event):

 instance = 'Instance'
 hitkey = event.char
 instance = multiprocessing.Process(target=player, args=(hitkey,))
 instance.start()



def player(hitkey):


 winsound.PlaySound(hitkey + '.wav', 
winsound.SND_FILENAME|winsound.SND_NOWAIT|winsound.SND_ASYNC)



#TK
frame.focus_set()
frame.bind("", key)
frame.pack()
fenetre.mainloop()

The problem is that I don't know where to put that clause.


I hope this helps:


#Import
from tkinter import *
import wave
import winsound
import multiprocessing


#Fonctions

def key(event):
instance = 'Instance'
hitkey = event.char
instance = multiprocessing.Process(target=player, args=(hitkey,))
instance.start()


def player(hitkey):
winsound.PlaySound(hitkey + '.wav', 
winsound.SND_FILENAME|winsound.SND_NOWAIT|winsound.SND_ASYNC)



if __name__ == "__main__":
# This part will be run if the file is run as the main script.
#
# The multiprocessing module will import this file to run the
# "player" function, but __name__ won't be "__main__" when it does
# so, therefore this bit of code won't be run.

#Initialisation
fenetre = Tk()
frame = Frame(fenetre, width=200, height=100)
instance = 'Instance'

#TK
frame.focus_set()
frame.bind("", key)
frame.pack()
fenetre.mainloop()

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


Can read in the BMP data correctly ,but the size is not right?

2013-04-29 Thread Jimmie He
   I'm trying to read in the BMP data by the the code below,and I'm check the 
data array with WINHEX,and it is correct,but which confuse me is why the size 
is 0x180,but the actual picture should be 48*48 = 0x120 bytes because I use 
1-bit BMP not the 24bit BMP,could any one give some hints?



__Head_Info = [
[ 'Type' ,0 , 2],#BM
[ 'FSize',2 , 4],#File Size
[ 'Reserved' ,6 , 4],#0x
[ 'OffBits'  ,10 , 4],#Offset of Image
[ 'SSize',14 , 4],# 40
[ 'Width',18 , 4],#Width
[ 'Height'   ,22 , 4],#Hight
[ 'Planes'   ,26 , 2],#1
[ 'BitCount' ,28 , 2],#{1,2,4,8,24}
[ 'Compress' ,30 , 4],#0
[ 'SizeImage',34 , 4],#Bytes Per Line
[ 'XPM'  ,38 , 4],#2835
[ 'YPM'  ,42 , 4],#2835
[ 'ClrUsed'  ,46 , 4],#0
[ 'ClrImportant' ,50 , 4]#0
]
_Type  =0;
_FSize =1;
_Reserved  =2;
_OffBits   =3;
_SSize =4;
_Width =5;
_Height=6;
_Planes=7;
_BitCount  =8;
_Compress  =9;
_SizeImage =10;
_XPM   =11;
_YPM   =12;
_ClrUsed   =13;
_ClrImportant =14;

def __getInt( b, idx):
return binToInt(b,__Head_Info[idx][1],__Head_Info[idx][2])

def saveMatrixtoASC(bmpfilename,ascfilename):
try:
handle1=open( bmpfilename ,"rb")
raw = bytearray(handle1.read( ))
handle1.close
except Exception as E:
return "error:"+ str(E),""

datastart=__getInt(raw, _OffBits)
datasize =__getInt(raw, _SizeImage)
print ('Image Offset = 0x%X'%datastart)
print ('Image Size   = 0x%X'%datasize)
handle2=open( ascfilename ,"w")
for i in range(0,datasize):
   handle2.write('0x%02X,'%raw[datastart+i])
   if (i+1) % 16 == 0 :
  handle2.write("\n")
handle2.close
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unwanted window spawns when using Tkinter with multiprocessing.

2013-04-29 Thread alternative00
It definetly helped, windows don't pop up anymore, but now it doesn't make any 
sound anymore. Could it be because of a local (non-global) variable ? 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unwanted window spawns when using Tkinter with multiprocessing.

2013-04-29 Thread Chris Angelico
On Tue, Apr 30, 2013 at 3:32 AM,   wrote:
> It definetly helped, windows don't pop up anymore, but now it doesn't make 
> any sound anymore. Could it be because of a local (non-global) variable ?

Did you read what I linked you to? There are rules to using
multiprocessing; more of them on Windows.

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


Re: Can read in the BMP data correctly ,but the size is not right?

2013-04-29 Thread Chris Angelico
On Tue, Apr 30, 2013 at 3:20 AM, Jimmie He  wrote:
> handle1.close

I haven't looked at the rest of the code, but be careful of this: You
aren't actually *calling* this function. That might be your problem
and it might not, but try fixing it (add the parentheses, even though
there's nothing to put in them) and see if that helps. Same with the
handle2.close at the end.

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


Re: Can read in the BMP data correctly ,but the size is not right?

2013-04-29 Thread Peter Otten
Jimmie He wrote:

>I'm trying to read in the BMP data by the the code below,and I'm check
>the data array with WINHEX,and it is correct,but which confuse me is
>why the size is 0x180,but the actual picture should be 48*48 = 0x120
>bytes because I use 1-bit BMP not the 24bit BMP,could any one give some
>hints?

According to wikipedia 

"""
The size of each row is rounded up to a multiple of 4 bytes [...]
"""

So 48/8 == 6 will be rounded to 8, and 8*48 == 384 == 0x180.

> handle1=open( bmpfilename ,"rb")
> raw = bytearray(handle1.read( ))
> handle1.close

To actually do something the last line should be handle1.close(). I 
recommend

with open(bmpfilename ,"rb") as handle1:
raw = bytearray(handle1.read())

instead which has the additional advantage that the file will be closed if 
an exception occurs in the with-suite.

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


Re: Can read in the BMP data correctly ,but the size is not right?

2013-04-29 Thread MRAB

On 29/04/2013 18:20, Jimmie He wrote:

I'm trying to read in the BMP data by the the code below,and I'm
check the data array with WINHEX,and it is correct,but which confuse
me is why the size is 0x180,but the actual picture should be 48*48 =
0x120 bytes because I use 1-bit BMP not the 24bit BMP,could any one
give some hints?


[snip]
What size is 0x180?

If you're asking why the file size is 0x180 and not 0x120, it's simply
because of the header. An image file contains not just the pixels of
the image, but also information about the image.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Can read in the BMP data correctly ,but the size is not right?

2013-04-29 Thread 88888 Dihedral
Jimmie He於 2013年4月30日星期二UTC+8上午1時20分49秒寫道:
> I'm trying to read in the BMP data by the the code below,and I'm check the 
> data array with WINHEX,and it is correct,but which confuse me is why the size 
> is 0x180,but the actual picture should be 48*48 = 0x120 bytes because I use 
> 1-bit BMP not the 24bit BMP,could any one give some hints?
> 
> 
> 
> 
> 
> 
> 
> __Head_Info = [
> 
> [ 'Type' ,0 , 2],#BM
> 
> [ 'FSize',2 , 4],#File Size
> 
> [ 'Reserved' ,6 , 4],#0x
> 
> [ 'OffBits'  ,10 , 4],#Offset of Image
> 
> [ 'SSize',14 , 4],# 40
> 
> [ 'Width',18 , 4],#Width
> 
> [ 'Height'   ,22 , 4],#Hight
> 
> [ 'Planes'   ,26 , 2],#1
> 
> [ 'BitCount' ,28 , 2],#{1,2,4,8,24}
> 
> [ 'Compress' ,30 , 4],#0
> 
> [ 'SizeImage',34 , 4],#Bytes Per Line
> 
> [ 'XPM'  ,38 , 4],#2835
> 
> [ 'YPM'  ,42 , 4],#2835
> 
> [ 'ClrUsed'  ,46 , 4],#0
> 
> [ 'ClrImportant' ,50 , 4]#0
> 
> ]
> 
> _Type  =0;
> 
> _FSize =1;
> 
> _Reserved  =2;
> 
> _OffBits   =3;
> 
> _SSize =4;
> 
> _Width =5;
> 
> _Height=6;
> 
> _Planes=7;
> 
> _BitCount  =8;
> 
> _Compress  =9;
> 
> _SizeImage =10;
> 
> _XPM   =11;
> 
> _YPM   =12;
> 
> _ClrUsed   =13;
> 
> _ClrImportant =14;
> 
> 
> 
> def __getInt( b, idx):
> 
> return binToInt(b,__Head_Info[idx][1],__Head_Info[idx][2])
> 
> 
> 
> def saveMatrixtoASC(bmpfilename,ascfilename):
> 
> try:
> 
> handle1=open( bmpfilename ,"rb")
> 
> raw = bytearray(handle1.read( ))
> 
> handle1.close
> 
> except Exception as E:
> 
> return "error:"+ str(E),""
> 
> 
> 
> datastart=__getInt(raw, _OffBits)
> 
> datasize =__getInt(raw, _SizeImage)
> 
> print ('Image Offset = 0x%X'%datastart)
> 
> print ('Image Size   = 0x%X'%datasize)
> 
> handle2=open( ascfilename ,"w")
> 
> for i in range(0,datasize):
> 
>handle2.write('0x%02X,'%raw[datastart+i])
> 
>if (i+1) % 16 == 0 :
> 
>   handle2.write("\n")
> 
> handle2.close

The start of each line of bytes must be in the 32 bit=4byte 
boundary in the MS BMP format.

Please read the MS specs.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unwanted window spawns when using Tkinter with multiprocessing.

2013-04-29 Thread alternative00
Yeah I did, but I globalized my variables, I've got only functions, and not 
methods, and my clause seems to work so I don't know why it doesn't work.
-- 
http://mail.python.org/mailman/listinfo/python-list


Mystery of module bindings!

2013-04-29 Thread Peter Rowat
This must be a trivial question:

I have "import numpy as np" in the python startup file.

A file called mod1.py contains "def myfn..."
 and inside myfn there is a call to, say, "np.convolve".

Interactively:
>python
 (numpy imported as np)

>import mod1
>
>mod1.myfn(...)

Error: global name "np" is not known.
===
Why is "np" not known to functions in an imported module ?
===

I can fix this by including "import numpy as np" in any module that uses numpy
functions -- but then what is the point of having a startup file?

-- PeterR
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Mystery of module bindings!

2013-04-29 Thread Steven D'Aprano
On Mon, 29 Apr 2013 12:30:29 -0700, Peter Rowat wrote:

> This must be a trivial question:
> 
> I have "import numpy as np" in the python startup file.

That only runs interactively. It does not run when you execute a script.


> A file called mod1.py contains "def myfn..."
>  and inside myfn there is a call to, say, "np.convolve".
> 
> Interactively:
>>python
>  (numpy imported as np)
> 
>>import mod1
>>
>>mod1.myfn(...)
> 
> Error: global name "np" is not known. ===
> Why is "np" not known to functions in an imported module ? ===

Let's suppose it was. What would that mean?

Look inside mod1, where there might be a function spam(), and also 
another function eggs(). spam() calls eggs(), it sees the eggs function 
in the same module, and all is well with the world.

Then you add to the startup file:

def eggs():
return "Something unexpected"


and all of a sudden mod1.spam() breaks, because it now sees *your* eggs() 
instead of its eggs. This would be a bad, bad thing.

This is why modules are their own namespace, and the interactive 
interpreter is its own, independent, namespace. What happens in the 
interactive interpreter stays in the interactive interpreter, without 
stomping all over every other module.


> I can fix this by including "import numpy as np" in any module that uses
> numpy functions -- but then what is the point of having a startup file?

The point of the startup file is to add things to the interactive 
interpreter's module, not to inject things into every module in sight.


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Mystery of module bindings!

2013-04-29 Thread Ned Batchelder

On 4/29/2013 3:30 PM, Peter Rowat wrote:

This must be a trivial question:

I have "import numpy as np" in the python startup file.

A file called mod1.py contains "def myfn..."
  and inside myfn there is a call to, say, "np.convolve".

Interactively:

python

 (numpy imported as np)


import mod1

mod1.myfn(...)

Error: global name "np" is not known.
===
Why is "np" not known to functions in an imported module ?
===

I can fix this by including "import numpy as np" in any module that uses numpy
functions -- but then what is the point of having a startup file?


The startup file is only for interactive use.  Python programs in files 
shouldn't depend on a startup file, as it will limit their portability.  
In an interactive session, it's helpful to save some typing, but you can 
use explicit imports in programs, as they only need to be typed once, 
and will be used many more times.


--Ned.



-- PeterR


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


Re: Mystery of module bindings!

2013-04-29 Thread Dave Angel

On 04/29/2013 03:30 PM, Peter Rowat wrote:

This must be a trivial question:

I have "import numpy as np" in the python startup file.

A file called mod1.py contains "def myfn..."
  and inside myfn there is a call to, say, "np.convolve".

Interactively:

python

 (numpy imported as np)


import mod1

mod1.myfn(...)


Error: global name "np" is not known.
===
Why is "np" not known to functions in an imported module ?
===

I can fix this by including "import numpy as np" in any module that uses numpy
functions




Yes, you need a separate import from any module that references numpy. 
Don't worry about performance, subsequent imports do not take noticeable 
time, since the module objects are cached.


Any time you want to use a symbol from another module, you have to get 
it somehow. into your own module.  You do not have to get it from the 
import statement, but it's certainly the simplest way, and the way 
that's usually clearest.


Each module represents a namespace, and except for the global namespace 
which is handled specially, you have to describe which ones you want 
access to.  It's a feature, not a limitation.


There are lots of modules automatically imported indirectly by the ones 
you use.  A quick test on a local copy of Python 3.3 shows 51 modules 
imported before I write any code.  I certainly wouldn't want all of them 
visible in my script's namespace.


> -- but then what is the point of having a startup file?
>

Presumably by startup file, you mean script.  Without a script, Python 
wouldn't have any idea what code to run.  You import a few modules, use 
functionality from them, and lots of things happen behind the scenes. 
Fortunately for all of us, Python doesn't throw all the symbols into one 
big pile.




--
DaveA
--
http://mail.python.org/mailman/listinfo/python-list


Re: Mystery of module bindings!

2013-04-29 Thread Dave Angel

On 04/29/2013 04:03 PM, Dave Angel wrote:

Please ignore my previous response, obviously I misread your question 
entirely.  I've never used a startup file, so I misread it as "script file".




Yes, you need a separate import from any module that references numpy.
Don't worry about performance, subsequent imports do not take noticeable
time, since the module objects are cached.


and more blathering...





--
DaveA
--
http://mail.python.org/mailman/listinfo/python-list


Re: Drag and drop in Windows

2013-04-29 Thread Christian Gollwitzer

Hi Robert,

Am 29.04.13 12:25, schrieb Robert Flintham:

I’ve found this (TkDND):

http://wiki.tcl.tk/2768

> But I don’t know how to implement this in Python.  The Windows binary
> for it comes as a set of “.tcl” files and a single “.dll” file.
> 2.direct implementation of the Tcl file [tk.eval(‘source …’)], but I
> don’t reallu understand what’s going on with this – can you only execute
> a “main” bit of Tcl files rather than implementing individual functions?

I can only comment on the Tcl side, since I'm not an expert in the 
Tkinter coupling mechanism. TkDND is indeed the way to go if you want 
native drag'n'drop support. The first step would indeed be to load the 
package into the Tcl interpreter. You need to:


1) Create a folder for the packages, put the files in a subfolder
Typically, this is something like lib/tkdnd, and at that level there 
must be the "pkgIndex.tcl" file

2) Append the lib/ folder to the auto path
tk.eval('lappend auto_path {mypath/lib}')
(the braces are Tcl's quoting mechanism)
3) load the package
tk.eval('package require tkdnd')

Then, you need to "register the target", i.e. declare a widget that it 
accepts files. Here, you need the Tk path name of the widget, which is 
retrieved by __str__:


tk.eval('tkdnd::drop_target register ' + yourwidget +' *')

Then, if you drop something, the widget recieves a virtual event 
<> . Now this is tricky, I don't know how to bind to 
that event. Following the tutorial for Tcl on http://wiki.tcl.tk/36708, 
I suppose something like


yourwidget.bind("<>", filesdropped)

should in principle work, but how to get the data out of it? It is 
stuffed into the %D bind substitution. Usual events store the MouseWheel 
distance in this field; so maybe you can get it from the field 
event.delta. I can't test it now, but I am a bit skeptical whether this 
works with the guts of TkInter. If not, you'd need to do some more 
forwarding from the Tcl side.


Christian
--
http://mail.python.org/mailman/listinfo/python-list


Re: Mystery of module bindings!

2013-04-29 Thread Ethan Furman

On 04/29/2013 12:30 PM, Peter Rowat wrote:

This must be a trivial question:

I have "import numpy as np" in the python startup file.

A file called mod1.py contains "def myfn..."
  and inside myfn there is a call to, say, "np.convolve".

Interactively:

python

 (numpy imported as np)


import mod1

mod1.myfn(...)


Error: global name "np" is not known.
===
Why is "np" not known to functions in an imported module ?
===

I can fix this by including "import numpy as np" in any module that uses numpy
functions -- but then what is the point of having a startup file?


That "fix" is indeed the way things are done.  Modules only know about what 
they import or create*.

The point of the startup file is to have things ready for interactive investigation -- if you use numpy *alot* then 
having it already there beats the heck out of typing `import numpy as np` every time you start the interpreter.


--
~Ethan~

*Unless you start playing with injection and stuff.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Unwanted window spawns when using Tkinter with multiprocessing.

2013-04-29 Thread Chris Angelico
On Tue, Apr 30, 2013 at 4:44 AM,   wrote:
> Yeah I did, but I globalized my variables, I've got only functions, and not 
> methods, and my clause seems to work so I don't know why it doesn't work.

I don't know what you mean by your "clause", and I think we have a
language barrier here. (Though your English is *way* better than my
French.) But for a simple rule of thumb, the only things you should
have flush left are "def" and "class" statements, and the one "if"
that checks __name__. Everything else should be indented.

Chris Angelico
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I encode and decode this data to write to a file?

2013-04-29 Thread Terry Jan Reedy

On 4/29/2013 5:47 AM, c...@isbd.net wrote:


case).  Here's the traceback:-




   File "/usr/local/lib/python2.7/dist-packages/gallery/picture.py", line 361,

> in createPictureHTML file.write("".join(html).encode('utf-8'))
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 
783: ordinal not in range(128)


Generiric advice for anyone getting unicode errors:
unpack the composition producing the error
so that one can see which operation produced it.

In this case
s = "".join(html)\
s = s.encode('utf-8')
file.write(s)

This also makes it possible to print intermediate results.
  print(type(s), s)  # would have been useful
Doing so would have immediately shown that in this case the error was 
the encode operation, because s was already bytes.
For many other posts, the error with the same type of message has been 
the print or write operation, do to output encoding issues, but that was 
not the case here.





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


repeat program

2013-04-29 Thread eschneider92
How do I make the following program repeat twice instead of asking whether the 
player wants to play again?


import random
import time

def intro():
print('You spot 2 caves in the distance.')
print ('You near 2 cave entrances..')
time.sleep(1)
print('You proceed even nearer...')
time.sleep(1)

def choosecave():
cave=''
while cave!='1' and cave !='2':
print('which cave?(1 or 2)')
cave=input()
return cave

def checkcave(chosencave):
friendlycave=random.randint(1,2)
if chosencave==str(friendlycave):
print ('you win')
else:
print('you lose')

playagain='yes'
while playagain=='yes':
intro()
cavenumber=choosecave()
checkcave(cavenumber)
print('wanna play again?(yes no)')
playagain=input()

Thanks in advance.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: repeat program

2013-04-29 Thread Dave Angel

On 04/29/2013 08:22 PM, eschneide...@comcast.net wrote:

How do I make the following program repeat twice instead of asking whether the 
player wants to play again?



Turn it into a function call, and call that function twice from 
top-level.  Or, more generally,




for i in range(2):
doit()




import random
import time

def intro():
 print('You spot 2 caves in the distance.')
 print ('You near 2 cave entrances..')
 time.sleep(1)
 print('You proceed even nearer...')
 time.sleep(1)

def choosecave():
 cave=''
 while cave!='1' and cave !='2':
 print('which cave?(1 or 2)')
 cave=input()
 return cave

def checkcave(chosencave):
 friendlycave=random.randint(1,2)
 if chosencave==str(friendlycave):
 print ('you win')
 else:
 print('you lose')

playagain='yes'
while playagain=='yes':
 intro()
 cavenumber=choosecave()
 checkcave(cavenumber)
 print('wanna play again?(yes no)')
 playagain=input()

Thanks in advance.




--
DaveA
--
http://mail.python.org/mailman/listinfo/python-list


Re: repeat program

2013-04-29 Thread MRAB

On 30/04/2013 01:22, eschneide...@comcast.net wrote:

How do I make the following program repeat twice instead of asking whether the 
player wants to play again?


import random
import time

def intro():
 print('You spot 2 caves in the distance.')
 print ('You near 2 cave entrances..')
 time.sleep(1)
 print('You proceed even nearer...')
 time.sleep(1)

def choosecave():
 cave=''
 while cave!='1' and cave !='2':
 print('which cave?(1 or 2)')
 cave=input()
 return cave

def checkcave(chosencave):
 friendlycave=random.randint(1,2)
 if chosencave==str(friendlycave):
 print ('you win')
 else:
 print('you lose')

playagain='yes'
while playagain=='yes':
 intro()
 cavenumber=choosecave()
 checkcave(cavenumber)
 print('wanna play again?(yes no)')
 playagain=input()

Thanks in advance.


Replace the 'while' loop with a 'for' loop that loops twice.

By the way, there's a bug in 'choosecave': what happens if the user
enters, say, '3'?

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


Re: repeat program

2013-04-29 Thread Denis McMahon
On Mon, 29 Apr 2013 17:22:28 -0700, eschneider92 wrote:

> How do I make the following program repeat twice instead of asking
> whether the player wants to play again?

You change something in the folowing two lines:

playagain='yes'
while playagain=='yes':

What you change and how you change it is probably your homework task, so 
I really shouldn't tell you any more than that.

Also, I might not be right about where you need to make the change, but 
hey, this is the internet, it must be right, yeah?

-- 
Denis McMahon, denismfmcma...@gmail.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unwanted window spawns when using Tkinter with multiprocessing.

2013-04-29 Thread alternative00
I thought 'clause' was reffering to the 'if __name__ == "__main__":' thing in 
English, but apparently not. 
Well except the import and the 'globalization' of my variables, every thing is 
idented.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: repeat program

2013-04-29 Thread Ian Kelly
On Mon, Apr 29, 2013 at 6:43 PM, MRAB  wrote:
> By the way, there's a bug in 'choosecave': what happens if the user
> enters, say, '3'?

Then they lose. :-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unwanted window spawns when using Tkinter with multiprocessing.

2013-04-29 Thread Dave Angel

On 04/29/2013 09:17 PM, alternativ...@rocketmail.com wrote:

I thought 'clause' was reffering to the 'if __name__ == "__main__":' thing in 
English, but apparently not.
Well except the import and the 'globalization' of my variables, every thing is 
idented.



No clue whom you think you're replying to, but all your replies so far 
are appearing in the thread as replies to your OP.  Please use a little 
context from the message you think you're responding to, so we have an 
idea what you're talking about.


Did you ever see MRAB's message, where he told you exactly what the 
change requested was?



if __name__ == "__main__":
# This part will be run if the file is run as the main script.
#
# The multiprocessing module will import this file to run the
# "player" function, but __name__ won't be "__main__" when it does
# so, therefore this bit of code won't be run.

#Initialisation
fenetre = Tk()
frame = Frame(fenetre, width=200, height=100)
instance = 'Instance'

#TK
frame.focus_set()
frame.bind("", key)
frame.pack()
fenetre.mainloop()

All of those lines were at the left margin in your earlier posted code, 
and you give us no reason to expect you've made them conditional.


--
DaveA
--
http://mail.python.org/mailman/listinfo/python-list