[Tutor] Plot trajectories on an map using matplotlib-basemap

2013-01-28 Thread Boris Vladimir Comi

#! /usr/bin/python
import numpy as np
data = np.loadtxt('path-tracks.csv',dtype=np.str,delimiter=',',skiprows=1)
print data

 [['19.70' '-95.20' '2/5/04 6:45 AM' '1' '-38' 'CCM']
 ['19.70' '-94.70' '2/5/04 7:45 AM' '1' '-48' 'CCM']
 ['19.30' '-93.90' '2/5/04 8:45 AM' '1' '-60' 'CCM']
 ['19.00' '-93.50' '2/5/04 9:45 AM' '1' '-58' 'CCM']
 ['19.00' '-92.80' '2/5/04 10:45 AM' '1' '-50' 'CCM']
 ['19.20' '-92.60' '2/5/04 11:45 AM' '1' '-40' 'CCM']
 ['19.90' '-93.00' '2/5/04 12:45 PM' '1' '-43' 'CCM']
 ['20.00' '-92.80' '2/5/04 1:15 PM' '1' '-32' 'CCM']
 ['23.10' '-100.20' '30/5/04 4:45 AM' '2' '-45' 'SCME']
 ['23.20' '-100.00' '30/5/04 5:45 AM' '2' '-56' 'SCME']
 ['23.30' '-100.00' '30/5/04 6:45 AM' '2' '-48' 'SCME']
 ['23.30' '-100.20' '30/5/04 7:45 AM' '2' '-32' 'SCME']
 ['23.40' '-99.00' '31/5/04 3:15 AM' '3' '-36' 'SCM']
 ['23.50' '-98.90' '31/5/04 4:15 AM' '3' '-46' 'SCM']
 ['23.60' '-98.70' '31/5/04 5:15 AM' '3' '-68' 'SCM']
 ['23.70' '-98.80' '31/5/04 6:15 AM' '3' '-30' 'SCM']]

 with the above code I get an array whose columns represent: [Lat, Lon, Date, 
Identifier, Temperatures, Category]. Now, I will put a code that allows me to 
plot the first and second column on the map of Mexico:



#!/usr/bin/python
#Project Storm: Plot trajectories of convective systems
#import libraries

import numpy as np
from mpl_toolkits.basemap import Basemap
import  matplotlib.pyplot as pl

# Plot a map for Mexico

m = Basemap(projection='cyl', llcrnrlat=12, urcrnrlat=35,llcrnrlon=-120, 
urcrnrlon=-80, resolution='c', area_thresh=1000.)
m.bluemarble()
m.drawcoastlines(linewidth=0.5)
m.drawcountries(linewidth=0.5)
m.drawstates(linewidth=0.5)

#Draw parallels and meridians

m.drawparallels(np.arange(10.,35.,5.))
m.drawmeridians(np.arange(-120.,-80.,10.))
m.drawmapboundary(fill_color='aqua')

#Open file whit numpy

data = np.loadtxt('path-tracks.csv', dtype=np.str,delimiter=' , ', 
skiprows=1)
latitude = data[:,0]
longitude = data[:,1]

#Convert latitude and longitude to coordinates X and Y

x, y = m(longitude, latitude)

#Plot the points on the map

pl.plot(x,y,'ro-')
pl.show()



The points plotted on the map, corresponding to three different paths with a 
line connecting all points. Mi final idea is to draw a line connecting the 
points associated with each path, How  I can do this? or How should I structure 
my data to plot the different paths?

is posible draw an identifier or a mark for each path?


how I can set the size of the figure so that it can distinguish the separation 
between the points?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Set Reply-To field to Tutor@python.org

2013-01-28 Thread DoanVietTrungAtGmail
As a student user of this list, I prefer leaving the Reply-To field
unchanged. I like the fact that this means a deliberate  decision is
required to send public emails.

Trung

On Tue, Jan 29, 2013 at 12:17 PM, Steven D'Aprano wrote:

> On 29/01/13 09:04, Oscar Benjamin wrote:
>
>  One particular list that I receive has recently made an administrative
>> change so that, from now on, all emails have the "Reply-To" header set
>> to the list address. This means that the default behaviour when
>> replying to a message is that the reply goes to the list. I think that
>> this is the right thing to do by default since replying off-list is
>> much less common and more likely to be something that you are
>> consciously aware of when you do it.
>>
>> The change seems to have gone down well on the other list so I
>> wondered: could it be done for this list as well?
>>
>
>
> http://www.unicom.com/pw/**reply-to-harmful.html
> http://woozle.org/~neale/**papers/reply-to-still-harmful.**html
>
> versus
>
> http://www.betacantrips.com/**bits/reply_to_munging_**
> considered_harmful_considered_**infuriating/
> http://www.metasystema.net/**essays/reply-to.mhtml
>
>
>
> --
> Steven
>
> __**_
> 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] Set Reply-To field to Tutor@python.org

