Re: [Tutor] where is the wsgi server root?

2015-04-15 Thread Jim Mooney
I'm guessing; but I'd expect it to be the current directory

> when you started the server. try adding a
>
> print( os,getcwd() )
>
> And see if the result is your root.
> Or you could try reading the documents...they might tell you!
>
> --
> Alan G


Read the docs? Where's the fun in that ;')

Actually, I found the root in the curdir and got a simple index.html page
going on the python server at port 8000. Odd, I figured it would be
index.py, as in index.php. That's all I wanted to do at present - see if I
could get it working.

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


Re: [Tutor] Regular expression on python

2015-04-15 Thread Alan Gauld

On 15/04/15 02:02, Steven D'Aprano wrote:

New one on me. Where does one find out about verbose mode?
I don't see it in the re docs?




or embed the flag in the pattern. The flags that I know of are:

(?x) re.X re.VERBOSE

The flag can appear anywhere in the pattern and applies to the whole
pattern, but it is good practice to put them at the front, and in the
future it may be an error to put the flags elsewhere.


I've always applied flags as separate params at the end of the
function call. I've never seen (or noticed?) the embedded form,
and don't see it described in the docs anywhere (although it
probably is). But the re module descriptions of the flags only goive the 
re.X/re.VERBOSE options, no mention of the embedded form.

Maybe you are just supposed to infer the (?x) form from the re.X...

However, that still doesn't explain the difference in your comment
syntax.

The docs say the verbose syntax looks like:

a = re.compile(r"""\d +  # the integral part
   \.# the decimal point
   \d *  # some fractional digits""", re.X)

Whereas your syntax is like:

a = re.compile(r"""(?x)  (?# turn on verbose mode)
   \d +  (?# the integral part)
   \.(?# the decimal point)
   \d *  (?# some fractional digits)""")

Again, where is that described?

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] Regular expression on python

2015-04-15 Thread Peter Otten
Alan Gauld wrote:

> On 15/04/15 02:02, Steven D'Aprano wrote:
>>> New one on me. Where does one find out about verbose mode?
>>> I don't see it in the re docs?
>>>
> 
>> or embed the flag in the pattern. The flags that I know of are:
>>
>> (?x) re.X re.VERBOSE
>>
>> The flag can appear anywhere in the pattern and applies to the whole
>> pattern, but it is good practice to put them at the front, and in the
>> future it may be an error to put the flags elsewhere.
> 
> I've always applied flags as separate params at the end of the
> function call. I've never seen (or noticed?) the embedded form,
> and don't see it described in the docs anywhere (although it
> probably is). 

Quoting :

"""
(?aiLmsux)
(One or more letters from the set 'a', 'i', 'L', 'm', 's', 'u', 'x'.) The 
group matches the empty string; the letters set the corresponding flags: 
re.A (ASCII-only matching), re.I (ignore case), re.L (locale dependent), 
re.M (multi-line), re.S (dot matches all), and re.X (verbose), for the 
entire regular expression. (The flags are described in Module Contents.) 
This is useful if you wish to include the flags as part of the regular 
expression, instead of passing a flag argument to the re.compile() function.

Note that the (?x) flag changes how the expression is parsed. It should be 
used first in the expression string, or after one or more whitespace 
characters. If there are non-whitespace characters before the flag, the 
results are undefined.
"""

> But the re module descriptions of the flags only goive the
> re.X/re.VERBOSE options, no mention of the embedded form.
> Maybe you are just supposed to infer the (?x) form from the re.X...
> 
> However, that still doesn't explain the difference in your comment
> syntax.
> 
> The docs say the verbose syntax looks like:
> 
> a = re.compile(r"""\d +  # the integral part
> \.# the decimal point
> \d *  # some fractional digits""", re.X)
> 
> Whereas your syntax is like:
> 
> a = re.compile(r"""(?x)  (?# turn on verbose mode)
> \d +  (?# the integral part)
> \.(?# the decimal point)
> \d *  (?# some fractional digits)""")
> 
> Again, where is that described?

"""
(?#...)
A comment; the contents of the parentheses are simply ignored.
"""

Let's try it out:

