Re: [Tutor] table to dictionary and then analysis

2012-05-15 Thread Alan Gauld

On 15/05/12 07:12, questions anon wrote:

Thanks Bob,
sql does appear to be very simple although I cannot get the queries to
work. Can you suggest a site that has examples for what I am trying to
do. I have done some googling but it has not been successful so far.


You can try my tutorial topic on databases.

It covers the basics of using SQLlite to query data in a fairly
concise format.

Its only available in the Version 2 tutor so far...

http://www.alan-g.me.uk/tutor/tutdbms.htm



--
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] table to dictionary and then analysis

2012-05-15 Thread Russel Winder
On Mon, 2012-05-14 at 23:38 -0400, bob gailer wrote:
[...]
> I would set up a SQLite database with a table of 4 numeric columns: 
> year, month, rainfall, firearea
> Use SQL to select the desired date range and do the max and avg 
> calculations:
> select year, avg(firearea), max(rainfall) from table where year = 1973 
> and month between 6 and 8)
> 
> you can use dictionaries but that will be harder. Here a start 
> (untested). Assumes data are correct.

Clearly if the data is to be stored for a long time and have various
(currently unknown) queries passed over it then year a database it the
right thing -- though I would probably choose a non-SQL database.

If the issues is to just do quick calculations over the data in the file
format then nothing wrong with using dictionaries or parallel arrays à
la:

with open ( 'yearmonthrainfire.txt' ) as infile :
climateindexname = infile.readline ( ).split ( )
data = [ line.split ( ) for line in infile.readlines ( ) ]

years = sorted ( { item[0] for item in data } )
months = [ 'Jan' , 'Feb' , 'Mar' , 'Apr' , 'May' , 'Jun' , 'Jul' , 
'Aug' , 'Sep' , 'Oct' , 'Nov' , 'Dec' ]

dataByYear = { year : [ ( float ( item[2] ) , float ( item[3] ) ) for 
item in data if item[0] == year ] for year in years } 
dataByMonth = { month : [ ( float ( item[2] ) , float ( item[3] ) ) for 
item in data if item[1] == month ] for month in months }

averagesByYear = { year : ( sum ( dataByYear[year][0] ) / len ( 
dataByYear[year][0] ) , sum ( dataByYear[year][1] ) / len ( dataByYear[year][1] 
) ) for year in years }
averagesByMonth = { month : ( sum ( dataByMonth[month][0] ) / len ( 
dataByMonth[month][0] ) , sum ( dataByMonth[month][1] ) / len ( 
dataByMonth[month][1] ) ) for month in months }

for year in years :
print ( year , averagesByYear[year][0] , averagesByYear[year][1] )

for month in months :
print ( month , averagesByMonth[month][0] , 
averagesByMonth[month][1] )

The cost of the repetition in the code here is probably minimal compared
to the disc access costs. On the other hand this is a small data set so
time is probably not a big issue.

-- 
Russel.
=
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder


signature.asc
Description: This is a digitally signed message part
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] hello~

2012-05-15 Thread Prasad, Ramit
>as you can see i only use some of the command. it doesnt produce an error 
>message tho.. just repeats "return Error("%s I couldn't find %s 
>anywhere", user.name.title(), name.title())"

Your problem might be is indenting of the else. It is indenting 
to be a for...else loop. Which means that if nothing breaks out of the
loop it will always do what is in the else (which in this case returns
Error[...]) after the loop has finished running through the rooms.

        for room in mgr.rooms:
                if data[1] == "join":
                        mgr.sendObject(target, Html("%s, %s wants to tell you %s", name.title, 
user.name.title$
        else:
                        return Error("%s I couldn't find %s anywhere", 
user.name.title(), name.title())


Why are you looping through the rooms and checking if data[1] == "join"?
You do not even use the room object in the loop, and target is not defined
in the local scope, although I suppose you meant to use room instead.
This loop just "smells" to me. 

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] extracting lines between patterns.

2012-05-15 Thread Prasad, Ramit
> Friends,
>Could someone please give some hint on how to extract lines between two 
>patterns in a file. I use the re module to compile my patterns but not able to 
>figure out how i can use these patterns to extract the lines lying in between.

Not sure exactly what you want but something like this could work. Note, this
is not memory efficient. 

line = file.read()
index1 = line.find( PATTERN1 )
index2 = line.find( PATTERN2 )
in_between = line[index1 + len( index1 ):index2]
number_of_lines = len( in_between.split() )  # -1 ?
# might need to adjust (-1) to avoid the line PATTERN1 is on

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] table to dictionary and then analysis

2012-05-15 Thread Alan Gauld

On 15/05/12 10:36, Russel Winder wrote:

...queries passed over it then year a database it the
right thing -- though I would probably choose a non-SQL database.


As a matter of interest why?
And what kind of alternative would you use?

It seems to me that SQL is ideally suited(*) to this type of role. I'm 
curious what the alternatives might be and why they would be preferred?


(*)Because: Flexible query language, wide set of tools including GUI 
query builders, reporting tools etc. Plus easy integration with 
programming environments, scaleability (less an issue here), 
security(usually) etc.


--
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