Re: [Tutor] Guidance if possible

2013-04-12 Thread Wayne Werner

On Fri, 12 Apr 2013, Alan Gauld wrote:


On 11/04/13 23:33, Scurvy Scott wrote:


the other for something like this. I have no intention of doing
anything professional/shady/annoying with this code and want to write
it purely for my own amusement as well as to learn and obviously to
perhaps win something cool.


Seems reasonable to me.

Try looking at the http, urllib and cookie stuff in the stdlib.


Ick httplib/urllib - if you want to write code for getting information off 
a website, the 
requests module is the way to go:


http://docs.python-requests.org/en/latest/

It makes http requests pretty trivial.

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


[Tutor] creating dictionary from a list

2013-04-12 Thread Saad Bin Javed
Hi, I'm using a script to fetch my calendar events. I split the output 
at newline which produced a list 'lst'. I'm trying to clean it up and 
create a dictionary with date:event key value pairs. However this is 
throwing up a bunch of errors.


lst = ['', 'Thu Apr 04   Weigh In', '', 'Sat Apr 06 
Collect NIC', \
' Finish PTI Video', '', 'Wed Apr 10   Serum 
uric acid test', \
'', 'Sat Apr 13   1:00pm  Get flag from dhariwal', '', 'Sun Apr 14 
 Louis CK Oh My God', '', '']


dict = {}
same_day = ''
for x in lst:
c = x.split('   ')
if c[0] is '':
for q in c:
if q is not '':
dict.update({same_day: dict[same_day] + ', ' + 
q.strip()})
break
else:
if c[0].find('  '):
print c[0]
a = c[0].split('  ', 1)
same_day = a[0]
print a[0], a[1].lstrip()
dict.update({a[0] : a[1].lstrip()})
else:
same_day = c[0]
dict.update({c[0] : c[1]})

I want a dictionary thats like this {'Thu Apr 04': 'Weigh In', 'Sat Apr 
13': '1:00pm  Get flag from dhariwal'...}


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


Re: [Tutor] creating dictionary from a list

2013-04-12 Thread Steven D'Aprano

On 13/04/13 09:52, Saad Bin Javed wrote:

Hi, I'm using a script to fetch my calendar events. I split the output at 
newline which produced a list 'lst'. I'm trying to clean it up and create a 
dictionary with date:event key value pairs. However this is throwing up a bunch 
of errors.



Would you like us to guess what the errors are? Please copy and paste the 
complete traceback, starting with the line

Traceback (most recent line last)

all the way to the end.


In the meantime, my comments are below, interleaved with your code:




lst = ['', 'Thu Apr 04   Weigh In', '', 'Sat Apr 06 Collect NIC', \
' Finish PTI Video', '', 'Wed Apr 10   Serum uric 
acid test', \
'', 'Sat Apr 13   1:00pm  Get flag from dhariwal', '', 'Sun Apr 14  Louis 
CK Oh My God', '', '']



There's no need to use line-continuation backslash \ inside a list. Any form of 
bracket (round, square or curly) automatically continues the line.

Also, you might find it easier to process the list if you strip out empty 
items. There are two simple ways to do it:


lst = [x for x in list if x != '']
# or
lst = filter(None, lst)


Either way should work nicely on the data shown, and that might then simplify 
your processing below.




dict = {}
same_day = ''
for x in lst:
 c = x.split('   ')


It's best not to rely on an exact number of spaces for the separation, if you can. If you 
call split() with no argument, the string will be split on runs of whitespace. If you 
must depend on the difference between one space and two spaces, it's probably best to 
split on "  " (two spaces only), then delete any extra empty strings, as above.

Also, please try to use more meaningful variable names. In this case, I would suggest "words" instead of 
"c". ("c" for "character"?)




 if c[0] is '':


Do you read the other emails on this tutor list? You should, you will learn a lot from other 
people's questions. We've just had a few emails explaining that you should never use "is" 
to test equality, since "is" tests for object identity. So, repeating myself from the 
last few emails:

* always use "if x is None" or "if x is not None" when comparing against None;

* otherwise, never use "is", always use == or != when comparing against 
(nearly) everything else.


("Always" and "never" of course are understood to be general rules which may be 
ignored by experts when necessary.)

So replace the line above with "if c[0] == '':".




 for q in c:
 if q is not '':


Likewise, replace this with "if q != '':".




 dict.update({same_day: dict[same_day] + ', ' + q.strip()})
 break


I'm not sure why you use a "break" here. That will stop processing the entire list. Is 
that intended? Maybe you meant to use "continue"?




 else:
 if c[0].find('  '):


You can save an indentation level by using "elif" here.


if test1:
...
else:
if test2:
...
else:
...

becomes more nicely written as:

if test1:
...
elif test2:
...
else:
...




 print c[0]
 a = c[0].split('  ', 1)
 same_day = a[0]
 print a[0], a[1].lstrip()


You can simplify your processing here by assigning directly to a list of names. 
Instead of:

a = c[0].split('  ', 1)
process(a[0])
process(a[1])


you can give each item a name directly:


same_day, text = c[0].split('  ', 1)
process(same_day)
process(text)


which makes your code cleaner and easier to understand.




 dict.update({a[0] : a[1].lstrip()})
 else:
 same_day = c[0]
 dict.update({c[0] : c[1]})


If you're going to assign a value to a name, why don't you actually use the 
name?

same_day = c[0]
dict.update({same_day: c[1]})



Try making the changes I've suggested, and see if that makes the code cleaner 
and easier to understand. It might even fix some of the errors that you are 
getting.

Good luck!



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


Re: [Tutor] creating dictionary from a list

2013-04-12 Thread Saad Bin Javed

Thank you Steven! I followed your tips and came up with this:

lst = filter(None, lst)
lst = [item.split('  ', 1) for item in lst]
lst = [item for sublist in lst for item in sublist]
lst = filter(None, lst)

dict = {}
for item in lst:
if item.startswith(('Mon','Tue','Wed','Thu','Fri','Sat','Sun')):

		dict.update({lst[lst.index(item)].lstrip(): 
lst[lst.index(item)+1].lstrip()})

print dict

The program lines have halved :)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor