Re: [Tutor] inserting degrees symbol in plot title

2011-08-09 Thread Steve Willoughby

On 09-Aug-11 20:11, questions anon wrote:

I am creating a number of plots of temperature in degrees celsius and I
would like the title of the plot to have -  C°



UnicodeDecodeError: 'ascii' codec can't decode byte 0xb0 in position 19:
ordinal not in range(128)


Putting the Unicode character in your program is one thing, but what 
library functions you pass that data to is a factor, too.  Without 
knowing anything about matplotlib, my guess from the exception you 
quoted here is that the library is expecting ASCII characters below 128, 
so that function won't accept the special character.



--
Steve Willoughby / st...@alchemy.com
"A ship in harbor is safe, but that is not what ships are built for."
PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] inserting degrees symbol in plot title

2011-08-09 Thread questions anon
I am creating a number of plots of temperature in degrees celsius and I
would like the title of the plot to have -  C°
Through a search I found a way to print the symbol

degree_symbol = unichr(176).encode("latin-1")
print degree_symbol

but when I add this to my plot title I receive a huge error (see below). I
have also tried copying (from word) and pasting the symbol into my title
(like I did at the beginning of this email) but this also receives a huge
error!
Any feedback will be greatly appreciated.


Traceback (most recent call last):
  File "D:\My Dropbox\Python_code\analysis_netcdf_subplots.py", line 75, in

plt.show()
  File "C:\Python27\lib\site-packages\matplotlib\backend_bases.py", line 74,
in __call__
manager.show()
  File "C:\Python27\lib\site-packages\matplotlib\backends\backend_wx.py",
line 1557, in show
self.canvas.draw()
  File "C:\Python27\lib\site-packages\matplotlib\backends\backend_wxagg.py",
line 59, in draw
FigureCanvasAgg.draw(self)
  File "C:\Python27\lib\site-packages\matplotlib\backends\backend_agg.py",
line 394, in draw
self.figure.draw(self.renderer)
  File "C:\Python27\lib\site-packages\matplotlib\artist.py", line 55, in
draw_wrapper
draw(artist, renderer, *args, **kwargs)
  File "C:\Python27\lib\site-packages\matplotlib\figure.py", line 798, in
draw
func(*args)
  File "C:\Python27\lib\site-packages\matplotlib\artist.py", line 55, in
draw_wrapper
draw(artist, renderer, *args, **kwargs)
  File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 1946, in
draw
a.draw(renderer)
  File "C:\Python27\lib\site-packages\matplotlib\artist.py", line 55, in
draw_wrapper
draw(artist, renderer, *args, **kwargs)
  File "C:\Python27\lib\site-packages\matplotlib\text.py", line 526, in draw
bbox, info = self._get_layout(renderer)
  File "C:\Python27\lib\site-packages\matplotlib\text.py", line 305, in
_get_layout
clean_line, ismath = self.is_math_text(line)
  File "C:\Python27\lib\site-packages\matplotlib\text.py", line 989, in
is_math_text
if cbook.is_math_text(s):
  File "C:\Python27\lib\site-packages\matplotlib\cbook.py", line 1793, in
is_math_text
s = unicode(s)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb0 in position 19:
ordinal not in range(128)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] General method for creating a partitioned window

2011-08-09 Thread Prasad, Ramit
> Say I want one half of a 720 x 480 window be dedicated for
> creating a
> 2-D structure, and the other half for a 3-D structure. There
> would be a
> line 1 or 2 pixels thick straight down the window that would
> divide the
> two sectors. How does one go about doing this without creating two
> separate GUI window frames?


See SplitterWindow 

http://www.wxpython.org/docs/api/wx.SplitterWindow-class.html OR 
http://www.wxpython.org/docs/api/wx.lib.splitter.MultiSplitterWindow-class.html 



Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423


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


Re: [Tutor] General method for creating a partitioned window

2011-08-09 Thread Alan Gauld

On 09/08/11 15:44, Shwinn Ricci wrote:


I am using wxPython and OpenGL,


I don't know anything about how OpenGL works with wxPython.
However in general one of the following strategies should work:

1) Create composite panel containing two child panels side by side.
   Create your graphics in the child panels using the OpenGL library.
