Re: [Tutor] Accessing Specific Dictionary items

2011-08-01 Thread bob gailer

On 8/1/2011 8:34 PM, Mike Nickey wrote:

[snip]


[{'city': 'Buena Park', 'region_name': 'CA', 'area_code': 714},
{'city': 'Wallingford', 'region_name': 'CT', 'area_code': 203},
{'city': 'Schenectady', 'region_name': 'NY', 'area_code': 518},
{'city': 'Athens', 'region_name': '35'}]


IMHO this is overkill.

Consider instead using City-Region as the key and storing the pther 
attributes in a tuple. There is no need to have separate dictionaries or 
lots of repetitive keys:


cities = {
'Buena Park CA': (414, 'Santa Clara, CA'),
'Wallingford'CT: (203),...}


--
Bob Gailer
919-636-4239
Chapel Hill NC

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


Re: [Tutor] Accessing Specific Dictionary items

2011-08-01 Thread Mike Nickey
Thank you all, I have found the assistance needed and the guidance
here was wonderful.

I needed to add a line in the middle that did a temporary storage for
me before trying to append this to the list. It might not be the most
elegant solution but it works.

On Mon, Aug 1, 2011 at 17:28,   wrote:
> Dictionaries are objects and you access their attributes through keys.
>
> So, let's say I had a dict: d = {'city':'plattsburgh'}
>
> I would thus access the attribute by doing this
>
> d['city']
>
> You can store that value to a variable
>
> Or you can append to a list directly.
>
> l = []
>
> for d in yourdict:
>     l.append(d['city'])
>
>
> Sent from my Verizon Wireless BlackBerry
>
> -Original Message-
> From: Mike Nickey 
> Sender: tutor-bounces+eire1130=gmail@python.org
> Date: Mon, 1 Aug 2011 15:26:32
> To: ian douglas
> Cc: 
> Subject: Re: [Tutor] Accessing Specific Dictionary items
>
> The input being used is through pygeoip.
> Using this I am pulling the data by IP and from what I am reading this
> populates as a dictionary.
>
> Here is some of the output that I can show currently
> [{'city': 'Buena Park', 'region_name': 'CA', 'area_code': 714},
> {'city': 'Wallingford', 'region_name': 'CT', 'area_code': 203},
> {'city': 'Schenectady', 'region_name': 'NY', 'area_code': 518},
> {'city': 'Athens', 'region_name': '35'}]
>
> I'd like to have an output similar to this:
> 'Buena Park', 'Wallingford', 'Schenectady','Athens' pulled by the
> "city" keys that are used in the returns. I think the easiest way to
> approach this would be simply to use the .append and populate a list
> but I don't know how to pull an item by key value from the dictionary
> returns.
>
> I hope this helps clear some confusion and thanks in advance.
>
>
> On Mon, Aug 1, 2011 at 15:14, ian douglas  wrote:
>> On 08/01/2011 03:05 PM, Mike Nickey wrote:
>>>
>>> Hi all,
>>>
>>> I'm trying to access and use specific items within a dictionary that
>>> is returned but am having issues in doing so.
>>> [{'city': 'Sunnyvale', 'region_name': 'CA', 'area_code': 408,
>>> 'metro_code': 'Santa Clara, CA', 'country_name': 'United States'}]
>>>
>>> How can I populate a list of many different returns so that I have a
>>> list that contains all the cities and in line with the item that is
>>> referenced.
>>>
>>> The first step is to populate the list unsorted as it needs to be in
>>> correlation to the item that it came from.
>>> Thanks for your help.
>>>
>>
>>
>> Could you give us examples of what you want the list to look like? It will
>> help us direct you to an answer.
>>
>
>
>
> --
> ~MEN
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



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


Re: [Tutor] Accessing Specific Dictionary items

2011-08-01 Thread eire1130
Dictionaries are objects and you access their attributes through keys.

So, let's say I had a dict: d = {'city':'plattsburgh'}

I would thus access the attribute by doing this 

d['city']

You can store that value to a variable

Or you can append to a list directly.

l = []

for d in yourdict:
 l.append(d['city'])


Sent from my Verizon Wireless BlackBerry

-Original Message-
From: Mike Nickey 
Sender: tutor-bounces+eire1130=gmail@python.org
Date: Mon, 1 Aug 2011 15:26:32 
To: ian douglas
Cc: 
Subject: Re: [Tutor] Accessing Specific Dictionary items

The input being used is through pygeoip.
Using this I am pulling the data by IP and from what I am reading this
populates as a dictionary.

Here is some of the output that I can show currently
[{'city': 'Buena Park', 'region_name': 'CA', 'area_code': 714},
{'city': 'Wallingford', 'region_name': 'CT', 'area_code': 203},
{'city': 'Schenectady', 'region_name': 'NY', 'area_code': 518},
{'city': 'Athens', 'region_name': '35'}]

I'd like to have an output similar to this:
'Buena Park', 'Wallingford', 'Schenectady','Athens' pulled by the
"city" keys that are used in the returns. I think the easiest way to
approach this would be simply to use the .append and populate a list
but I don't know how to pull an item by key value from the dictionary
returns.

I hope this helps clear some confusion and thanks in advance.


On Mon, Aug 1, 2011 at 15:14, ian douglas  wrote:
> On 08/01/2011 03:05 PM, Mike Nickey wrote:
>>
>> Hi all,
>>
>> I'm trying to access and use specific items within a dictionary that
>> is returned but am having issues in doing so.
>> [{'city': 'Sunnyvale', 'region_name': 'CA', 'area_code': 408,
>> 'metro_code': 'Santa Clara, CA', 'country_name': 'United States'}]
>>
>> How can I populate a list of many different returns so that I have a
>> list that contains all the cities and in line with the item that is
>> referenced.
>>
>> The first step is to populate the list unsorted as it needs to be in
>> correlation to the item that it came from.
>> Thanks for your help.
>>
>
>
> Could you give us examples of what you want the list to look like? It will
> help us direct you to an answer.
>



-- 
~MEN
___
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] Accessing Specific Dictionary items

2011-08-01 Thread Steven D'Aprano

Mike Nickey wrote:

The input being used is through pygeoip.
Using this I am pulling the data by IP and from what I am reading this
populates as a dictionary.

Here is some of the output that I can show currently
[{'city': 'Buena Park', 'region_name': 'CA', 'area_code': 714},
{'city': 'Wallingford', 'region_name': 'CT', 'area_code': 203},
{'city': 'Schenectady', 'region_name': 'NY', 'area_code': 518},
{'city': 'Athens', 'region_name': '35'}]

I'd like to have an output similar to this:
'Buena Park', 'Wallingford', 'Schenectady','Athens' pulled by the
"city" keys that are used in the returns. 



What do you mean, "the returns"?

If all you want is a list of cities, that's easy:

cities = [d['city'] for d in list_of_dicts]


or if you prefer a more verbose way:

cities = []
for d in list_of_dicts:
cities.append(d['city'])


To get the list sorted, just sort it afterwards:

cities.sort()



I think the easiest way to
approach this would be simply to use the .append and populate a list
but I don't know how to pull an item by key value from the dictionary
returns.


The same way you would pull an item by key from any other dict.




--
Steven

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


Re: [Tutor] Accessing Specific Dictionary items

2011-08-01 Thread Mike Nickey
The input being used is through pygeoip.
Using this I am pulling the data by IP and from what I am reading this
populates as a dictionary.

Here is some of the output that I can show currently
[{'city': 'Buena Park', 'region_name': 'CA', 'area_code': 714},
{'city': 'Wallingford', 'region_name': 'CT', 'area_code': 203},
{'city': 'Schenectady', 'region_name': 'NY', 'area_code': 518},
{'city': 'Athens', 'region_name': '35'}]

I'd like to have an output similar to this:
'Buena Park', 'Wallingford', 'Schenectady','Athens' pulled by the
"city" keys that are used in the returns. I think the easiest way to
approach this would be simply to use the .append and populate a list
but I don't know how to pull an item by key value from the dictionary
returns.

I hope this helps clear some confusion and thanks in advance.


On Mon, Aug 1, 2011 at 15:14, ian douglas  wrote:
> On 08/01/2011 03:05 PM, Mike Nickey wrote:
>>
>> Hi all,
>>
>> I'm trying to access and use specific items within a dictionary that
>> is returned but am having issues in doing so.
>> [{'city': 'Sunnyvale', 'region_name': 'CA', 'area_code': 408,
>> 'metro_code': 'Santa Clara, CA', 'country_name': 'United States'}]
>>
>> How can I populate a list of many different returns so that I have a
>> list that contains all the cities and in line with the item that is
>> referenced.
>>
>> The first step is to populate the list unsorted as it needs to be in
>> correlation to the item that it came from.
>> Thanks for your help.
>>
>
>
> Could you give us examples of what you want the list to look like? It will
> help us direct you to an answer.
>



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


Re: [Tutor] time zone conversion

2011-08-01 Thread spawgi
Quick question - Is any daylight saving taken into consideration for this?

On Tue, Aug 2, 2011 at 1:35 AM, ian douglas wrote:

> Hi all,
>
> Been trying to wrap my head around some datetime vs time stuff with regards
> to parsing a string as a date plus time with a timezone offset.
>
> This is the string I'm given:
>
> 2010-01-22T00:14:33.000Z
>
> And I can use time.strptime to parse out its individual elements, but then
> I need to adjust that time, in UTC/Zulu to my local time zone.
>
> Here's what I'm currently trying:
>
>old_launch_time = '2010-01-22T00:14:33.000Z'
>os.environ['TZ'] = 'UTC'
>time.tzset()
>launch_time = time.strptime(old_launch_time,
> '%Y-%m-%dT%H:%M:%S.000Z')
>os.environ['TZ'] = 'US/Pacific'
>time.tzset()
>print 'old time: ' + old_launch_time
>print 'new time: ' + time.strftime("%Y-%m-%d %H:%M:%S", launch_time)
>
> output:
>
> old time: 2010-01-22T00:14:33.000Z
> new time: 2010-01-22 00:14:33
>
> But the different tzset() calls are not adjusting based on the 7 or 8 hour
> difference.
>
> I know that all incoming dates/times are coming to me in UTC. I just can't
> seem to get a good handle on how to properly convert it to my own time zone.
>
> Thanks,
> Ian
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>



-- 
http://spawgi.wordpress.com
We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Accessing Specific Dictionary items

2011-08-01 Thread ian douglas

On 08/01/2011 03:05 PM, Mike Nickey wrote:

Hi all,

I'm trying to access and use specific items within a dictionary that
is returned but am having issues in doing so.
[{'city': 'Sunnyvale', 'region_name': 'CA', 'area_code': 408,
'metro_code': 'Santa Clara, CA', 'country_name': 'United States'}]

How can I populate a list of many different returns so that I have a
list that contains all the cities and in line with the item that is
referenced.

The first step is to populate the list unsorted as it needs to be in
correlation to the item that it came from.
Thanks for your help.




Could you give us examples of what you want the list to look like? It 
will help us direct you to an answer.

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


Re: [Tutor] Accessing Specific Dictionary items

2011-08-01 Thread eire1130
I'm not sure I'm following. Could you give an example of expected input and 
expected output?


Sent from my Verizon Wireless BlackBerry

-Original Message-
From: Mike Nickey 
Sender: tutor-bounces+eire1130=gmail@python.org
Date: Mon, 1 Aug 2011 15:05:30 
To: 
Subject: [Tutor] Accessing Specific Dictionary items

Hi all,

I'm trying to access and use specific items within a dictionary that
is returned but am having issues in doing so.
[{'city': 'Sunnyvale', 'region_name': 'CA', 'area_code': 408,
'metro_code': 'Santa Clara, CA', 'country_name': 'United States'}]

How can I populate a list of many different returns so that I have a
list that contains all the cities and in line with the item that is
referenced.

The first step is to populate the list unsorted as it needs to be in
correlation to the item that it came from.
Thanks for your help.

-- 
~MEN
___
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] Accessing Specific Dictionary items

2011-08-01 Thread Mike Nickey
Hi all,

I'm trying to access and use specific items within a dictionary that
is returned but am having issues in doing so.
[{'city': 'Sunnyvale', 'region_name': 'CA', 'area_code': 408,
'metro_code': 'Santa Clara, CA', 'country_name': 'United States'}]

How can I populate a list of many different returns so that I have a
list that contains all the cities and in line with the item that is
referenced.

The first step is to populate the list unsorted as it needs to be in
correlation to the item that it came from.
Thanks for your help.

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


Re: [Tutor] Question about sorting a list within a dictionary within a list

2011-08-01 Thread Peter Otten
Peter Otten wrote:

> Untested:
> 
> from operator import attrgetter, itemgetter
> from itertools import imap
> 
> firsts = imap(itemgetter(0), conn.get_all_instances())
> reservations = sorted(firsts, key=attrgetter("launch_time"))
> 
> This gives you objects rather than the objects' __dict__s.

Oops, I think I missed one level of indirection:

firsts = (r.instances[0] for r in conn.get_all_instances())
reservations = sorted(firsts, key=attrgetter("launch_time"))
 


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