>>> re.compile("\d+(?# sequence of digits)").findall("alpha 123 beta 456")
['123', '456']
>>> re.compile("\d+# sequence of digits").findall("alpha 123 beta 456")
[]
>>> re.compile("\d+# sequence of digits", re.VERBOSE).findall("alpha 123 
beta 456")
['123', '456']

So (?#...)-style comments work in non-verbose mode, too, and Steven is 
wearing belt and braces (almost, the verbose flag is still necessary to 
ignore the extra whitespace).

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


Re: [Tutor] Regular expression on python

2015-04-15 Thread Albert-Jan Roskam

On Tue, 4/14/15, Peter Otten <__pete...@web.de> wrote:

 Subject: Re: [Tutor] Regular expression on python
 To: tutor@python.org
 Date: Tuesday, April 14, 2015, 4:37 PM
 
 Steven D'Aprano wrote:
 
 > On Tue, Apr 14, 2015 at 10:00:47AM +0200, Peter Otten
 wrote:
 >> Steven D'Aprano wrote:
 > 
 >> > I swear that Perl has been a blight on an
 entire generation of
 >> > programmers. All they know is regular
 expressions, so they turn every
 >> > data processing problem into a regular
 expression. Or at least they
 >> > *try* to. As you have learned, regular
 expressions are hard to read,
 >> > hard to write, and hard to get correct.
 >> > 
 >> > Let's write some Python code instead.
 > [...]
 > 
 >> The tempter took posession of me and dictated:
 >> 
 >> >>> pprint.pprint(
 >> ... [(k, int(v)) for k, v in
 >> ...
 re.compile(r"(.+?):\s+(\d+)(?:\s+\(.*?\))?\s*").findall(line)])
 >> [('Input Read Pairs', 2127436),
 >>  ('Both Surviving', 1795091),
 >>  ('Forward Only Surviving', 17315),
 >>  ('Reverse Only Surviving', 6413),
 >>  ('Dropped', 308617)]
 > 
 > Nicely done :-)
 > 


Yes, nice, but why do you use 
re.compile(regex).findall(line) 
and not
re.findall(regex, line)

I know what re.compile is for. I often use it outside a loop and then actually 
use the compiled regex inside a loop, I just haven't see the way you use it 
before.



 > I didn't say that it *couldn't* be done with a regex. 
 
 I didn't claim that.
 
 > Only that it is
 > harder to read, write, etc. Regexes are good tools, but
 they aren't the
 > only tool and as a beginner, which would you rather
 debug? The extract()
 > function I wrote, or
 r"(.+?):\s+(\d+)(?:\s+\(.*?\))?\s*" ?
 
 I know a rhetorical question when I see one ;)
 
 > Oh, and for the record, your solution is roughly 4-5
 times faster than
 > the extract() function on my computer. 
 
 I wouldn't be bothered by that. See below if you are.
 
 > If I knew the requirements were
 > not likely to change (that is, the maintenance burden
 was likely to be
 > low), I'd be quite happy to use your regex solution in
 production code,
 > although I would probably want to write it out in
 verbose mode just in
 > case the requirements did change:
 > 
 > 
 > r"""(?x)    (?# verbose mode)

personally, I prefer to be verbose about being verbose, ie use the re.VERBOSE 
flag. But perhaps that's just a matter of taste. Are there any use cases when 
the ?iLmsux operators are clearly a better choice than the equivalent flag? For 
me, the mental burden of a regex is big enough already without these operators. 


 >     (.+?):  (?# capture one or
 more character, followed by a colon)
 >     \s+     (?#
 one or more whitespace)
 >     (\d+)   (?#
 capture one or more digits)
 >     (?:     (?#
 don't capture ... )
 >       \s+   
    (?# one or more whitespace)
 >   
    \(.*?\)   (?# anything
 inside round brackets)
 >       )?     
   (?# ... and optional)
 >     \s*     (?#
 ignore trailing spaces)
 >     """


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


Re: [Tutor] Regular expression on python

2015-04-15 Thread Peter Otten
Albert-Jan Roskam wrote:

> On Tue, 4/14/15, Peter Otten <__pete...@web.de> wrote:

>>> >>> pprint.pprint(
>>> ... [(k, int(v)) for k, v in
>>> ...
>re.compile(r"(.+?):\s+(\d+)(?:\s+\(.*?\))?\s*").findall(line)])
>>> [('Input Read Pairs', 2127436),
>>>('Both Surviving', 1795091),
>>>('Forward Only Surviving', 17315),
>>>('Reverse Only Surviving', 6413),
>>>('Dropped', 308617)]

> Yes, nice, but why do you use
> re.compile(regex).findall(line)
> and not
> re.findall(regex, line)
> 
> I know what re.compile is for. I often use it outside a loop and then
> actually use the compiled regex inside a loop, I just haven't see the way
> you use it before.

What you describe here is how I use regular expressions most of the time.
Also, re.compile() behaves the same over different Python versions while the 
shortcuts for the pattern methods changed signature over time. 
Finally, some have a gotcha. Compare:

>>> re.compile("a", re.IGNORECASE).sub("b", "aAAaa")
'b'
>>> re.sub("a", "b", "aAAaa", re.IGNORECASE)
'bAAba'

Did you expect that? Congrats for thorough reading of the docs ;)

> personally, I prefer to be verbose about being verbose, ie use the
> re.VERBOSE flag. But perhaps that's just a matter of taste. Are there any
> use cases when the ?iLmsux operators are clearly a better choice than the
> equivalent flag? For me, the mental burden of a regex is big enough
> already without these operators. 

I pass flags separately myself, but

>>> re.sub("(?i)a", "b", "aAAaa")
'b'

might serve as an argument for inlined flags.

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


[Tutor] Changing a string number to another number

2015-04-15 Thread Ken G.

When running the following code, I get the following
error code:

201504110102030405061
Traceback (most recent call last):
  File "Mega_Millions_Tickets_Change.py", line 11, in 
datecode[20:21] = "0"
TypeError: 'str' object does not support item assignment


datecode = "201504110102030405061"
print datecode
if datecode[20:21] == "1":
datecode[20:21] = "0"
print datecode


I have tried using the zero as an integer but still get the same error code.
Any suggestion?

Thanks in advance,

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


Re: [Tutor] Changing a string number to another number

2015-04-15 Thread Dave Angel

On 04/15/2015 08:21 AM, Ken G. wrote:

When running the following code, I get the following
error code:

201504110102030405061
Traceback (most recent call last):
   File "Mega_Millions_Tickets_Change.py", line 11, in 
 datecode[20:21] = "0"
TypeError: 'str' object does not support item assignment


A 'str' object is immutable, which means simply that you cannot modify 
it in place.  All you can do is create a new str object, and rebind your 
variable to that new one.  That means that there are a number of things 
you cannot do directly to a string.  One of them is modifying a slice, 
as you're trying to do.





datecode = "201504110102030405061"
print datecode
if datecode[20:21] == "1":
 datecode[20:21] = "0"
print datecode




I can see that the first part of this is probably a standard datetime, 
and might suggest you convert it to one, and modify that as needed.  But 
you're so far to the right that I have to figure you're doing some 
custom encoding.


If you just want to replace a single character of a string, you could 
use the construct:


datecode = datecode[:20] + "0" + datecode[21:]

If you need something fancier, perhaps you can generalize it.

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


Re: [Tutor] Changing a string number to another number

2015-04-15 Thread Peter Otten
Ken G. wrote:

> When running the following code, I get the following
> error code:
> 
> 201504110102030405061
> Traceback (most recent call last):
>File "Mega_Millions_Tickets_Change.py", line 11, in 
>  datecode[20:21] = "0"
> TypeError: 'str' object does not support item assignment
> 
> 
> datecode = "201504110102030405061"
> print datecode
> if datecode[20:21] == "1":
>  datecode[20:21] = "0"
> print datecode
> 
> 
> I have tried using the zero as an integer but still get the same error
> code. Any suggestion?

Strings in Python are "immutable", i. e. you cannot change them once they 
are created. Instead you have to construct a new string. In the general case 
you can get the same effect as replacing the character #n of an all-ascii 
string with

>>> s = "01234567890"
>>> n = 3
>>> s[:n] + "x" + s[n+1:]
'012x4567890'

In your case you want to replace the last character, so s[n+1:] is empty

>>> s = "201504110102030405061"
>>> n = 20
>>> s[n+1:]
''

and just

>>> s[:n] + "x"
'20150411010203040506x'

is sufficient. A word of warning: as you are using Python 2 you are actually 
manipulating bytes not characters when using the default string type. This 
may have ugly consequences:

>>> s = "ähnlich" # I'm using UTF-8
>>> print "ae" + s[1:]
ae�hnlich

Here the new string is not valid UTF-8 because in that encoding a-umlaut 
consists of two bytes and I'm only removing one of them.

A partial fix is to use unicode explicitly:

>>> s = u"ähnlich"
>>> print "ae" + s[1:]
aehnlich

But if you are just starting consider switching to Python 3 where unicode is 
the default string type.

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


Re: [Tutor] Changing a string number to another number [RESOLVED]

2015-04-15 Thread Ken G.



On 04/15/2015 08:36 AM, Dave Angel wrote:

On 04/15/2015 08:21 AM, Ken G. wrote:

When running the following code, I get the following
error code:

201504110102030405061
Traceback (most recent call last):
   File "Mega_Millions_Tickets_Change.py", line 11, in 
 datecode[20:21] = "0"
TypeError: 'str' object does not support item assignment


A 'str' object is immutable, which means simply that you cannot modify 
it in place.  All you can do is create a new str object, and rebind 
your variable to that new one.  That means that there are a number of 
things you cannot do directly to a string.  One of them is modifying a 
slice, as you're trying to do.





datecode = "201504110102030405061"
print datecode
if datecode[20:21] == "1":
 datecode[20:21] = "0"
print datecode




I can see that the first part of this is probably a standard datetime, 
and might suggest you convert it to one, and modify that as needed.  
But you're so far to the right that I have to figure you're doing some 
custom encoding.


If you just want to replace a single character of a string, you could 
use the construct:


datecode = datecode[:20] + "0" + datecode[21:]

If you need something fancier, perhaps you can generalize it.


Thank you, Dave. That did the trick. You were partly right about the
datetime as the first part is a date and following contains the lotto
numbers purchased for that date of a drawing. The last number in
the string indicates if a bonus multiplier for the ticket was purchased,
0 for no and 1 for yes.

Again, thanks.

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


[Tutor] How to get a Tkinter window to print a color copy of itself as a .pdf file?

2015-04-15 Thread boB Stepp
Solaris 10, Python 2.4.4

I have very little experience with issuing print commands using a Unix
environment. Despite this, I wish to design a Tkinter window with a
"Print" button, which, when clicked, would create a copy of the
contents of the window as a .pdf file. GhostScript is available on my
systems.

I know that the Canvas container has the ability to print itself to a
postscript file, but I do not want to use Canvas as my container
object. So I am hopeful this is as simple as discovering the proper
command to associate with a button, which will generate a color
postscript file of the containing window. Once I can get that file, I
know how to get a .pdf out of it.

The main software that we use on these systems has this print command
associated with its color printer *button*:

lp -c -d ricoh -o resolution=1200 -o profile=photo -o dithering=photo
-o colorsettings=superfine -o black=k -o papper=letter -o itray=tray1

BTW, "papper" is what is actually in this software product's color
printer settings. But can this be correct? Anyway, it does print to
*paper*!

I suspect that some variant of this command associated with a print
button would print *something*, but how do I get it to print the
specific window containing the to-be-designed print button? Also, I
need it to print a postscript file, not print to paper. I suspect this
is another configuration setting that I need to research.

As always, many thanks in advance!

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


Re: [Tutor] Changing a string number to another number

2015-04-15 Thread Steven D'Aprano
On Wed, Apr 15, 2015 at 08:21:33AM -0400, Ken G. wrote:
> When running the following code, I get the following
> error code:
> 
> 201504110102030405061
> Traceback (most recent call last):
>   File "Mega_Millions_Tickets_Change.py", line 11, in 
> datecode[20:21] = "0"
> TypeError: 'str' object does not support item assignment

Try this:

datacode = datacode[:20] + "0" + datacode[21:]


This is called "slicing". We take a slice of the string, from the start 
up to just before position 20; "0"; and a slice from position 21 to the 
end of the string. The three pieces are then concatenated together, 
giving a new string.



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


Re: [Tutor] Changing a string number to another number [RESOLVED]

2015-04-15 Thread Ken G.



On 04/15/2015 08:50 AM, Peter Otten wrote:

Ken G. wrote:


When running the following code, I get the following
error code:

201504110102030405061
Traceback (most recent call last):
File "Mega_Millions_Tickets_Change.py", line 11, in 
  datecode[20:21] = "0"
TypeError: 'str' object does not support item assignment


datecode = "201504110102030405061"
print datecode
if datecode[20:21] == "1":
  datecode[20:21] = "0"
print datecode


I have tried using the zero as an integer but still get the same error
code. Any suggestion?

Strings in Python are "immutable", i. e. you cannot change them once they
are created. Instead you have to construct a new string. In the general case
you can get the same effect as replacing the character #n of an all-ascii
string with


s = "01234567890"
n = 3
s[:n] + "x" + s[n+1:]

'012x4567890'

In your case you want to replace the last character, so s[n+1:] is empty


s = "201504110102030405061"
n = 20
s[n+1:]

''

and just


s[:n] + "x"

'20150411010203040506x'

is sufficient. A word of warning: as you are using Python 2 you are actually
manipulating bytes not characters when using the default string type. This
may have ugly consequences:


s = "ähnlich" # I'm using UTF-8
print "ae" + s[1:]

ae�hnlich

Here the new string is not valid UTF-8 because in that encoding a-umlaut
consists of two bytes and I'm only removing one of them.

A partial fix is to use unicode explicitly:


s = u"ähnlich"
print "ae" + s[1:]

aehnlich

But if you are just starting consider switching to Python 3 where unicode is
the default string type.


Thank you, Peter. That would be something for me to consider.
Alas, I am still using Python 2.7.6 but do have it on standby.

Again, thanks.

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


Re: [Tutor] Changing a string number to another number [RESOLVED]

2015-04-15 Thread Ken G.



On 04/15/2015 09:09 AM, Steven D'Aprano wrote:

On Wed, Apr 15, 2015 at 08:21:33AM -0400, Ken G. wrote:

When running the following code, I get the following
error code:

201504110102030405061
Traceback (most recent call last):
   File "Mega_Millions_Tickets_Change.py", line 11, in 
 datecode[20:21] = "0"
TypeError: 'str' object does not support item assignment

Try this:

datacode = datacode[:20] + "0" + datacode[21:]


This is called "slicing". We take a slice of the string, from the start up to just before 
position 20; "0"; and a slice from position 21 to the end of the string. The three pieces 
are then concatenated together, giving a new string.

Wow! So darn simple with a good explanation. Thanks!

Ken

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


Re: [Tutor] How to get a Tkinter window to print a color copy of itself as a .pdf file?

2015-04-15 Thread Steven D'Aprano
On Wed, Apr 15, 2015 at 07:55:28AM -0500, boB Stepp wrote:
> Solaris 10, Python 2.4.4
> 
> I have very little experience with issuing print commands using a Unix
> environment. Despite this, I wish to design a Tkinter window with a
> "Print" button, which, when clicked, would create a copy of the
> contents of the window as a .pdf file. GhostScript is available on my
> systems.
> 
> I know that the Canvas container has the ability to print itself to a
> postscript file, but I do not want to use Canvas as my container
> object.

You will excuse me, I hope, but I'm afraid that comes across as rather 
foolish:

"I want to hammer this nail into this piece of wood. I have a hammer, 
but I don't want to use a hammer. I would prefer to use a saw, or 
perhaps a paint brush. How can I do this?"


Why don't you want to use a Canvas? That sounds like it will solve your 
problem. Use a Canvas as the container, give it a button, and have the 
button send a message to the Canvas saying "Print yourself to PDF 
file!"

If you explain why you don't wish to use a Canvas, perhaps we can 
suggest a solution that doesn't involve trying to hammer nails with 
paint brushes.


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


Re: [Tutor] How to get a Tkinter window to print a color copy of itself as a .pdf file?

2015-04-15 Thread boB Stepp
On Wed, Apr 15, 2015 at 8:29 AM, Steven D'Aprano  wrote:
> On Wed, Apr 15, 2015 at 07:55:28AM -0500, boB Stepp wrote:
>> Solaris 10, Python 2.4.4
>>
>> I have very little experience with issuing print commands using a Unix
>> environment. Despite this, I wish to design a Tkinter window with a
>> "Print" button, which, when clicked, would create a copy of the
>> contents of the window as a .pdf file. GhostScript is available on my
>> systems.
>>
>> I know that the Canvas container has the ability to print itself to a
>> postscript file, but I do not want to use Canvas as my container
>> object.
>
> You will excuse me, I hope, but I'm afraid that comes across as rather
> foolish:
>
> "I want to hammer this nail into this piece of wood. I have a hammer,
> but I don't want to use a hammer. I would prefer to use a saw, or
> perhaps a paint brush. How can I do this?"
>
>
> Why don't you want to use a Canvas? That sounds like it will solve your
> problem. Use a Canvas as the container, give it a button, and have the
> button send a message to the Canvas saying "Print yourself to PDF
> file!"
>
> If you explain why you don't wish to use a Canvas, perhaps we can
> suggest a solution that doesn't involve trying to hammer nails with
> paint brushes.

Perhaps I am being foolish! But I do have my reasons, which, in this
case, is I wanted to take advantage of the pack and grid geometry
managers. These two tools seem to make the positioning of the widgets
much easier. Unless I am missing something, in a Canvas container I
will have to assign pixel coordinates for everything, which sounds
like a lot more work!

In any event, I *would* like to know how to solve the original
problem, creating a print button that will print the contents of its
overall container object.



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


Re: [Tutor] How to get a Tkinter window to print a color copy of itself as a .pdf file?

2015-04-15 Thread Zachary Ware
On Apr 15, 2015 9:38 AM, "boB Stepp"  wrote:
> Perhaps I am being foolish! But I do have my reasons, which, in this
> case, is I wanted to take advantage of the pack and grid geometry
> managers. These two tools seem to make the positioning of the widgets
> much easier. Unless I am missing something, in a Canvas container I
> will have to assign pixel coordinates for everything, which sounds
> like a lot more work!