2) Create a single panel and general two images using OpenGL and
   then blit those two images side by side on the panel.

Hopefully somebody with more OpenGL experience can give more direct help...

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




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


Re: [Tutor] General method for creating a partitioned window

2011-08-09 Thread Knacktus

Am 09.08.2011 16:44, schrieb Shwinn Ricci:


I am using wxPython and OpenGL, although I would prefer to run a script
in Maya, but this does not seem efficient for what I want to do, since
maya only allows for 3-D visualization, not simultaneous 2-D/3-D viewing
(i.e., side-by-side)



On Thu, Aug 4, 2011 at 7:37 PM, Alan Gauld mailto:alan.ga...@btinternet.com>> wrote:

On 04/08/11 20:52, Shwinn Ricci wrote:

Say I want one half of a 720 x 480 window be dedicated for
creating a
2-D structure, and the other half for a 3-D structure. There
would be a
line 1 or 2 pixels thick straight down the window that would
divide the
two sectors. How does one go about doing this without creating two
separate GUI window frames?


You would need one frame (or widget) with an opengl context with 2 
independent viewports. I don't think that's supported by any framework. 
You probably have to use two widgets. But that's no problem, as long as 
you can use layouts and styles to get the desired appearance of the two 
widgets (i.e. to make them appear as one).


I do not have my code with me right

now, I
can post as soon as I have access to it.


You need to give yus more detail. This kind of thing is very GUI
frame-work specific. Are you using Tkinter? wxPython?, Cocoa on MacOSX?

They are all different.

And because you want to do graphics you also need to tell us what
graphics toolkit you are using? OpenGL? Something else?

Or are you trying to script Blender or Maya or some other 2D/3D
modelling tool?


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



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





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


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


Re: [Tutor] precision?

2011-08-09 Thread Chris Fuller
There's the one that comes with Python: 
http://docs.python.org/library/functions.html#round

You should consider working with integers, though.  Multiply all the values by 
1000 and drop the fractional part.

Cheers

On Tuesday 09 August 2011, Shwinn Ricci wrote:
> When comparing a given value with a database of values, but allowing for
> freedom due to variation at say, the thousandth digit, how does one
> generalize the precision to this degree? I don't want to truncate, so is
> there a round() function that takes into account what decimal place the
> value (database value) needs to be rounded to?

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


Re: [Tutor] precision?

2011-08-09 Thread Steven D'Aprano

Shwinn Ricci wrote:

When comparing a given value with a database of values, but allowing for
freedom due to variation at say, the thousandth digit, how does one
generalize the precision to this degree? I don't want to truncate, so is
there a round() function that takes into account what decimal place the
value (database value) needs to be rounded to?


Thousandth digit??? Python doesn't support floating point numbers with a 
thousand digits! I think about seventeen is about the limit.



Yes, there is a round function:


>>> x = 123.45678
>>> y = 123.45731
>>> x == y
False
>>> round(x, 3) == round(y, 3)
True




--
Steven

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


[Tutor] precision?

2011-08-09 Thread Shwinn Ricci
When comparing a given value with a database of values, but allowing for
freedom due to variation at say, the thousandth digit, how does one
generalize the precision to this degree? I don't want to truncate, so is
there a round() function that takes into account what decimal place the
value (database value) needs to be rounded to?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] General method for creating a partitioned window

2011-08-09 Thread Shwinn Ricci
I am using wxPython and OpenGL, although I would prefer to run a script in
Maya, but this does not seem efficient for what I want to do, since maya
only allows for 3-D visualization, not simultaneous 2-D/3-D viewing (i.e.,
side-by-side)



On Thu, Aug 4, 2011 at 7:37 PM, Alan Gauld wrote:

> On 04/08/11 20:52, Shwinn Ricci wrote:
>
>> Say I want one half of a 720 x 480 window be dedicated for creating a
>> 2-D structure, and the other half for a 3-D structure. There would be a
>> line 1 or 2 pixels thick straight down the window that would divide the
>> two sectors. How does one go about doing this without creating two
>> separate GUI window frames? I do not have my code with me right now, I
>> can post as soon as I have access to it.
>>
>
> You need to give yus more detail. This kind of thing is very GUI frame-work
> specific. Are you using Tkinter? wxPython?, Cocoa on MacOSX?
>
> They are all different.
>
> And because you want to do graphics you also need to tell us what graphics
> toolkit you are using? OpenGL? Something else?
>
> Or are you trying to script Blender or Maya or some other 2D/3D modelling
> tool?
>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Python mechanize: Auto follow meta refresh?

2011-08-09 Thread Troels Emtekær Linnet
Hi.

I try to to make a python interaction with the form on:
http://propka.ki.ku.dk/
I just try to set: version='v3.1' and PDBID='4AKE'

After the click and sending the form it stops.
It should automatically refresh.

Here is my code:

import mechanize, sys, time, os

url = "http://propka.ki.ku.dk/";
version = "v3.1"
PDBID = "4AKE"
verbose = 'yes'
server_wait = 1.0

br = mechanize.Browser()
br.addheaders = [('User-agent', 'pythonMechanizeClient')]
### The things was added according to:
http://stockrt.github.com/p/emulating-a-browser-in-python-with-mechanize/
### Dont know what they do.
br.set_handle_equiv(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
# Turn on debugging messages
br.set_debug_http(True)
#br.set_debug_redirects(True)
#br.set_debug_responses(True)

openpage = br.open(url)
readpage = openpage.read()
if verbose == 'yes': print br.title(); print br.geturl()
#print readpage

# To get available forms
forms = [f.name for f in br.forms()]
if verbose == 'yes': print forms

## Select first form
br.select_form(name=forms[0])
## Print the parameters of the 'version' RadioControl button and current
value
if verbose == 'yes': print br.find_control(name='version'); print
br.find_control(name='version').value
## Change the parameters of the 'version' radio button and then reprint the
new value
br.form['version'] = [version]
if verbose == 'yes': print br.find_control(name='version').value#
## Print the parameters of the 'PDBID' TextControl button and current value
if verbose == 'yes': print br.find_control(name='PDBID'); print
br.find_control(name='PDBID').value
## Change the parameters of the 'PDBID' TextControl and then reprint the new
value
br.form["PDBID"] = PDBID
if verbose == 'yes': print br.find_control(name='PDBID').value
## Now reprint the current selected form, so we see that we have the right
values.
if verbose == 'yes': print br.form

## Two ways of submitting is tested
#br.submit()
req = br.click(type="submit", nr=0)

### Have to pass by a mechanize exception. Thats the reason for the why True
### The error was due to:
#br.open(req)
# mechanize._response.httperror_seek_wrapper: HTTP Error refresh: The
HTTP server returned a redirect error that would lead to an infinite loop.
# The last 30x error message was:
# OK
while True:
try:
br.open(req)
break
### If there is raised an exception, we make a break.
except:
print("Waiting %s seconds for server"%server_wait)
time.sleep(server_wait)
break
print br.geturl()
print br.title()
print br.response().read()
## Looking at some results in link format
for l in br.links():
print l
#for l in br.links(url_regex='stockrt'):
#print l
*
It returns:*



PROPKA Progress
http://propka.ki.ku.dk/baker.css";
type="text/css">



PROPKA Progress
The PROPKA server is generating your results - this page will automatically
refresh every 1 seconds.
Thank you for your patience!
Server Progress:

Elapsed Time: 19.00 seconds

Server Information:

Server load: 1.72 (1min)  0.86 (5min)  0.41
(15min)
Server time: Tue Aug  9 15:33:56 2011



*And it should be send forward to a result page afterwards.
*I think it is because I got exception error.
# mechanize._response.httperror_seek_wrapper: HTTP Error refresh: The
HTTP server returned a redirect error that would lead to an infinite loop.
# The last 30x error message was:
# OK

and I stop after this exception error.
Is there a way to prolong the allowance for refreshing?



Troels Emtekær Linnet
Karl-Liebknecht-Straße 53, 2 RE 
04107 Leipzig, Tyskland
Mobil: +49 1577-8944752
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Reprojection tool for vector datasets

2011-08-09 Thread Steven D'Aprano

Helen Brown wrote:

I am such a novice at this but I have gotten to a place where I am
stuck. The attachment is what I have done so far but it also has an
error for indentation. I will be working on that to. I have place
comment as to what I want the modules to do but that is as far as I
can go. Can someone lead me to the finish line ? I have four datasets
and they are supposed to be reproject to NAD 1983 UTM Zone 10. Plus I
can not have any hard code.


I don't understand what you mean by "I can not have any hard code".


The first thing I would do is get rid of the try...except clause. You 
have this:


try:
[lots and lots of indented code]
except:
 # Report an error messages
arcpy.AddMessage("This project is incomplete")
#Report any error messages that the projection might have generated
arcpy.AddMessage(arcpy.GetMessages())



This is a bad design, because it hides genuine programming bugs and 
changes them to say "This project is incomplete". How useless is that? 
If your code has a bug, you need to see what the bug is before you can 
fix it. So I recommend you do this:



 (1) Take all the lines between "try" and "except" and outdent them 
back to the margin.


(2) Delete the line "try".

(3) Delete the line "except".

(4) Comment out the four lines following "except"

## Report an error messages
#arcpy.AddMessage("This project is incomplete")
##Report any error messages that the projection might have generated
#arcpy.AddMessage(arcpy.GetMessages())

(rather than delete them, because you may have to add them back in later).


Now run the code and see what errors you get, if any, and fix them. 
You'll almost certainly get lots of errors, but only one at a time, 
fortunately! Read the error messages, try to understand what they are 
telling you, and try to fix them.


And don't be shy about coming back for assistance.




--
Steven

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


Re: [Tutor] function remember calls?

2011-08-09 Thread Peter Otten
Robert Johansson wrote:

> This is my attempt to generate all pairs of relatively prime pairs of
> nonnegative integers (n, m) such that n + m <= N, using the Stern-Brocot
> tree.
> 
> def rp_bounded_sum(n, i = 0, p = (0, 1), q = (1, 1), S = set([(0, 1), (1,
> 1)])):
> # Returns a set S with all relatively prime pairs (a, b)
> # such that 0 <= a <= b and a + b <= n.
> r = p[0] + q[0], p[1] + q[1]
> if r[0] + r[1] <= n:
> S.add(r)
> rp_bounded_sum(n, 1, p, r, S)
> rp_bounded_sum(n, 1, r, q, S)
> if not i:
> return S

Default values for function arguments are shared across function 
invocations. If you have mutable defaults, e. g. a list or set, and modify 
that default value during the function call the modifications will be 
visible the next time you invoke the function. A simple example:

>>> def f(x, items=[]):
... items.append(x)
... return items
...
>>> f(1)
[1]
>>> f(2)
[1, 2]
>>> f(3)
[1, 2, 3]
>>> f(1, [42])
[42, 1]
>>> f(4)
[1, 2, 3, 4]

The standard idiom to avoid this issue is to use a special default value to 
signal that the argument was not provided. In most cases that special value 
is None:

>>> def f(x, items=None):
... if items is None:
... items = [] # if no list was provided make a new one 
... items.append(x)
... return items
...
>>> f(1)
[1]
>>> f(2)
[2]
>>> f(3)
[3]
>>> f(1, [42])
[42, 1]
>>> f(4)
[4]


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


Re: [Tutor] Reprojection tool for vector datasets

2011-08-09 Thread Alan Gauld

On 09/08/11 02:00, Helen Brown wrote:

...I have place
comment as to what I want the modules to do but that is as far as I can
go. Can someone lead me to the finish line ?


The problem is that your code is wholly reliant on the classes that you 
are using and unless you find somebody who is familiar witht those 
classes it is unlikely we can suggest much of help.


Based on the comments your basic logic appears to be ok, but how you 
turn the comments into reality depemds on the classes and their 
available operations. You need to study those classes and any associated 
documentation/sample code.


HTH

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



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


Re: [Tutor] function remember calls?

2011-08-09 Thread Alan Gauld

On 09/08/11 08:44, Robert Johansson wrote:


def rp_bounded_sum(n, i = 0, p = (0, 1), q = (1, 1), S = set([(0, 1),
(1, 1)])):



Strangely, (to me) it seems that the function remembers the set S
between different calls:


That's correct. Python evaluates the default parameter value once at 
function definition time so every call to the function uses the same 
default object value. This often catches out newbies but should be 
considered a 'feature of the language' rather than a bug - it's 
existence  enables some useful techniques which would not otherwise be 
possible :-)


If you check the documentation on default parameters you will find more 
details.


hth,

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



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


[Tutor] function remember calls?

2011-08-09 Thread Robert Johansson
Hi all,

This is my attempt to generate all pairs of relatively prime pairs of 
nonnegative integers (n, m) such that n + m <= N, using the Stern-Brocot tree.

def rp_bounded_sum(n, i = 0, p = (0, 1), q = (1, 1), S = set([(0, 1), (1, 1)])):
# Returns a set S with all relatively prime pairs (a, b)
# such that 0 <= a <= b and a + b <= n.
r = p[0] + q[0], p[1] + q[1]
if r[0] + r[1] <= n:
S.add(r)
rp_bounded_sum(n, 1, p, r, S)
rp_bounded_sum(n, 1, r, q, S)
if not i:
return S

Strangely, (to me) it seems that the function remembers the set S between 
different calls:

>>> T=rp_bounded_sum(200)
>>> len(T)
6117
>>> T=rp_bounded_sum(100)
>>> len(T)
6117

Why?

I modified it to

def rp_bounded_sum(n, i = 0, p = (0, 1), q = (1, 1), S = set()):
# Returns a set S with all relatively prime pairs (a, b)
# such that 0 <= a <= b and a + b <= n.
if not i:
S = set([(0, 1), (1, 1)])
r = p[0] + q[0], p[1] + q[1]
if r[0] + r[1] <= n:
S.add(r)
rp_bounded_sum(n, 1, p, r, S)
rp_bounded_sum(n, 1, r, q, S)
if not i:
return S

Now it works fine but I don't understand why the first example fails. I'm not 
very pleased with any of these two solutions so I would be grateful for any 
suggestions of improvements. I use Python 2.7.2 under Windows 7 and I ran this 
in Python shell in IDLE.

Cheers, Robert

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


Re: [Tutor] How to get the keys of a dict inside a dict. ValueError: too many values to unpack

2011-08-09 Thread Peter Otten
Kayode Odeyemi wrote:

> Thanks so much. This is exactly what I'm looking for. In addition, since
> fields is obviously a dict, I won't want to have to display it's keys
> repeatedly. Is there a way to get the keys once, have it displayed and
> used as columns, then it's values are displayed beneath it. Something
> like:
> 
> updated tel
> 2011 3456
> 2011 34510
> 
> right now, I'm having it displayed as
> 
> updated
> 2011
> tel
> 3456
> updated
> 2011
> tel
> 34510
> 
> I am having the fields being written using a writer like this:
> 
> x = {'pk': 1L, 'model': 'trans', 'fields': {'updated': 2011, 'tel':
> 3456}},
> {'pk': 2L, 'model': 'trans2', 'fields': {'updated': 2011, 'tel': 34510}}
> writer = csv.writer(response)
> 
> for entry in x:
>for key, value in entry.items():
>print key
>if key == "fields":
>for field_key, field_value in value.items():
>d = [field_key] #a list of sequence
>x = [field_value]
>writer.writerow(d)
>writer.writerow(x)
>else:
>print "  %s" % value

You are doing too much at once. You are only interested in the "fields" 
dictionary, so I recommend that you extract that first:

items = [
{'pk': 1, 'model': 'trans', 'fields': {'updated': 2011, 'tel': 3456}},
{'pk': 2, 'model': 'trans2', 'fields': {'updated': 2011, 'tel': 34510}}
]

rows = [item["fields"] for item in items]

Now the data structure has become simpler, just a list of dictionaries, you 
can think about how you generate the output. One way, the str.format() 
method, was already mentioned:

>>> for row in rows:
... print "{updated}\t{tel}".format(**row)
...
20113456
201134510

If you want to continue to use the csv module you should use the DictWriter:

>>> import csv, sys
>>> fieldnames = rows[0].keys()
>>> writer = csv.DictWriter(sys.stdout, fieldnames)
>>> writer.writeheader()
updated,tel
>>> writer.writerows(rows)
2011,3456
2011,34510


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