Re: [Tutor] Question about sorting a list within a dictionary within a list

2011-08-01 Thread Peter Otten
ian douglas wrote:

> On 08/01/2011 01:03 PM, Peter Otten wrote:
>> ian douglas wrote:
>>
>>> I'm using the Bono library for talking to EC2, and I'm getting a list of
>>>
>>> I cannot help you with the django or boto part.
> 
> Well, I suppose that using django/bono wasn't really relevant to the
> question.
> 
> I appreciate the feedback though, I'll definitely keep it in mind for
> future projects. The sort() and mykey() stuff you proposed looked neat.
> I'll dig into that stuff more to see how it works when I finish this
> project. I appreciate the LEGAL_SORTKEYS bit too, it was on my to-do
> list as well.
> 
> 
> In the end, I ended up flattening things a little, instead of having a
> list of objects, and those objects holding a list of instances, and each
> of those instances being objects themselves:
> 
>  reservations_bulk = conn.get_all_instances()
>  reservations_unsorted = [] ;
>  for reservation in reservations_bulk:
>  instance = reservation.instances[0].__dict__
>  reservations_unsorted.append(instance)
>  reservations = sorted(reservations_unsorted,
> key=itemgetter('launch_time'))
> 
> I'm sure there's an even cleaner way of doing the for loop too?

Untested:

from operator import attrgetter, itemgetter
from itertools import imap

firsts = imap(itemgetter(0), conn.get_all_instances())
reservations = sorted(firsts, key=attrgetter("launch_time"))

This gives you objects rather than the objects' __dict__s.

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


Re: [Tutor] Question about sorting a list within a dictionary within a list

2011-08-01 Thread ian douglas

On 08/01/2011 01:03 PM, Peter Otten wrote:

ian douglas wrote:


I'm using the Bono library for talking to EC2, and I'm getting a list of

I cannot help you with the django or boto part.


Well, I suppose that using django/bono wasn't really relevant to the 
question.


I appreciate the feedback though, I'll definitely keep it in mind for 
future projects. The sort() and mykey() stuff you proposed looked neat. 
I'll dig into that stuff more to see how it works when I finish this 
project. I appreciate the LEGAL_SORTKEYS bit too, it was on my to-do 
list as well.



In the end, I ended up flattening things a little, instead of having a 
list of objects, and those objects holding a list of instances, and each 
of those instances being objects themselves:


reservations_bulk = conn.get_all_instances()
reservations_unsorted = [] ;
for reservation in reservations_bulk:
instance = reservation.instances[0].__dict__
reservations_unsorted.append(instance)
reservations = sorted(reservations_unsorted, 
key=itemgetter('launch_time'))


I'm sure there's an even cleaner way of doing the for loop too?

I hadn't seen the __dict__ property before for objects, and that allowed 
me to just build a simple list of dictionaries, which I could then run 
through the itemgetter() solution I'd found on StackOverflow.


Ian

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


[Tutor] time zone conversion

2011-08-01 Thread ian douglas

Hi all,

Been trying to wrap my head around some datetime vs time stuff with 
regards to parsing a string as a date plus time with a timezone offset.


This is the string I'm given:

2010-01-22T00:14:33.000Z

And I can use time.strptime to parse out its individual elements, but 
then I need to adjust that time, in UTC/Zulu to my local time zone.


Here's what I'm currently trying:

old_launch_time = '2010-01-22T00:14:33.000Z'
os.environ['TZ'] = 'UTC'
time.tzset()
launch_time = time.strptime(old_launch_time, 
'%Y-%m-%dT%H:%M:%S.000Z')

os.environ['TZ'] = 'US/Pacific'
time.tzset()
print 'old time: ' + old_launch_time
print 'new time: ' + time.strftime("%Y-%m-%d %H:%M:%S", 
launch_time)


output:

old time: 2010-01-22T00:14:33.000Z
new time: 2010-01-22 00:14:33

But the different tzset() calls are not adjusting based on the 7 or 8 
hour difference.


I know that all incoming dates/times are coming to me in UTC. I just 
can't seem to get a good handle on how to properly convert it to my own 
time zone.


Thanks,
Ian

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


Re: [Tutor] Question about sorting a list within a dictionary within a list

2011-08-01 Thread Peter Otten
ian douglas wrote:

> I'm using the Bono library for talking to EC2, and I'm getting a list of
> EC2 instances back in a list called "reservations". Each element in the
> list is a dictionary of information. One of those dictionary elements is
> a list called 'instances', and I only ever care about the first entry.
> That instances[0] value is a dictionary, which holds a key I want to use
> to sort the base "reservations" list.
> 
> I found this helpful bit for sorting a list of dictionaries by a key
> within the dictionaries:
> http://stackoverflow.com/questions/72899/in-python-how-do-i-sort-a-list-
of-dictionaries-by-values-of-the-dictionary
> 
> ... this works great if you just have a list of dictionaries. I'm not
> sure how to go deeper within that to sort my data without iterating
> through the entire 'reservations' list and building an entirely new
> list. Maybe it would be easier, but I'm just curious if it's possible
> without messing with my main 'reservations' list.
> 
> 
> My current code looks like this: (it's a views.py file)
> 
> from django.http import HttpResponse
> import boto.ec2
> from operator import itemgetter
> 
> def index(request):
>  conn = boto.connect_ec2()
>  reservations = conn.get_all_instances()
> 
>  # this is where I'm trying to sort everything
>  res_sorted = sorted(reservations, key=itemgetter('launch_time'))
> 
>  output = ''
>  output += ''
>  output += 'State'
>  output += 'Launched'
>  output += 'Public Hostname'
>  output += 'Public IP'
>  output += 'Private Hostname'
>  output += 'Private IP'
>  output += ''
>  for reservation in res_sorted:
>  instance = reservation.instances[0]
>  output += ''
>  output += '' + instance.state + ''
>  output += '' + instance.launch_time + ''
>  output += '' + instance.public_dns_name + ''
>  output += '' + instance.ip_address + ''
>  output += '' + instance.private_dns_name + ''
>  output += '' + instance.private_ip_address + ''
> 
> 
> Ideally, I'd like to make each table column 'sortable' so the user can
> click on state/launched/etc (I may add more columns in the future), but
> I'm not sure how to search deeper within the 'reservations' list for the
> sorted() call to get at the 'launch_time' element within the instaces[0]
> dictionary.
> 
> Also, I'm sure there are much better ways to do the display logic, but
> I'll tackle that another time.

I cannot help you with the django or boto part. As to sorting lists: to do 
it inplace invoke the sort() method:

reservations.sort(key=mykey)

You can think of itemgetter("launch_time") as a function that creates the 
the following function on the fly:

def mykey(item):
return item["launch_time"]

Judging from the code you provide you need

def mykey(item):
return item.instances[0].launch_time

instead. If you want to be flexible about the actual attribute you can 
change that to

attrname = ...
def mykey(item):
return getattr(item.instances[0], attrname)

Djange probably offers a way to provide the desired attribute name via the 
request object. Your index() function would then become

LEGAL_SORTKEYS = set(["launch_time", "ip_address"])

def index(request):
conn = boto.connect_ec2()
reservations = conn.get_all_instances()
attrname = ... # your code
if attrname in LEGAL_SORTKEYS:
def mykey(item):
return getattr(item.instances[0], attrname)
reservations.sort(key=mykey)
else:
... # provide an error message or just don't sort

output = ''
output += ''
...


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


Re: [Tutor] get last 7 days string in a log file

2011-08-01 Thread Bjørn Roar Eriksen
Yes, that is what I want to do. 

Kind Regard
Bjørn-Roar

Den 1. aug. 2011 kl. 20:24 skrev spa...@gmail.com:

> Hi,
> 
> Let me understand the problem
> - Do you want to count the number of times the application was restarted 
> between today and 7 days back?
> => If yes, then you will need to do some string manipulation to get the 
> required dates. Then you will need to apply both the conditions to the line 
> i.e. if the date is the required date and if the string contains what you 
> want.
> i.e. if "2009-03-6" in line and :
> ... count += 1
> You can apply this logic to each date and create a complex statement using OR 
> to get the count.
> OR you can use the datetime object with proper formatting and check the 
> existence of the condition.
> 
> Do I understand your problem correctly? 
> 
> Regards
> Sumod
> 
> 
> 2011/8/1 Bjørn-Roar Eriksen 
> Hi
> 
> I'm new to Python and I'm trying to write a script that's count restart of an 
> application. The log file contains:
> 2009-03-06 18:20:26,423  User operation - start nanny
> 2009-03-06 18:20:26,423  User operation - start all services
> And 2009-03-06 18:20:26,423  User operation - start all services tells me 
> that the application has been restarted.
> 
> My script do count it, but I cant figure out how to code so it's only get 
> from to day and 7 day's back.
> #Counts restart of an application
> count = 0
> with open("c:/test/bre.log")as f:
> for line in f:
> if "User operation - start all services" in line:
> count += 1
> #print line.strip()
> print 'Server restart: %s' % count
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
> 
> 
> 
> 
> -- 
> http://spawgi.wordpress.com
> We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] get last 7 days string in a log file