2013-01-28 Thread Steven D'Aprano

On 29/01/13 09:04, Oscar Benjamin wrote:


One particular list that I receive has recently made an administrative
change so that, from now on, all emails have the "Reply-To" header set
to the list address. This means that the default behaviour when
replying to a message is that the reply goes to the list. I think that
this is the right thing to do by default since replying off-list is
much less common and more likely to be something that you are
consciously aware of when you do it.

The change seems to have gone down well on the other list so I
wondered: could it be done for this list as well?



http://www.unicom.com/pw/reply-to-harmful.html
http://woozle.org/~neale/papers/reply-to-still-harmful.html

versus

http://www.betacantrips.com/bits/reply_to_munging_considered_harmful_considered_infuriating/
http://www.metasystema.net/essays/reply-to.mhtml



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


Re: [Tutor] automate add-to-cart with python

2013-01-28 Thread ALAN GAULD



>> Or maybe, just maybe, somebody has written a javascript interpreter in
>> Python as a module that you can import. It sounds just about crazy enough
>> that it might even exist! But as I said, that's probably the wrong thing to
>> do...
>
>A direct approach would probably use the webdriver API to automate the
>browser.  Selenium has one:
>
>    http://selenium.googlecode.com/svn/trunk/docs/api/py/index.html
>
>
>
>You could use COM to drive IE on Windows or Applescript to drive 
a MacOS browser. No idea what you'd do on Linux...

But direct API or even a simple CGI url call would be better.

Alan g.

PS. 
Nice to see you posting on tutor again Danny :-)___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] automate add-to-cart with python

2013-01-28 Thread Danny Yoo
> Or maybe, just maybe, somebody has written a javascript interpreter in
> Python as a module that you can import. It sounds just about crazy enough
> that it might even exist! But as I said, that's probably the wrong thing to
> do...

A direct approach would probably use the webdriver API to automate the
browser.  Selenium has one:

http://selenium.googlecode.com/svn/trunk/docs/api/py/index.html

That being said, I agree with you that this is most likely an overkill
approach.  Better to see if the site has an officially supported
REST-style API that can be more easily mechanized.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] automate add-to-cart with python

2013-01-28 Thread Alan Gauld

On 28/01/13 22:51, Benjamin Fishbein wrote:

In general, despite scouring the Internet, I haven't been able to figure
out how to get Python to cause javascripts I find in the html to be run.


That's because Javascript is run in the browser which has a Javascript 
interpreter built in. Python doesn't have a Javascript interpreter so 
can't run Javascript.


But that's usually the wrong thing to do anyway so it doesn't matter. 
What you need to do is figure out what the Javascript is doing - usually 
sending some data to a server - and replicate that.


Alternatively fire up a real browser and drive that with automation. How 
you do that will largely depend on the browser and the OS.


Or maybe, just maybe, somebody has written a javascript interpreter in 
Python as a module that you can import. It sounds just about crazy 
enough that it might even exist! But as I said, that's probably the 
wrong thing to do...


--
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] automate add-to-cart with python

2013-01-28 Thread Benjamin Fishbein
Hello,
I'm trying to automate putting a series of items into my cart. Here's the html 
for the add-to-cart button on the website:
http://cart.half.ebay.com/ws/eBayISAPI.dll?HalfAddItemToCart&ap=additem&itemid=343343950913&pr=57017296&fromshop=1";>Add
 to cart
Using the cookielib and urllib2 module, I did:

a= text.find("href")+6
b = text.find('"', a)
url = text[a:b]
new_text = opener.open(url).read()

It goes to the page that shows my cart, but there are zero items--the item is 
not added to the cart.
I'm guessing this has something to do with:

class="PDP_button_addToCart2"

I'm not sure how to have Python click the button.
In general, despite scouring the Internet, I haven't been able to figure out 
how to get Python to cause javascripts I find in the html to be run.
Any help would be greatly appreciated.
Thanks,
Ben

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


Re: [Tutor] Help!

2013-01-28 Thread Joel Goldstick
On Mon, Jan 28, 2013 at 4:51 PM, Danny Yoo  wrote:

> > Hi guys I wanted to make a program called Binary/decimal converter. But
> I want to do it the hard way e.g. not using built in python functions. Can
> you give me an idea about how I can do that?
>
>
> See if you can write the steps to do this by hand.  You take binary
number -- say 10110 and convert it to decimal.  If you can do that with pad
and pencil, you are off to a good start.  If you can't, then you have to
learn that first.  Its not magic.  Learning about bit shifting will help



> Do you have an idea of what kind of things would be useful test cases
> for this converter?  Thinking about this may help solidify what it is
> you're trying to do.  By it, we want to help you express concretely
> what you mean when you say "binary/decimal converter".
>
> ---
>
> For example, if I wanted to write a program to convert "snow" to
> "water", I might start like this:
>
> I want to write a program to take words like "snow" and rewrite them
> to "water".  But anything else should stay the same.  Let me give a
> name to this.  Call it "melt".  Here are some examples I'd like to
> make work (or not work).
>
> melt("The snow is cold!")  ==> "The water is cold!"
> melt("The snowflakes are falling") ==>  "The snowflakes are falling"
> melt("Snow and ice") ==>  "Water and ice"
>
> That is, I want to make sure the translation is case sensitive, but
> only applies when the whole word "snow" shows up.
>
>
> ... etc.  A potential approach might use regular expression
> replacement, with a little bit of care about using a function for the
> replacement argument so we can handle the weird uppercasing
> requirement...
>
> ---
>
>
> If you plan like this, and include concrete test cases, then you'll
> have a better shot at solving the problem.
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



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


Re: [Tutor] Help!

2013-01-28 Thread Danny Yoo
> Hi guys I wanted to make a program called Binary/decimal converter. But I 
> want to do it the hard way e.g. not using built in python functions. Can you 
> give me an idea about how I can do that?


Do you have an idea of what kind of things would be useful test cases
for this converter?  Thinking about this may help solidify what it is
you're trying to do.  By it, we want to help you express concretely
what you mean when you say "binary/decimal converter".

---

For example, if I wanted to write a program to convert "snow" to
"water", I might start like this:

I want to write a program to take words like "snow" and rewrite them
to "water".  But anything else should stay the same.  Let me give a
name to this.  Call it "melt".  Here are some examples I'd like to
make work (or not work).

melt("The snow is cold!")  ==> "The water is cold!"
melt("The snowflakes are falling") ==>  "The snowflakes are falling"
melt("Snow and ice") ==>  "Water and ice"

That is, I want to make sure the translation is case sensitive, but
only applies when the whole word "snow" shows up.


... etc.  A potential approach might use regular expression
replacement, with a little bit of care about using a function for the
replacement argument so we can handle the weird uppercasing
requirement...

---


If you plan like this, and include concrete test cases, then you'll
have a better shot at solving the problem.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help!

2013-01-28 Thread Mark K. Zanfardino

Ghadir,

I did a quick google search for how to convert digital to binary. The 
first link was to 
http://www.ehow.com/how_5164721_convert-digital-binary.html which gives 
a pretty clear example of the process for converting digital to binary.  
Of course, you will need to translate this psuedo-code into an algorithm 
using Python.


Cheers!
Mark K. Zanfardino

On 01/28/2013 01:22 PM, Ghadir Ghasemi wrote:

Hi guys I wanted to make a program called Binary/decimal converter. But I want 
to do it the hard way e.g. not using built in python functions. Can you give me 
an idea about how I can do that?
Thank you.
___
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] Help!

2013-01-28 Thread Ghadir Ghasemi
Hi guys I wanted to make a program called Binary/decimal converter. But I want 
to do it the hard way e.g. not using built in python functions. Can you give me 
an idea about how I can do that?
Thank you.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question on re.findall usage

2013-01-28 Thread Mitya Sirenef

On 01/28/2013 03:19 PM, Dave Wilder wrote:


> On 28 January 2013 2:44, : Oscar Benjamin 
[mailto:oscar.j.benja...@gmail.com wrote:

>
> Please post in plain text (not html) as otherwise the code gets 
screwed up.

> ...
>
> Some people like to use regexes for everything. I prefer to try 
string methods first as I find them easier to understand.

> Here's my attempt:
 junk_list = 'tmsh list net interface 1.3 media-ca 
\rpabilities\r\nnet interface 1.3 {\r\n media-capabilities {\r\n 
none\r\n auto\r\n 4SR4-FD\r\n 10T-HD\r\n 100TX-FD\r\n 100TX-HD\r\n 
1000T-FD\r\n 4LR4-FD\r\n 1000T-HD\r\n }\r\n}\r\n'

 junk_list = [s.strip() for s in junk_list.splitlines()] junk_list =
 [s for s in junk_list if s == 'auto' or s[:2] in ('10', '40')]
 junk_list
> ['auto', '4SR4-FD', '10T-HD', '100TX-FD', '100TX-HD', '1000T-FD', 
'4LR4-FD', '1000T-HD']

>
> Does that do what you want?
>
>
> Oscar
>
>
> *
>
> Got it Oscar. Thank you for your respectful corrections and your 
solution.
> I used "Rich Text" which is what I thought was recommended by the 
list gurus at one point. Plain Text it is then.

>
> Your response definitely does the trick and I can use that as a base 
for the future.

>
> As per Joel's comment that it is a variation of questions I asked in 
the past, right you are. I had to put this away for a while and am 
picking it up again now.

> I will get string manipulation / RegEx educated.
>
> Thank You,
>
> Dave

I would like to emphasize that regex is an entirely wrong approach for
this task. The reason is that it's very brittle, hard to read, hard to
debug and update if the text file changes. The first step should be to
simplfy the task -- in this case, to split the string into lines and
strip each line.

List comps as shown by Oscar is the best approach, but if you don't feel
comfortable with list comps, you can then use a loop:

lst = []
for line in junk_list:
if line == "auto"  : lst.append(line)
elif line.startswith("10") : lst.append(line)
elif line.startswith("40") : lst.append(line)

You could even use a regex as part of the loop:

for line in junk_list:
if re.match("^(auto|10|40)", line):
lst.append(line)

It's still much better than using a humongous regex.

 - m


--
Lark's Tongue Guide to Python: http://lightbird.net/larks/

When a friend succeeds, I die a little.  Gore Vidal

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


Re: [Tutor] Question on re.findall usage

2013-01-28 Thread Dave Wilder

On 28 January 2013 2:44, : Oscar Benjamin [mailto:oscar.j.benja...@gmail.com 
wrote:

Please post in plain text (not html) as otherwise the code gets screwed up. 
...

Some people like to use regexes for everything. I prefer to try string methods 
first as I find them easier to understand. 
Here's my attempt:
>>> junk_list = 'tmsh list net interface 1.3 media-ca \rpabilities\r\nnet 
>>> interface 1.3 {\r\nmedia-capabilities {\r\nnone\r\n
>>> auto\r\n 4SR4-FD\r\n  10T-HD\r\n100TX-FD\r\n
>>> 100TX-HD\r\n1000T-FD\r\n4LR4-FD\r\n 1000T-HD\r\n
>>> }\r\n}\r\n'
>>> junk_list = [s.strip() for s in junk_list.splitlines()] junk_list = 
>>> [s for s in junk_list if s == 'auto' or s[:2] in ('10', '40')] 
>>> junk_list
['auto', '4SR4-FD', '10T-HD', '100TX-FD', '100TX-HD', '1000T-FD', 
'4LR4-FD', '1000T-HD']

Does that do what you want?


Oscar


*

Got it Oscar.  Thank you for your respectful corrections and your solution.
 I used "Rich Text" which is what  I thought was recommended by the list gurus 
at one point.   Plain Text it is then.

Your response definitely does the trick and I can use that as a base for the 
future.

As per Joel's comment that it is a variation of questions I asked in the past, 
right you are.   I had to put this away for a while and am picking it up again 
now.
I will get string manipulation / RegEx educated.

Thank You,

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


Re: [Tutor] Question on re.findall usage

2013-01-28 Thread Joel Goldstick
On Mon, Jan 28, 2013 at 2:15 PM, Dave Wilder  wrote:

>  Hello,
>
> I am trying using re.findall to parse the string below and then create a
> list from the results.
> junk_list = 'tmsh list net interface 1.3 media-ca \rpabilities\r\nnet
> interface 1.3 {\r\nmedia-capabilities {\r\nnone\r\n
> auto\r\n 4SR4-FD\r\n  10T-HD\r\n100TX-FD\r\n
> 100TX-HD\r\n1000T-FD\r\n4LR4-FD\r\n 1000T-HD\r\n
> }\r\n}\r\n'
>

This looks like a variation on the questions you asked over the last couple
of months.  Printing junk_list I get this:

>>> print junk_list
pabilitiesnet interface 1.3 media-ca
net interface 1.3 {
media-capabilities {
none
auto
 4SR4-FD
  10T-HD
100TX-FD
100TX-HD
1000T-FD
4LR4-FD
 1000T-HD
}
}
How do you get junk_list?  Read from a file?  Is there more in the file
besides what is in junk_list?  Do you have this exact file format every
time?

You might do better to use readline() instead of read(), and strip() each
line so that you don't have the new line issue.  I'm guessing but it looks
like you can toss every line until you get past media-capabilities, and
toss every line that contains }.  But maybe I am reading more into the data
format than is appropriate.

If my guesses are correct, you don't need regex stuff at all, because each
line (that you don't toss) contains something you want, and you can build
you list




> What I am doing now is obviously quite ugly, but I have not yet able to
> manipulate it to work how I want but in a much more efficient and modular
> way.
> I did some research on re.findall but am still confused as to how to do
> character repetition searches, which  I guess is what I need to do here.
> >> junk_list =
> re.findall(r'(auto|[1|4]0+[A-Z]-[HF]D|[1|4]0+[A-Z][A-Z]-[HF]D|[1|4]0+[A-Z][A-Z][0-9])',
> junk_list)
> >> junk_list
> ['auto', '4SR4', '10T-HD', '100TX-FD', '100TX-HD', '4LR4',
> '1000T-FD', '1000T-HD']
> >>>
>
> Basically, all I need to search on is:
>
>- auto
>- anything that starts w/ ‘1’ or ‘4’ and then any number of subsequent
>zeroes   e.g. 10T-HD, 4LR4-FD, 100TX-FD
>
>
> My environment:
> [root@f5ite ~/tests]$ uname -a
> Linux VM-QA-ITE-03 2.6.32-220.17.1.el6.i686 #1 SMP Tue May 15 22:09:39 BST
> 2012 i686 i686 i386 GNU/Linux
> [root@f5ite ~/tests]$
> [root@f5ite ~/tests]$ /usr/bin/python
> Python 2.7 (r27:82500, Jul  6 2010, 02:54:50)
> [GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>
> Any ideas?
>
> Thanks,
>
> Dave
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


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


Re: [Tutor] Question on re.findall usage

2013-01-28 Thread Oscar Benjamin
Please post in plain text (not html) as otherwise the code gets
screwed up. When I paste your code into a terminal this is what
happens:

>>> junk_list = 'tmsh list net interface 1.3 media-ca \rpabilities\r\nnet 
>>> interface 1.3 {\r\nmedia-capabilities {\r\nnone\r\n
>>> auto\r\n 4SR4-FD\r\n  10T-HD\r\n100TX-FD\r\n
>>> 100TX-HD\r\n1000T-FD\r\n4LR4-FD\r\n 1000T-HD\r\n
>>> }\r\n}\r\n'
>>> junk_list
'tmsh list net interface 1.3 media-ca \rpabilities\r\nnet interface
1.3 {\r\n\xc2\xa0\xc2\xa0\xc2\xa0 media-capabilities
{\r\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0
none\r\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0
auto\r\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 4SR4-FD\r\n\xc2\xa0
10T-HD\r\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0
100TX-FD\r\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0
100TX-HD\r\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0
1000T-FD\r\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0
4LR4-FD\r\n \xc2\xa0\xc2\xa0\xc2\xa0
1000T-HD\r\n\xc2\xa0\xc2\xa0\xc2\xa0 }\r\n}\r\n'

Those \xc2\xa0 characters are non-breaking space characters. The
trouble is that I don't know if they were added by your email client
or are actually part of your junk string. I've assumed the former and
replaced them with spaces in the code I show below.

On 28 January 2013 19:15, Dave Wilder  wrote:
> Hello,
>
> I am trying using re.findall to parse the string below and then create a
> list from the results.
> junk_list = 'tmsh list net interface 1.3 media-ca \rpabilities\r\nnet
> interface 1.3 {\r\nmedia-capabilities {\r\nnone\r\n
> auto\r\n 4SR4-FD\r\n  10T-HD\r\n100TX-FD\r\n
> 100TX-HD\r\n1000T-FD\r\n4LR4-FD\r\n 1000T-HD\r\n
> }\r\n}\r\n'
>
> What I am doing now is obviously quite ugly, but I have not yet able to
> manipulate it to work how I want but in a much more efficient and modular
> way.
> I did some research on re.findall but am still confused as to how to do
> character repetition searches, which  I guess is what I need to do here.
>>> junk_list =
>>> re.findall(r'(auto|[1|4]0+[A-Z]-[HF]D|[1|4]0+[A-Z][A-Z]-[HF]D|[1|4]0+[A-Z][A-Z][0-9])',
>>> junk_list)
>>> junk_list
> ['auto', '4SR4', '10T-HD', '100TX-FD', '100TX-HD', '4LR4',
> '1000T-FD', '1000T-HD']

This output doesn't match what I would expect from the string above.
Why is '1000T-FD' after '4LR4-FD'? Is that the problem with the
code you posted?


>
> Basically, all I need to search on is:
>
> auto
> anything that starts w/ ‘1’ or ‘4’ and then any number of subsequent zeroes
> e.g. 10T-HD, 4LR4-FD, 100TX-FD

Does "any number" mean "one or more" or "zero or more"?

Some people like to use regexes for everything. I prefer to try string
methods first as I find them easier to understand. Here's my attempt:

>>> junk_list = 'tmsh list net interface 1.3 media-ca \rpabilities\r\nnet 
>>> interface 1.3 {\r\nmedia-capabilities {\r\nnone\r\n
>>> auto\r\n 4SR4-FD\r\n  10T-HD\r\n100TX-FD\r\n
>>> 100TX-HD\r\n1000T-FD\r\n4LR4-FD\r\n 1000T-HD\r\n
>>> }\r\n}\r\n'
>>> junk_list = [s.strip() for s in junk_list.splitlines()]
>>> junk_list = [s for s in junk_list if s == 'auto' or s[:2] in ('10', '40')]
>>> junk_list
['auto', '4SR4-FD', '10T-HD', '100TX-FD', '100TX-HD', '1000T-FD',
'4LR4-FD', '1000T-HD']

Does that do what you want?


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


[Tutor] Question on re.findall usage

2013-01-28 Thread Dave Wilder
Hello,

I am trying using re.findall to parse the string below and then create a list 
from the results.
junk_list = 'tmsh list net interface 1.3 media-ca \rpabilities\r\nnet interface 
1.3 {\r\nmedia-capabilities {\r\nnone\r\nauto\r\n 
4SR4-FD\r\n  10T-HD\r\n100TX-FD\r\n100TX-HD\r\n
1000T-FD\r\n4LR4-FD\r\n 1000T-HD\r\n}\r\n}\r\n'

What I am doing now is obviously quite ugly, but I have not yet able to 
manipulate it to work how I want but in a much more efficient and modular way.
I did some research on re.findall but am still confused as to how to do 
character repetition searches, which  I guess is what I need to do here.
>> junk_list = 
>> re.findall(r'(auto|[1|4]0+[A-Z]-[HF]D|[1|4]0+[A-Z][A-Z]-[HF]D|[1|4]0+[A-Z][A-Z][0-9])',
>>  junk_list)
>> junk_list
['auto', '4SR4', '10T-HD', '100TX-FD', '100TX-HD', '4LR4', '1000T-FD', 
'1000T-HD']
>>>

Basically, all I need to search on is:
-   auto
-   anything that starts w/ '1' or '4' and then any number of subsequent 
zeroes   e.g. 10T-HD, 4LR4-FD, 100TX-FD

My environment:
[root@f5ite ~/tests]$ uname -a
Linux VM-QA-ITE-03 2.6.32-220.17.1.el6.i686 #1 SMP Tue May 15 22:09:39 BST 2012 
i686 i686 i386 GNU/Linux
[root@f5ite ~/tests]$
[root@f5ite ~/tests]$ /usr/bin/python
Python 2.7 (r27:82500, Jul  6 2010, 02:54:50)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

Any ideas?

Thanks,

Dave

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


Re: [Tutor] Facebook login using python

2013-01-28 Thread Alan Gauld

On 28/01/13 15:40, Aaron Misquith wrote:


like if someone is able to provide me a code to export the output to
pdf.


Oops, sorry ReportLab is the module I was thinking of not pyPDF. The 
latter is mainly for reading PDF not producing them. Apologies.


http://www.reportlab.com/software/opensource/


--
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] Facebook login using python

2013-01-28 Thread Kwpolska
I will kill the one who invented this bullshit with the Reply button.
(sent 1 h 13 min ago)
-- Forwarded message --
From: Kwpolska 
Date: Mon, Jan 28, 2013 at 4:48 PM
Subject: Re: [Tutor] Facebook login using python
To: Aaron Misquith 


On Mon, Jan 28, 2013 at 4:40 PM, Aaron Misquith  wrote:
> I am working on a program which is used to login to facebook, get my friend
> lists using graph api and export them into pdf format.
> So far i'm able to login with facebook and get the friend list. I would like
> if someone is able to provide me a code to export the output to pdf. Another
> thing is that i'm hardcoding username, password and access tokens; is there
> anyway this can be obtained using user input?
>
> Code:
>
> import urllib2, cookielib, re, os, sys
> from facepy import GraphAPI
> class Facebook():
> def __init__(self, email, password):
> self.email = email
> self.password = password
>
> cj = cookielib.CookieJar()
> opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
> opener.addheaders = [('Referer', 'http://login.facebook.com/login.php'),
> ('Content-Type', 'application/x-www-form-urlencoded'),
> ('User-Agent', 'Mozilla/8.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.7)
> Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)')]
>
> self.opener = opener
>
> def login(self):
> url = 'https://login.facebook.com/login.php?login_attempt=1'
> data =
> "locale=en_US&non_com_login=&email="+self.email+"&pass="+self.password+"&lsd=20TOl"
>
> usock = self.opener.open('http://www.facebook.com')
> usock = self.opener.open(url, data)
> if "Logout" in usock.read():
> print "Logged in."
> else:
> print "failed login"
> print usock.read()
> sys.exit()
> f = Facebook("Enter email", "Password")
> f.login()
> graph=GraphAPI('Enter Access code from fql in developer's page')
>
> nik=graph.fql('select uid, name, friends from user where uid in (select
> first_name, middle_name, last_name from friend where uid1 = me())')
>
> print nik
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

1. Code indentation was lost in your message.  Everything below is
based on guessing, guessing and guessing.
2. In order to get user data, do (assuming Python 2):

import getpass
self.email= raw_input('E-mail address: ')
self.password = getpass.getpass('Password: ')
devcode = raw_input(Developer access code: ')

Although you should provide the user with the tokens, using the same
methods REAL developers use, i.e. app authentication.

3. http://lmgtfy.com/?q=python+pdf
--
Kwpolska  | GPG KEY: 5EAAEA16
stop html mail| always bottom-post
http://asciiribbon.org| http://caliburn.nl/topposting.html
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Facebook login using python

2013-01-28 Thread Alan Gauld

On 28/01/13 15:40, Aaron Misquith wrote:

I am working on a program which is used to login to facebook, get my
friend lists using graph api and export them into pdf format.
So far i'm able to login with facebook and get the friend list. I would
like if someone is able to provide me a code to export the output to
pdf.


There is a third part module for this, a Google search for pyPDF should 
find it... and several tutorials.



Another thing is that i'm hardcoding username, password and access
tokens; is there anyway this can be obtained using user input?


raw_input()?

or am I missing something?

--
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] Facebook login using python

2013-01-28 Thread Dave Angel

On 01/28/2013 10:40 AM, Aaron Misquith wrote:

I am working on a program which is used to login to facebook, get my friend
lists using graph api and export them into pdf format.
So far i'm able to login with facebook and get the friend list. I would
like if someone is able to provide me a code to export the output to pdf.
Another thing is that i'm hardcoding username, password and access tokens;
is there anyway this can be obtained using user input?

Code:

import urllib2, cookielib, re, os, sys
from facepy import GraphAPI
class Facebook():


You should derive all classes from object, or from another class that 
already does.  Old-style classes are long deprecated (and eliminated in 
Python 3.x)



def __init__(self, email, password):


Please fix the indentation, and send as a text message, not html.


self.email = email
self.password = password

cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders = [('Referer', 'http://login.facebook.com/login.php'),
('Content-Type', 'application/x-www-form-urlencoded'),
('User-Agent', 'Mozilla/8.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.7)
Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)')]

self.opener = opener

def login(self):
url = 'https://login.facebook.com/login.php?login_attempt=1'
data =
"locale=en_US&non_com_login=&email="+self.email+"&pass="+self.password+"&lsd=20TOl"

usock = self.opener.open('http://www.facebook.com')
usock = self.opener.open(url, data)
if "Logout" in usock.read():
print "Logged in."
else:
print "failed login"
print usock.read()
sys.exit()
f = Facebook("Enter email", "Password")
f.login()
graph=GraphAPI('Enter Access code from fql in developer's page')

nik=graph.fql('select uid, name, friends from user where uid in (select
first_name, middle_name, last_name from friend where uid1 = me())')

print nik


Before doing too much work on such an app, consider if you're violating 
Facebook's terms of service.  The following links may not be the right 
ones, but it could get you started thinking, anyway.


http://developers.facebook.com/blog/post/2013/01/25/clarifying-our-platform-policies/
http://www.facebook.com/help/131112897028467/

As for creating a pdf from a simple text list, I think you'd be best 
shelling out to a program that does just that.  Naturally, the choices 
vary by OS, and I have not recommendations.


As for username and password, why not use raw_input ?



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


Re: [Tutor] sqlite search

2013-01-28 Thread Prasad, Ramit
Alan Gauld wrote:
> 
> On 18/01/13 18:11, Roger wrote:
> 
> > At the moment this works to search for everything beginning with A
> > sql = "SELECT * FROM plants WHERE genus LIKE 'A%'";
> > cursor.execute(sql);
> 
> SQLlite supports a form of format string where you put in some magic
> charactrs then provide arguments which SQLLite will substitute in your
> SQL statement.
> 
> You can see examples of that in the database topic in my tutorial:
> 
> file:///home/alang/Documents/HomePage/tutor/tutdbms.htm
> 
> Look at the address book example near the end for the 'Find Entry'
> feature, and definitely read the 'word about security' subheading for
> the correct way to do it!
> 

Just to clarify, the '%' should go in the parameter string, not
as part of the query string. The query string should retain the '?'
(without any quotations).


~Ramit


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


[Tutor] Facebook login using python

2013-01-28 Thread Aaron Misquith
I am working on a program which is used to login to facebook, get my friend
lists using graph api and export them into pdf format.
So far i'm able to login with facebook and get the friend list. I would
like if someone is able to provide me a code to export the output to pdf.
Another thing is that i'm hardcoding username, password and access tokens;
is there anyway this can be obtained using user input?

Code:

import urllib2, cookielib, re, os, sys
from facepy import GraphAPI
class Facebook():
def __init__(self, email, password):
self.email = email
self.password = password

cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders = [('Referer', 'http://login.facebook.com/login.php'),
('Content-Type', 'application/x-www-form-urlencoded'),
('User-Agent', 'Mozilla/8.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.7)
Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)')]

self.opener = opener

def login(self):
url = 'https://login.facebook.com/login.php?login_attempt=1'
data =
"locale=en_US&non_com_login=&email="+self.email+"&pass="+self.password+"&lsd=20TOl"

usock = self.opener.open('http://www.facebook.com')
usock = self.opener.open(url, data)
if "Logout" in usock.read():
print "Logged in."
else:
print "failed login"
print usock.read()
sys.exit()
f = Facebook("Enter email", "Password")
f.login()
graph=GraphAPI('Enter Access code from fql in developer's page')

nik=graph.fql('select uid, name, friends from user where uid in (select
first_name, middle_name, last_name from friend where uid1 = me())')

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


Re: [Tutor] using ranges with argparse()

2013-01-28 Thread Peter Otten
Kurt Lieber wrote:

> Hi -- brand new to python, but trying to write a simple script that takes
> command line arguments. One of the arguments needs to test if a value is
> a)
> an integer and b) within a stated range.  I currently have:
> 
> parser.add_argument("-f", "--floor", default=6000, help="floor is the
> minimum amount of bonus points.",type=int, choices=range(5995, 6001))
> 
> This works, but when the user enters an out of bounds value, the help
> message is unfriendly:
> 
> % ./bonus.py -f 1
> usage: bonus.py [-h] [-f {5995,5996,5997,5998,5999,6000}]
> MAM-bonus.py: error: argument -f/--floor: invalid choice: 1 (choose
> from 5995, 5996, 5997, 5998, 5999, 6000)
> 
> The problem is my default range is actually 0,1 -- I changed it above
> for brevity's sake. So in the real world, it floods the screen to the
> point where it's unreadable.
> 
> I can suppress the whole thing with argparse.SUPPRESS, but then I'm left
> with no help message at all.  Is there a way to suppress the "(choose from
> 1,2,3,etc.)" part of the help message?  Or a cleaner/different way
> altogether to accomplish the same thing?

You can supply a custom function as the type to the add_argument() method:

http://docs.python.org/2/library/argparse.html#type

Here's a basic example:

$ cat argparse_intrange.py
import argparse

def check_range(arg):
try:
value = int(arg)
except ValueError as err:
   raise argparse.ArgumentTypeError(str(err))

if value < 0 or value > 1:
message = "Expected 0 <= value <= 1, got value = {}".format(value)
raise argparse.ArgumentTypeError(message)

return value

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("alpha", type=check_range, nargs="?")

print(parser.parse_args())

$ python argparse_intrange.py 
Namespace(alpha=None)
$ python argparse_intrange.py 100
Namespace(alpha=100)
$ python argparse_intrange.py -100
usage: argparse_intrange.py [-h] [alpha]
argparse_intrange.py: error: argument alpha: Expected 0 <= value <= 1, got 
value = -100
$ python argparse_intrange.py 10
usage: argparse_intrange.py [-h] [alpha]
argparse_intrange.py: error: argument alpha: Expected 0 <= value <= 1, got 
value = 10
$ python argparse_intrange.py x
usage: argparse_intrange.py [-h] [alpha]
argparse_intrange.py: error: argument alpha: invalid literal for int() with 
base 10: 'x'

If you want to use check_range() with arbitrary ranges you can parameterise it:

import functools

def check_range(arg, min, max):
# your code

check_range_alpha = functools.partial(check_range, min=0, max=1)
check_range_beta = functools.partial(check_range, min=-10, max=10)

parser = argparse.ArgumentParser()
parser.add_argument("alpha", type=check_range_alpha, nargs="?")
parser.add_argument("beta", type=check_range_beta, nargs="?")


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


Re: [Tutor] HELP- Regarding working with python

2013-01-28 Thread Steven D'Aprano

On 28/01/13 18:26, Gayathri S wrote:

Hi all..!
wanna know how to compile python script in python command line,
and is there any need for setting path for python like JAVA? whats the
difference between .py file and .pyc file?



Python is a byte-code compiled language, like Java many years ago. Normally to 
compile a python module, you just import it from within Python.

That is not very convenient for scripts, so from the shell (bash, command.com, 
cmd.exe, or similar) you can run

python -m compileall NAME NAME ...

to compile scripts.


You do not necessarily need to set the PYTHONPATH, but you can if you need to.

.py files are source code. .pyc are compiled byte code. .pyo are compiled byte 
code with optimization turned on. Don't get too excited, the standard Python 
optimization doesn't do very much.



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