Couldn't you just map a single Frame widget on the canvas, and use the
Frame as your "outer" container?

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


Re: [Tutor] How to get a Tkinter window to print a color copy of itself as a .pdf file?

2015-04-15 Thread boB Stepp
On Wed, Apr 15, 2015 at 8:50 AM, Zachary Ware
 wrote:
>
> On Apr 15, 2015 9:38 AM, "boB Stepp"  wrote:
>> Perhaps I am being foolish! But I do have my reasons, which, in this
>> case, is I wanted to take advantage of the pack and grid geometry
>> managers. These two tools seem to make the positioning of the widgets
>> much easier. Unless I am missing something, in a Canvas container I
>> will have to assign pixel coordinates for everything, which sounds
>> like a lot more work!
>
> Couldn't you just map a single Frame widget on the canvas, and use the Frame
> as your "outer" container?

You are SMART, I am NOT! That sounds like a great idea!!

Is this the intent of the Tkinter design to provide print
functionality? To always start with a Canvas container?

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


Re: [Tutor] How to get a Tkinter window to print a color copy of itself as a .pdf file?

2015-04-15 Thread Peter Otten
boB Stepp wrote:

> Solaris 10, Python 2.4.4
> 
> I have very little experience with issuing print commands using a Unix
> environment. Despite this, I wish to design a Tkinter window with a
> "Print" button, which, when clicked, would create a copy of the
> contents of the window as a .pdf file. GhostScript is available on my
> systems.
> 
> I know that the Canvas container has the ability to print itself to a
> postscript file, but I do not want to use Canvas as my container
> object. So I am hopeful this is as simple as discovering the proper
> command to associate with a button, which will generate a color
> postscript file of the containing window. Once I can get that file, I
> know how to get a .pdf out of it.
> 
> The main software that we use on these systems has this print command
> associated with its color printer *button*:
> 
> lp -c -d ricoh -o resolution=1200 -o profile=photo -o dithering=photo
> -o colorsettings=superfine -o black=k -o papper=letter -o itray=tray1
> 
> BTW, "papper" is what is actually in this software product's color
> printer settings. But can this be correct? Anyway, it does print to
> *paper*!
> 
> I suspect that some variant of this command associated with a print
> button would print *something*, but how do I get it to print the
> specific window containing the to-be-designed print button? Also, I
> need it to print a postscript file, not print to paper. I suspect this
> is another configuration setting that I need to research.
> 
> As always, many thanks in advance!

I'm on linux and surprisingly

subprocess.call(["import", "-window", window_title, postscript_file])

worked. I admit it's a roundabout way...

Here's the complete experiment; I ran it with Python 2.7, but that shouldn't 
make a difference.

import Tkinter as tk
import subprocess

window_title = "gewizzede"
postscript_file = "tmp_snapshot.ps"

def print_canvas():
#canvas.postscript(file="tmp.ps")
subprocess.call(["import", "-window", window_title, postscript_file])

root = tk.Tk()
root.title(window_title)

canvas = tk.Canvas(root, width=200, height=200)
canvas.pack()

button = tk.Button(root, text="Print", command=print_canvas)
button.pack()

demo = tk.Button(root, text = "demo widget button")
canvas.create_window(50, 50, window=demo)
canvas.create_rectangle(25, 25, 75, 150, fill="red")
root.mainloop()

You might guess from the above that I tried Zachary's suggestion first, but 
that printed only the red rectangle, not the demo button.

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


Re: [Tutor] How to get a Tkinter window to print a color copy of itself as a .pdf file?

2015-04-15 Thread Zachary Ware
On Apr 15, 2015 9:59 AM, "boB Stepp"  wrote:
>
> On Wed, Apr 15, 2015 at 8:50 AM, Zachary Ware
>  wrote:
> >
> > On Apr 15, 2015 9:38 AM, "boB Stepp"  wrote:
> >> Perhaps I am being foolish! But I do have my reasons, which, in this
> >> case, is I wanted to take advantage of the pack and grid geometry
> >> managers. These two tools seem to make the positioning of the widgets
> >> much easier. Unless I am missing something, in a Canvas container I
> >> will have to assign pixel coordinates for everything, which sounds
> >> like a lot more work!
> >
> > Couldn't you just map a single Frame widget on the canvas, and use the
Frame
> > as your "outer" container?
>
> You are SMART, I am NOT! That sounds like a great idea!!