2011-08-01 Thread spawgi
Hi,

Let me understand the problem
- Do you want to count the number of times the application was restarted
between today and 7 days back?
=> If yes, then you will need to do some string manipulation to get the
required dates. Then you will need to apply both the conditions to the line
i.e. if the date is the required date and if the string contains what you
want.
i.e. if "2009-03-6" in line and :
... count += 1
You can apply this logic to each date and create a complex statement using
OR to get the count.
OR you can use the datetime object with proper formatting and check the
existence of the condition.

Do I understand your problem correctly?

Regards
Sumod


2011/8/1 Bjørn-Roar Eriksen 

> Hi
>
> I'm new to Python and I'm trying to write a script that's count restart of
> an application. The log file contains:
> 2009-03-06 18:20:26,423  User operation - start nanny
> 2009-03-06 18:20:26,423  User operation - start all services
> And 2009-03-06 18:20:26,423  User operation - start all services tells me
> that the application has been restarted.
>
> My script do count it, but I cant figure out how to code so it's only get
> from to day and 7 day's back.
> #Counts restart of an application
> count = 0
> with open("c:/test/bre.log")as f:
> for line in f:
> if "User operation - start all services" in line:
> count += 1
> #print line.strip()
> print 'Server restart: %s' % count
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
http://spawgi.wordpress.com
We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] get last 7 days string in a log file

2011-08-01 Thread Bjørn-Roar Eriksen
Hi

I'm new to Python and I'm trying to write a script that's count restart of
an application. The log file contains:
2009-03-06 18:20:26,423  User operation - start nanny
2009-03-06 18:20:26,423  User operation - start all services
And 2009-03-06 18:20:26,423  User operation - start all services tells me
that the application has been restarted.

My script do count it, but I cant figure out how to code so it's only get
from to day and 7 day's back.
#Counts restart of an application
count = 0
with open("c:/test/bre.log")as f:
for line in f:
if "User operation - start all services" in line:
count += 1
#print line.strip()
print 'Server restart: %s' % count
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Question about sorting a list within a dictionary within a list

2011-08-01 Thread ian douglas
I'm using the Bono library for talking to EC2, and I'm getting a list of 
EC2 instances back in a list called "reservations". Each element in the 
list is a dictionary of information. One of those dictionary elements is 
a list called 'instances', and I only ever care about the first entry. 
That instances[0] value is a dictionary, which holds a key I want to use 
to sort the base "reservations" list.


I found this helpful bit for sorting a list of dictionaries by a key 
within the dictionaries:

http://stackoverflow.com/questions/72899/in-python-how-do-i-sort-a-list-of-dictionaries-by-values-of-the-dictionary

... this works great if you just have a list of dictionaries. I'm not 
sure how to go deeper within that to sort my data without iterating 
through the entire 'reservations' list and building an entirely new 
list. Maybe it would be easier, but I'm just curious if it's possible 
without messing with my main 'reservations' list.



My current code looks like this: (it's a views.py file)

from django.http import HttpResponse
import boto.ec2
from operator import itemgetter

def index(request):
conn = boto.connect_ec2()
reservations = conn.get_all_instances()

# this is where I'm trying to sort everything
res_sorted = sorted(reservations, key=itemgetter('launch_time'))

output = ''
output += ''
output += 'State'
output += 'Launched'
output += 'Public Hostname'
output += 'Public IP'
output += 'Private Hostname'
output += 'Private IP'
output += ''
for reservation in res_sorted:
instance = reservation.instances[0]
output += ''
output += '' + instance.state + ''
output += '' + instance.launch_time + ''
output += '' + instance.public_dns_name + ''
output += '' + instance.ip_address + ''
output += '' + instance.private_dns_name + ''
output += '' + instance.private_ip_address + ''


Ideally, I'd like to make each table column 'sortable' so the user can 
click on state/launched/etc (I may add more columns in the future), but 
I'm not sure how to search deeper within the 'reservations' list for the 
sorted() call to get at the 'launch_time' element within the instaces[0] 
dictionary.


Also, I'm sure there are much better ways to do the display logic, but 
I'll tackle that another time.


Thanks for any pointers,
Ian

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