To almost quote Jacob Kaplan-Moss from his keynote talk at PyCon this year,
"Hi, I'm [Zach], and I'm a mediocre programmer" :)

> Is this the intent of the Tkinter design to provide print
> functionality? To always start with a Canvas container?

I honestly have no idea.

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


Re: [Tutor] Regular expression on python

2015-04-15 Thread Alan Gauld

On 15/04/15 09:24, Peter Otten wrote:


function call. I've never seen (or noticed?) the embedded form,
and don't see it described in the docs anywhere


Quoting :

"""
(?aiLmsux)
(One or more letters from the set 'a', 'i', 'L', 'm', 's', 'u', 'x'.) The
group matches the empty string; the letters set the corresponding flags:


Aha. The trick is knowing the correct search string... I tried 'flag' 
and 'verbose' but missed this entry.



Again, where is that described?


"""
(?#...)
A comment; the contents of the parentheses are simply ignored.
"""


OK, I missed that too.
Maybe I just wasn't awake enough this morning! :-)

Thanks Peter.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] How to get a Tkinter window to print a color copy of itself as a .pdf file?

2015-04-15 Thread Alan Gauld

On 15/04/15 13:55, boB Stepp wrote:

Solaris 10, Python 2.4.4

I have very little experience with issuing print commands using a Unix
environment. Despite this, I wish to design a Tkinter window with a
"Print" button, which, when clicked, would create a copy of the
contents of the window as a .pdf file. GhostScript is available on my
systems.


Your problem is that Tkinter does not really support the concept
of printing to hard copy. Other GUI frameworks (like WxPython)
do this much better. So anything you do will be a bit of a kluge.
There are some valid reasons for this: printing is extremely OS 
dependant. And Tk as a cross OS platform doesn't want to have

to deal with all the complexity(*).

The usual solution is to render the output in some print
friendly file format (html, groff, SVG etc) and then send that
to the appropriate print command. However if you really want
a screen image you might be better invoking one of the many
screen capture programs and then printing its output?

(*)
The main culprits are
- Windows which uses a "Device Context" to render its UI. The device can 
be either a screen or a printer (or notionally anything else) so in 
theory you just swap the DC and send the GUI to the new device. In 
practice its not that easy.
- MacOSX uses a Cocoa framework which, in turn, uses OpenGL primitives 
to render the UI and like Windows can theoretically render the UI on non 
screen devices, It can also render it as PDF natively which is how it 
normally prints screen images.
- *nix usually relies on rendering the output *content* on a new device 
or in a new format. Printing screenshots in generic Unix is a bit of an 
afterthought IMHO...Possibly a consequence of X having been designed 
before the days of affordable colour printers - the same reason Steve 
Jobs didn't bother with colour on the early Macs - if you couldn't print 
it, there was no point, he thought...


Printing UI images and WYSIWYG type output is one of the few triggers 
that drive me to WxPython. It handles it much more easily than Tk.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] How to get a Tkinter window to print a color copy of itself as a .pdf file?

2015-04-15 Thread boB Stepp
On Wed, Apr 15, 2015 at 10:39 AM, Peter Otten <__pete...@web.de> wrote:
> boB Stepp wrote:
>
>> Solaris 10, Python 2.4.4

[...]

>
> I'm on linux and surprisingly
>
> subprocess.call(["import", "-window", window_title, postscript_file])
>
> worked. I admit it's a roundabout way...

I did not find the "import" command in my Unix reference, but found it
in the man pages on my Solaris workstation. It looks like this should
work without having to use the Canvas container, which was what I was
originally asking about.

> Here's the complete experiment; I ran it with Python 2.7, but that shouldn't
> make a difference.

Yes, it ran fine on Python 2.4.4.

> import Tkinter as tk

Question: I have been using "from Tkinter import *" as suggested in
"Programming Python" by Lutz. He remarks that unlike other situations,
this is generally safe with Tkinter. Is there a benefit to doing the
import as you have?

> import subprocess
>
> window_title = "gewizzede"
> postscript_file = "tmp_snapshot.ps"
>
> def print_canvas():
> #canvas.postscript(file="tmp.ps")
> subprocess.call(["import", "-window", window_title, postscript_file])

At first I thought you had a typo here, but now I understand that "#
canvas.postscript(file="tmp.ps")" only gave you the window framework.
When I made my attempt to employ Zach's suggestion by embedding my
existing frames within a Canvas, I only got an empty white rectangle.
After searching online for that, I found references that Canvas'
postscript method only works for objects drawn on its canvas, NOT
embedded windows, frames, etc. This had me scratching my head again.
But then your post came in. I am very grateful for your solution!

> root = tk.Tk()
> root.title(window_title)
>
> canvas = tk.Canvas(root, width=200, height=200)
> canvas.pack()
>
> button = tk.Button(root, text="Print", command=print_canvas)
> button.pack()
>
> demo = tk.Button(root, text = "demo widget button")
> canvas.create_window(50, 50, window=demo)
> canvas.create_rectangle(25, 25, 75, 150, fill="red")
> root.mainloop()
>
> You might guess from the above that I tried Zachary's suggestion first, but
> that printed only the red rectangle, not the demo button.


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


Re: [Tutor] How to get a Tkinter window to print a color copy of itself as a .pdf file?

2015-04-15 Thread boB Stepp
On Wed, Apr 15, 2015 at 11:13 AM, Alan Gauld  wrote:

[...]

> Your problem is that Tkinter does not really support the concept
> of printing to hard copy. Other GUI frameworks (like WxPython)
> do this much better. So anything you do will be a bit of a kluge.
> There are some valid reasons for this: printing is extremely OS dependant.
> And Tk as a cross OS platform doesn't want to have
> to deal with all the complexity(*).

This is sounding like developing a platform-independent program
involving Tkinter and requiring printing is no easy task. Fortunately
my efforts need only run successfully on the Solaris 10 OS.

[...]

> Printing UI images and WYSIWYG type output is one of the few triggers that
> drive me to WxPython. It handles it much more easily than Tk.

I will have to keep wxPython in mind. It does not help me at work, but
for non-work projects it might be very useful. However, I should work
on mastering Tkinter first, as I am sure that all of the principles I
learn here will be generically useful for any GUI programming.

Thanks, Alan!

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


Re: [Tutor] How to get a Tkinter window to print a color copy of itself as a .pdf file?

2015-04-15 Thread Alan Gauld

On 15/04/15 18:00, boB Stepp wrote:


import Tkinter as tk


Question: I have been using "from Tkinter import *" as suggested in
"Programming Python" by Lutz. He remarks that unlike other situations,
this is generally safe with Tkinter. Is there a benefit to doing the
import as you have?


Biggest benefit is if you ever move to Python 3 you cansimply change 
that line to


import tkinter as tk

and it carries on working (or at least until you hit some
of the other renamed modules!)

But also if you ever get to use Tix, because it is a
superset of Tkinter so you can just change to

import tix as tk

and it keeps working but you now have access to all the
Tix widgets too.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


[Tutor] Reference last email message...

2015-04-15 Thread Ken G.

I just emailed that I was unable to correct a message in ModTools
so I went to Yahoo and made the change and then approved it.

Noticing it did not appear on the list, I checked the Activity Log
in Yahoo and it was marked Bounced!

Several days ago, we had another message correction and that
too, bounced.

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


Re: [Tutor] Reference last email message...

2015-04-15 Thread Alan Gauld

On 16/04/15 00:47, Ken G. wrote:

I just emailed that I was unable to correct a message in ModTools
so I went to Yahoo and made the change and then approved it.


What is ModTools? What kind of message?
Where does Yahoo fit in?

What does any of it have to do with this list?

I'm confused.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] Reference last email message...

2015-04-15 Thread Dave Angel

On 04/15/2015 07:47 PM, Ken G. wrote:

I just emailed that I was unable to correct a message in ModTools
so I went to Yahoo and made the change and then approved it.

Noticing it did not appear on the list, I checked the Activity Log
in Yahoo and it was marked Bounced!

Several days ago, we had another message correction and that
too, bounced.



It's conceivable that you're referring to:

https://pypi.python.org/pypi/modtools/1.0.2

But your message is so garbled that I have no idea how to respond.

You have a gmail address, but you're somehow involved with yahoo.

You refer to "the list," but never say if it's this one.

You refer to "message correction" but there's no standard way to modify 
a message on python-tutor once it's mailed or posted. So perhaps the 
message correction and the list you're referring to are somewhere else.


Python-tutor is a limited mailing list that only takes messages from 
those who have subscribed/joined.  So perhaps you have two different 
email addresses and you've registered using gmail, and are trying to 
post using yahoo.


In any case, if it is ModTools, this is the wrong list to post 
questions.  This list is intended for usage of the Python language and 
the standard library, and while other subjects are permissible, they're 
not as likely to get good responses.


I'd suggest you email to python-l...@python.org a new message, starting 
a new thread.  Be explicit about your question, supplying a URL when you 
refer to a 3rd party package, and giving python version and OS version. 
 Then show exactly what you've tried and what the result was, posting a 
full traceback if you got an exception.


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