Re: [Tutor] different behaviour in Idle shell vs Mac terminal

2012-01-09 Thread Peter Otten
Alan Gauld wrote:

 On 08/01/12 23:34, Adam Gold wrote:

 I have short piece of code I'm using to print a string to
   the terminal one letter at a time.  It works fine when
 I invoke the script from within Idle; each letter appears
 afterthe preceding one according to the designated time
   interval.
   However if I run it in the Mac terminal
   ('python3 ./script.py'),
   there's a pause and then the whole string prints in one go.
 
 Thats because you are writing to stdout rather than using print
 The output is buffered and the terminal prints the output after the
 bufrfer is flushed, which happens at the end of the program
 (probably when the file object is auto closed). if you use print
 that shouldn't happen.
 
 The alternative is to explicitly flush() the file after each write.
 
 import sys
 import time

 text = this text is printing one letter at a time...
 for char in text:
  sys.stdout.write(char)
 
 either use
 print char,# comma suppresses \n

The newline will be suppressed, but the next print statement will inject a 
space before dumping its arguments. Also, you still need to flush(). That 
makes a working print-based solution a bit esoteric:

for c in text:
print c,
sys.stdout.softspace = False
sys.stdout.flush()
time.sleep(.3)


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


[Tutor] re module help

2012-01-09 Thread Ganesh Kumar
Hi Gurus,

I have created regular expression with os modules, I have created file
sdptool to match the regular expression pattern, will print the result.
I want without creating file how to get required output, I tried but i
didn't get output correctly, over stream.

#! /usr/bin/python
import os,re

def scan():

cmd = sdptool -i hci0 search OPUSH  sdptool
fp = os.popen(cmd)

results = []
l = open(sdptool).read()


pattern = r^Searching for OPUSH on (\w\w(:\w\w)+).*?Channel: (\d+)
r = re.compile(pattern, flags=re.MULTILINE|re.DOTALL)
while True:
for match in r.finditer(l):
g  = match.groups()

results.append((g[0],'phone',g[2]))
return results

## output [('00:15:83:3D:0A:57', 'phone', '1')]


http://dpaste.com/684335/
please guide me. with out file creating, to archive required output.


Did I learn something today? If not, I wasted it.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Moving from snippits to large projects?

2012-01-09 Thread Steven D'Aprano

Leam Hall wrote:
I'm taking the O'Reilly Python 2 course on-line, and enjoying it. Well, 
when Eclipse works, anyway. I'm still getting the hang of that.


While my coding over the years has been small snippits in shell, PHP, 
and a little C, python, and perl, I've never made the transition from 
dozens of lines to hundreds or thousands. I'd like to start working on 
that transition but the projects I know about are much larger than my 
brain can handle and there are a lot of other corollary tools and 
practices to learn.


How does one go from small to medium, to large, as a coder? Large 
projects, that is.



Pick something useful which you would like. Write the program. That might be a 
few dozen lines.


Stage 2: pretend that other people will use it, people who don't know how it 
is supposed to work, so put in plenty of error checking. Before you know it, 
you've got 100-200 lines of code.


Then add documentation. There's another 200-400 lines. (Except for the most 
trivial programs, in my experience documentation will be about double the size 
of the code you are documenting, at least the way I write it, with plenty of 
detail and examples.)


Now write extensive tests for it. Ideally, you should test every function and 
class in the program. You should test that it works as expected with good 
input, and fails as expected with bad input. That's another 400-800 lines. (In 
my experience, proper testing will be at least four times as big as the code 
you are testing.)


And lo and behold, you now have a medium-sized project. A *large* project will 
be tens of thousands of lines, not hundreds. A *major* project will be 
millions of lines.


Here's an example: I wrote a script to download images from the Quickmeme 
website. http://www.quickmeme.com/Web-Developer-Walrus/


Even without moving onto stage 2, I have 120 lines of code and documentation.

If I were to continue to work on it, I'd start adapting the script to be more 
generic. Turn it a general purpose download anything from anywhere library 
(a bit like wget or curl, for those who know those tools). Off the top of my 
head, I expect that would require 500-1000 lines of Python code and 
documentation. Plus tests.




--
Steven

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


Re: [Tutor] Moving from snippits to large projects?

2012-01-09 Thread Leam Hall

Leam Hall wrote:

 Steve and Hugo Responded

 To which Leam Replies:

Thanks! The O'Reilly class has twelve lessons, the first two are on unit 
testing. The rest of them will enforce tests be written for their 
projects.  :)


I'll look at Git and Sourceforge in the next couple days. In theory I'd 
like to get to kernel programming so Git would be useful. However, it 
doesn't have the Software Lifecycle tools Sourceforge has.


One of the problems with Python is that the neat things I'd like to 
write and use are already written! Well, there are things I need to 
learn, like Sphinx, Twisted, and maybe CherryPy. As well as PDB and 
better unittests.


So much to learn! I'm happy...

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


Re: [Tutor] different behaviour in Idle shell vs Mac terminal

2012-01-09 Thread Adam Gold

 Date: Sun, 8 Jan 2012 23:34:15 +
 From: Adam Gold 
 To: 
 Subject: [Tutor] different behaviour in Idle shell vs Mac terminal
 Message-ID: 
 Content-Type: text/plain; charset=iso-8859-1


 I have short piece of code I'm using to print a string to the terminal one 
 letter at a time.? It works fine when I invoke the script from within Idle; 
 each letter appears after the preceding one according to the designated time 
 interval.? However if I run it in the Mac terminal ('python3 ./script.py'), 
 there's a pause and then the whole string prints in one go.? Here's the 
 relevant code:

 import sys
 import time

 text = this text is printing one letter at a time...
 for char in text:
 ??? sys.stdout.write(char)
 ??? time.sleep(0.03)

 I'm thinking this may be a tty issue (is stdout going to the right terminal?) 
 but I'm still finding my way and would therefore appreciate any guidance.? Of 
 course if there's a better way of printing out one letter at a time, I'm also 
 interested to know that.? Thanks.

 P.S. if it's relevant, this is part of a simple financial maths program and 
 it's used to display the results after certain inputs have been gathered.


 --

 Message: 3
 Date: Mon, 09 Jan 2012 10:56:29 +1100
 From: Steven D'Aprano 
 To: tutor@python.org
 Subject: Re: [Tutor] different behaviour in Idle shell vs Mac terminal
 Message-ID: 4f0a2d2d.9000...@pearwood.info
 Content-Type: text/plain; charset=ISO-8859-1; format=flowed

 Adam Gold wrote:
  I have short piece of code I'm using to print a string to the terminal one 
  letter at a time. It works fine when I invoke the script from within Idle; 
  each letter appears after the preceding one according to the designated 
  time interval. However if I run it in the Mac terminal ('python3 
  ./script.py'), there's a pause and then the whole string prints in one go. 
  Here's the relevant code:
 
  import sys
  import time
 
  text = this text is printing one letter at a time...
  for char in text:
  sys.stdout.write(char)
  time.sleep(0.03)
 
  I'm thinking this may be a tty issue (is stdout going to the right 
  terminal?)

 It's a buffering issue.

 [...]
  P.S. if it's relevant, this is part of a simple financial maths program and 
  it's used to display the results after certain inputs have been gathered.

 To annoy your users? I'm not sure why you think it's a good idea to pretend
 that the computer has to type the letters one at a time. This isn't some
 stupid imaginary program in a Hollywood movie, I assume it is meant to
 actually be useful and usable, and trust me on this, waiting while the program
 pretends to type gets old *really* fast.

 (What are you trying to emulate? A stock ticker or something? Do those things
 still even exist? I haven't seen one since the last time I watched the Addams
 Family TV series. The old one, in black  white.)

 But if you must, after writing each character, call sys.stdout.flush() to
 flush the buffer.



 --
 Steven

Thanks Steven that worked.  In terms of why I'm using this: I shouldn't 
overstate what I'm doing when I say financial maths.  One of the elements is a 
mortgage calculator for my mother who's, shall we say, not a power user.  
After taking the basic inputs, it prints out a few stats (monthly payment 
etc.).  Without some literal brake in how the info gets written on the 
screen, it all appears in one go and having tested it on said power user, it 
was too much at once.  Hence I want to slow things down.  I appreciate, I'm not 
pushing the boundaries here but one step at a time ('excuse the pun!).  Anyway, 
again, thanks for your help.
  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] exception about ctrl+c

2012-01-09 Thread daedae11
I want to catch the ctrl+c exception. My program is as following. But when I 
run my script and press ctrl+c, the  program output nothing. I don't know 
where did I go wrong. Please help me. Thank you!

def safe_input(prompting):
try:
return raw_input(prompting);
except KeyboardInterrupt, error:
print error;
return None;

def main():
a = safe_input(input any thing!\n);
print a;


if __name__ == '__main__':
main();




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


[Tutor] Sorting Nested Lists

2012-01-09 Thread Sarma Tangirala
Hi list,

I was banging my head about a pythonic way of doing the following,

Given a nested list, how do I sort the uppermost list based on one key and
when a special condition occurs a sort on another key should be performed?

For example, [[1,2], [2, 2], [3, 2], [4, 0]] would be sorted, in my example
as, [[4, 0], [3, 2], [2, 2], [1, 2]]. That is, sort on the second value and
in case they are equal, reverse sort on the first value.

I tried doing this using sorted and using a custom cmp function but not
sure about how to define the cmp function.

-- 
Sarma Tangirala,
Class of 2012,
Department of Information Science and Technology,
College of Engineering Guindy - Anna University
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] re module help

2012-01-09 Thread bodsda
You could use read directly on the popen call to negate having to write to a 
file

output = os.popen(“sdptool -i hci0 search OPUSH“).read()

Bodsda
Sent from my BlackBerry® wireless device

-Original Message-
From: Ganesh Kumar bugcy...@gmail.com
Sender: tutor-bounces+bodsda=googlemail@python.org
Date: Mon, 9 Jan 2012 14:47:46 
To: tutor@python.org
Subject: [Tutor] re module help

___
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] exception about ctrl+c

2012-01-09 Thread Joel Goldstick
On Mon, Jan 9, 2012 at 7:24 AM, daedae11 daeda...@126.com wrote:
 I want to catch the ctrl+c exception. My program is as following. But when
 I run my script and press ctrl+c, the  program output nothing. I don't
 know where did I go wrong. Please help me. Thank you!

 def safe_input(prompting):
 try:
 return raw_input(prompting);
 except KeyboardInterrupt, error:
 print error;
 return None;

 def main():
 a = safe_input(input any thing!\n);
 print a;


 if __name__ == '__main__':
 main();

 
 daedae11

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

  python ctl-c.py
  input any thing!
  fdsaj
  fdsaj
  python ctl-c.py
  input any thing!
  ^C
  None

I just got this from your code.  Seems to work on python 2.65, linux

Are you running it from a command line like:  python ctl-c.py
or are you running in a python shell?  If you are in a shell it might
be consuming the ctl-c before your program can


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


Re: [Tutor] exception about ctrl+c

2012-01-09 Thread Christian Witts

On 2012/01/09 02:24 PM, daedae11 wrote:
I want to catch the ctrl+c exception. My program is as following. 
But when I run my script and press ctrl+c, the  program output 
nothing. I don't know where did I go wrong. Please help me. Thank you!

def safe_input(prompting):
try:
return raw_input(prompting);
except KeyboardInterrupt, error:
print error;
return None;
def main():
a = safe_input(input any thing!\n);
print a;
if __name__ == '__main__':
main();

daedae11


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

def safe_input(prompting):
try:
return raw_input(prompting)
except KeyboardInterrupt:
print 'KeyboardInterrupt Issued'
return None

That will work as intended, if you had your original `except 
KeyboardInterrupt, error:` and did a `print repr(error)` afterwards you 
will see it does not contain an error message as you perhaps wanted.


Also, Python does not require semi-colons to denote the end-of-line. It 
can be used if you want to have multiple statements on a single line though.


--

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


Re: [Tutor] Sorting Nested Lists

2012-01-09 Thread Steven D'Aprano

Sarma Tangirala wrote:

Hi list,

I was banging my head about a pythonic way of doing the following,

Given a nested list, how do I sort the uppermost list based on one key and
when a special condition occurs a sort on another key should be performed?
For example, [[1,2], [2, 2], [3, 2], [4, 0]] would be sorted, in my example
as, [[4, 0], [3, 2], [2, 2], [1, 2]]. That is, sort on the second value and
in case they are equal, reverse sort on the first value.


That is not exactly a good example. There are at least two other ways to get 
the result you show, both much more obvious:


py L = [[1,2], [2, 2], [3, 2], [4, 0]]
py list(reversed(L))
[[4, 0], [3, 2], [2, 2], [1, 2]]
py sorted(L, reverse=True)
[[4, 0], [3, 2], [2, 2], [1, 2]]


If I ignore your example, and just use the description:

sort on the second value, and in case they are equal, reverse sort on the 
first value


the way to do this is with a double sort. Note that this works because 
Python's sort is stable: in the event of ties, the first item remains first. 
In earlier versions of Python, this was not always the case.


So, given this list:

L = [[1,2], [4,0], [3,2], [2,2], [5,1], [1,1]]

first sort in reverse by the first item, then by the second:


py L.sort(key=lambda sublist: sublist[0], reverse=True)
py L.sort(key=lambda sublist: sublist[1])
py print L
[[4, 0], [5, 1], [1, 1], [3, 2], [2, 2], [1, 2]]



Note that using a key function is MUCH more efficient than a cmp function. 
Comparison functions end up doing much more work, and hence are very much 
slower, than a key function.


Also note that in recent versions of Python, you can do without the lambda 
function and use the special itemgetter function:



py from operator import itemgetter
py L = [[1,2], [4,0], [3,2], [2,2], [5,1], [1,1]]
py L.sort(key=itemgetter(0), reverse=True)
py L.sort(key=itemgetter(1))
py print L
[[4, 0], [5, 1], [1, 1], [3, 2], [2, 2], [1, 2]]


Last but not least, I will show how to do it using a custom cmp function. But 
I recommend that you don't use this!


def my_cmp(list1, list2):
x = cmp(list1[1], list2[1])
if x == 0:  # a tie
x = cmp(list2[0], list1[0])  # swap the order for reverse sort
# or if you prefer, x = -cmp(list1[0], list2[0])
return x

sorted(L, my_cmp)




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


Re: [Tutor] different behaviour in Idle shell vs Mac terminal

2012-01-09 Thread Steven D'Aprano

Adam Gold wrote:


Thanks Steven that worked.  In terms of why I'm using this: I shouldn't
overstate what I'm doing when I say financial maths.  One of the elements
is a mortgage calculator for my mother who's, shall we say, not a power
user.  After taking the basic inputs, it prints out a few stats (monthly
payment etc.).  Without some literal brake in how the info gets written
on the screen, it all appears in one go and having tested it on said power
user, it was too much at once.  Hence I want to slow things down.


My recommendation is to display a full line of text, then pause before the 
next line of text. Not one character at a time.



Regards,


--
Steven

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


Re: [Tutor] Sorting Nested Lists

2012-01-09 Thread Peter Otten
Sarma Tangirala wrote:

 I was banging my head about a pythonic way of doing the following,
 
 Given a nested list, how do I sort the uppermost list based on one key and
 when a special condition occurs a sort on another key should be performed?
 
 For example, [[1,2], [2, 2], [3, 2], [4, 0]] would be sorted, in my
 example as, [[4, 0], [3, 2], [2, 2], [1, 2]]. That is, sort on the second
 value and in case they are equal, reverse sort on the first value.
 
 I tried doing this using sorted and using a custom cmp function but not
 sure about how to define the cmp function.

Python's list.sort() is stable, it doesn't change the order of items that 
compare equal. Therefore you can achieve your goal by sorting twice:

 items = [[1,2], [2, 2], [3, 2], [0, 0]]
 items.sort(key=itemgetter(0), reverse=True)
 items
[[3, 2], [2, 2], [1, 2], [0, 0]]
 items.sort(key=itemgetter(1))
 items
[[0, 0], [3, 2], [2, 2], [1, 2]]

(I changed your last item to [0, 0] to allow me to demonstrate that two 
sorts are indeed necessary)

Using a more complex key is also possible,

 sorted([[1,2], [2, 2], [3, 2], [0, 0]], key=lambda item: (item[1], -
item[0]))
[[0, 0], [3, 2], [2, 2], [1, 2]]

but I think that is less elegant.

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


Re: [Tutor] Sorting Nested Lists

2012-01-09 Thread Sarma Tangirala
On 9 January 2012 18:26, Steven D'Aprano st...@pearwood.info wrote:

 Sarma Tangirala wrote:

 Hi list,

 I was banging my head about a pythonic way of doing the following,

 Given a nested list, how do I sort the uppermost list based on one key and
 when a special condition occurs a sort on another key should be performed?
 For example, [[1,2], [2, 2], [3, 2], [4, 0]] would be sorted, in my
 example
 as, [[4, 0], [3, 2], [2, 2], [1, 2]]. That is, sort on the second value
 and
 in case they are equal, reverse sort on the first value.


 That is not exactly a good example. There are at least two other ways to
 get the result you show, both much more obvious:

 py L = [[1,2], [2, 2], [3, 2], [4, 0]]
 py list(reversed(L))

 [[4, 0], [3, 2], [2, 2], [1, 2]]
 py sorted(L, reverse=True)

 [[4, 0], [3, 2], [2, 2], [1, 2]]


 If I ignore your example, and just use the description:

 sort on the second value, and in case they are equal, reverse sort on the
 first value

 the way to do this is with a double sort. Note that this works because
 Python's sort is stable: in the event of ties, the first item remains
 first. In earlier versions of Python, this was not always the case.

 So, given this list:

 L = [[1,2], [4,0], [3,2], [2,2], [5,1], [1,1]]

 first sort in reverse by the first item, then by the second:


 py L.sort(key=lambda sublist: sublist[0], reverse=True)
 py L.sort(key=lambda sublist: sublist[1])
 py print L
 [[4, 0], [5, 1], [1, 1], [3, 2], [2, 2], [1, 2]]



 Note that using a key function is MUCH more efficient than a cmp function.
 Comparison functions end up doing much more work, and hence are very much
 slower, than a key function.

 Also note that in recent versions of Python, you can do without the lambda
 function and use the special itemgetter function:


 py from operator import itemgetter
 py L = [[1,2], [4,0], [3,2], [2,2], [5,1], [1,1]]
 py L.sort(key=itemgetter(0), reverse=True)
 py L.sort(key=itemgetter(1))
 py print L
 [[4, 0], [5, 1], [1, 1], [3, 2], [2, 2], [1, 2]]


I tried this a lot yesterday but seemed to get a wrong answer but now I
realize its because of a bad test case. Thank you.



 Last but not least, I will show how to do it using a custom cmp function.
 But I recommend that you don't use this!

 def my_cmp(list1, list2):
x = cmp(list1[1], list2[1])
if x == 0:  # a tie
x = cmp(list2[0], list1[0])  # swap the order for reverse sort
# or if you prefer, x = -cmp(list1[0], list2[0])
return x

 sorted(L, my_cmp)




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




-- 
Sarma Tangirala,
Class of 2012,
Department of Information Science and Technology,
College of Engineering Guindy - Anna University
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Sorting Nested Lists

2012-01-09 Thread Walter Prins
Hi,

On 9 January 2012 12:31, Sarma Tangirala tvssarma.ome...@gmail.com wrote:
 Given a nested list, how do I sort the uppermost list based on one key and
 when a special condition occurs a sort on another key should be performed?

 For example, [[1,2], [2, 2], [3, 2], [4, 0]] would be sorted, in my example
 as, [[4, 0], [3, 2], [2, 2], [1, 2]]. That is, sort on the second value and
 in case they are equal, reverse sort on the first value.

 I tried doing this using sorted and using a custom cmp function but not sure
 about how to define the cmp function.

See the following page (particularly the section that describes
multiple levels of sorting) for how to do (I think) what you want
(though this does not use the compare function):
http://wiki.python.org/moin/HowTo/Sorting

If you really want to have this done via the compare function method
then post back again.

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


Re: [Tutor] making a custom file parser?

2012-01-09 Thread Devin Jeanpierre
 IIRC, Python's only non-regular feature is backreferences though

Probably. I'm not too familiar with a couple other features or how
their semantics work, in particular the (?(id)yes|no) syntax.

 I'm not calling bs or anything, I don't know anything about .net
 regexes and I'll readily believe it can be done (I just want to see
 the code for myself).

They add the ability to push and pop from a stack, which turns their
regular expressions into at-least-as-powerful as push-down automata,
which are equivalent in power to context-free-grammars, which means
they can match XML. I think this has been well-known in the .NET
community for years, but nobody had ever done it, and nobody ever
mentioned it. It's a dirty secret you don't tell the newbies because
then they think regexps are fine to use for everything.

It's also why I don't like the this isn't regular so don't use
regular expressions spiel. We call things regular expressions even
when they're context-free parsing expressions! The term has meaning,
but it's no longer tied to finite state automata, and any argument
along that lines is just waiting to be broken by the next feature
addition to the re module.

Anyway, I found the reference I was thinking of:
http://porg.es/blog/so-it-turns-out-that-dot-nets-regex-are-more-powerful-than-i-originally-thought

 Quite right. We haven't seen enough of it to be sure, but that little
 bite seems parseable enough with some basic string methods and one or
 two regexes. That's really all you need, and trying to do the whole
 thing with pure regex is just needlessly overcomplicating things (I'm
 pretty sure we all actually agree on that).

Oh I dunno. If the regex would be simple, it'd be the simplest
solution. As soon as you have order-independence though...

 You mean like flex/bison? May be overkill, but then again, maybe not.
 So much depends on the data.

Flex/Bison are a little old-school / difficult to deal with. I'm more
thinking LEPL or PyMeta or something.

-- Devin

On Sun, Jan 8, 2012 at 9:06 PM, Hugo Arts hugo.yo...@gmail.com wrote:
 On Mon, Jan 9, 2012 at 2:19 AM, Devin Jeanpierre jeanpierr...@gmail.com 
 wrote:
 Parsing XML with regular expressions is generally very bad idea. In
 the general case, it's actually impossible. XML is not what is called
 a regular language, and therefore cannot be parsed with regular
 expressions. You can use regular expressions to grab a limited amount
 of data from a limited set of XML files, but this is dangerous, hard,
 and error-prone.

 Python regexes aren't regular, and this isn't XML.

 A working XML parser has been written using .NET regexes (sorry, no
 citation -- can't find it), and they only have one extra feature
 (recursion, of course). And it was dreadfully ugly and nasty and
 probably terrible to maintain -- that's the real cost of regexes.


 IIRC, Python's only non-regular feature is backreferences though; I'm
 pretty sure that isn't enough to parse XML. It does not make it
 powerful enough to parse context-free languages. I really would like
 that citation though, tried googling for it but not much turned up.
 I'm not calling bs or anything, I don't know anything about .net
 regexes and I'll readily believe it can be done (I just want to see
 the code for myself). But really I still wouldn't dare try without a
 feature set like perl 6's regexes. And even then..

 You're technically correct (it's the best kind), but I feel like it
 doesn't really take away the general correctness of my advice ;)

 In particular, his data actually does look regular.


 Quite right. We haven't seen enough of it to be sure, but that little
 bite seems parseable enough with some basic string methods and one or
 two regexes. That's really all you need, and trying to do the whole
 thing with pure regex is just needlessly overcomplicating things (I'm
 pretty sure we all actually agree on that).

 I'll assume that said (.*). There's still a few problems:  and 
 shouldn't be escaped, which is why you're not getting any matches.
 Also you shouldn't use * because it is greedy, matching as much as
 possible. So it would match everything in between the first unit and
 the last /unit tag in the file, including other unit/unit tags
 that might show up.

 On the can you do work with this with regexes angle: if units can be
 nested, then neither greedy nor non-greedy matching will work. That's
 a particular case where regular expressions can't work for your data.

 Test it carefully, ditch elementtree, use as little regexes as
 possible (string functions are your friends! startswith, split, strip,
 et cetera) and you might end up with something that is only slightly
 ugly and mostly works. That said, I'd still advise against it. turning
 the files into valid XML and then using whatever XML parser you fancy
 will probably be easier.

 He'd probably do that using regexes.


 Yeah, that's what I was thinking when I said it too. Something like,
 one regex to quote attributes, and one that 

[Tutor] Moving from snippits to large projects?

2012-01-09 Thread Mike G
quote How does one go from small to medium, to large, as a coder? /quote

You might look into contributing to an existing project.

There is a new project, MediaLocker, a Python / wxPython app recently
underway, started from a blog post. I
believe they are looking for input, including contributing - have a look.

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


Re: [Tutor] Moving from snippits to large projects?

2012-01-09 Thread leam hall
On 1/9/12, Mike G msg@gmail.com wrote:
 quote How does one go from small to medium, to large, as a coder? /quote

 You might look into contributing to an existing project.

 There is a new project, MediaLocker, a Python / wxPython app recently
 underway, started from a blog post. I
 believe they are looking for input, including contributing - have a look.

 http://www.medialocker.pythonlibrary.org/

Mike,

Media locker looks interesting, thanks!

Leam


-- 
Mind on a Mission http://leamhall.blogspot.com/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] USB volume unique volume info

2012-01-09 Thread Jeff Peery
Hello,
I am writing a python script to install a program onto a customer computer from 
a USB drive. To prevent piracy, I want to know if the user has copied my 
install program to another USB drive. Do USB drives have some unique volume 
info or another feature that I might query to detect if the program is still 
operating on the original USB drive?

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


Re: [Tutor] Game lag

2012-01-09 Thread Japhy Bartlett
If you made an effort to strip out parts of your code, it would  
probably show you where the bottlenecks are.


You say that the large map is not the problem, but do you really know?


On Jan 5, 2012, at 10:08 AM, Nate Lastname defens...@gmail.com wrote:

Thanks for the profilers - never had hear of 'em. Also, no, I cannot  
strip out unnecessary parts, 'cuz I don't know what part is slowing  
it down.  Thanks a lot, though Hugo.


On Thu, Jan 5, 2012 at 11:02 AM, Hugo Arts hugo.yo...@gmail.com  
wrote:
On Thu, Jan 5, 2012 at 3:56 PM, Nate Lastname defens...@gmail.com  
wrote:

 Hello all,

 The attached zip file contains a file called 'cameramovement.py'.   
As you
 can see, this file is extremely laggy.  After searching through my  
code, I
 can't find anything that is slowing it down.  Could someone help  
me out?  It

 takes a while to load due to a large map.  This is not the problem.

 Thanks,
 The Defenestrator

 P.S. Yes, I am new here.  Hello, all!


1) although posting here is a good idea, you're gonna be better off
asking the pygame mailing list about this as well, they're the experts
2) that's a lot of code for me to read all in my spare time man, isn't
there any way you can simplify it or strip out unnecessary parts?
3) did you profile it? Do this! Now! rather than guess where your code
is slow, this will tell you exactly. Use cProfile:

http://docs.python.org/library/profile.html

HTH,
Hugo



--
My Blog - Defenestration Coding

http://defenestrationcoding.wordpress.com/

___
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] Removing certain sequences from a string list elements

2012-01-09 Thread Varsha Purohit
Hello,

I have a simple python program where I am comparing two log files and I am
storing the differences in a list. I am programming in python after a long
time so may be I might have not written something very efficient. Please
let me know what alternate solution I can apply for my program.

I am reading each line in the file individually and storing them in a list.
After that i am comparing the two lists and printing out the differences.
But I wanted to know how can I iter through each string in the list and
remove certain sequences like \n and ',' comma. I want to basically
printout a column where it has each element of the list in each row.
It should also avoid priting the time stamp since they will be different
anyway. i want the program as simple as it looks right now.

Input file contains something like this in each line. I have not included
the complete log file.

*MegaMon mfc*
*MFC data:*
*vendorId/deviceId=1000/005b, subVendorId/subDeviceId=1000/9285, OEM=1,
SubOem=1, isRaidKeySecondary=0*
*MFCF: disableSAS=0, maxDisks=0, enableRaid6=1, disableWideCache=0*
*disableRaid5=0, enableSecurity=0, enableReducedFeatureSet=0*
*enableCTIO=0 enableSnapshot=1 enableSSC=1 enableCacheOffload=0*
*maxHANodes=2*


here is the program

def readList1():
f1 = open('mfc_node1.txt',r)
lines = f1.read().split( )
q = []
for line in lines:
if not line in q:
q.append(line)
f1.close()
return q

def readList2():
f = open('mfc_node2.txt',r)
lines = f.read().split( )
p = []
for line in lines:
if not line in p:
p.append(line)
f.close()
return p



if __name__ == __main__:
q = readList1()
#print q
p = readList2()
#print p
newList = []

for x in q:
if x not in p:
newList.append(x)


Here is the part of the output list
['enableCTIO=0,', 'enableSnapshot=1,', 'enableSSC=1,', 'maxHANodes=0\n',
'sasAddr=5123456712345678\nSecondary', 'enableSnapshot=0,', 'enableSSC=0,',
'sasAddr=\nMegaMon', '13:53:14:']

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


[Tutor] x%2

2012-01-09 Thread emin
http://www.youtube.com/watch?src_vid=QaYAOR4Jq2Efeature=ivannotation_id=annotation_149056v=M3g1GEkmyrw
in this tutorial what does mean x%2 ?
i think: i * 2% = always even number 
but why not 4,6 or 8? but  i * 4(6,8,10,12...)% = always even number too
for example: 100 * 2(4,6,8,10,12...)% = 2(4,6,8,10,12...)
even numbers = {2,4,6,8,10,12...}
and how pyton understanding 0(even or odd number or it is an exception?)?___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Subclassing Exceptions

2012-01-09 Thread Chris Fuller
On Friday 06 January 2012, Steven D'Aprano wrote:
 Chris Fuller wrote:
  class Foo(SyntaxError):
  ...  def __init__(self, a,b,c):
  ...   self.args = (a,b,c)
  ...
  
  raise Foo(1,2,3)
  
  Traceback (most recent call last):
File stdin, line 1, in module
  
  __main__.Foo: None
  
  
  Inheriting from SyntaxError doesn't work!  When I create a new exception,
  I generally subclass from the built-in exception it most resembles, in
  case there was some reason to also catch it via an ancestor.  But I'm
  not sure if that is really all that useful an idea in practice.  How do
  you folk do it?
 
 What do you mean, doesn't work? It looks like it works to me. You get a
 Foo exception, exactly as expected. The error message isn't what you
 expect, because you're making unwarranted assumptions about SyntaxError
 and how it works.
 
 In general, when you override a method, you take full responsibility to
 perform everything that the superclass method was supposed to do. In this
 case, you fail to assign to msg as well as args. It is safer to overload a
 
 message rather than override it:
   class Spam(SyntaxError):
 ... def __init__(self, *args):
 ... if args:
 ... args  = (I pity the fool who made a mistake,) +
 args[1:] ... super(Spam, self).__init__(*args)
 ...
 
   raise Spam('you made a mistake', 1, 2)
 
 Traceback (most recent call last):
File stdin, line 1, in module
 __main__.Spam: I pity the fool who made a mistake
 
 
 
 Unfortunately, there's no real consistency in what arguments exceptions are
 expected to take. The best thing is to read the docs, if they have any, or
 use introspection and trial and error to work out what they do.
 
   try:
 ... raise SyntaxError(you made a mistake)
 ... except SyntaxError, err:
 ... pass
 ...
 
   err.msg
 
 'you made a mistake'
 
 
 See dir(err) for more; you can use help(SyntaxError) but unfortunately it
 isn't very useful.
 
 
 You probably shouldn't inherit from SyntaxError, since it represents syntax
 errors in the Python code being interpreted or compiled. Any syntax error
 in your own data structures should be independent of SyntaxError.

In Python: Essential Reference, David Beazley recommends that the parameters 
of the exception be assigned to the args attribute, so it is passed all the 
way through the traceback.  You will observe that the last element in the 
traceback loses this information when subclassed from SyntaxError.  This isn't 
a problem when the whole traceback is laid out before you, but can come into 
play with automated tools that catch/log/manipulate exceptions.  This behavior 
of exceptions isn't apparently mentioned in the canonical documentation, 
however.

I had the same thought about not wanting to mix syntax errors in the data with 
syntax errors in the code, but that applies to any exception, really.  In 
fact, it's better to inherit from a more derived class, because when you catch 
an ancestor, you'll be grabbing less greedily at the possible coding errors.  
Which suggests that giving a damn about built-in ancestors of user-defined 
exceptions is a losing proposition, now that I think about it.

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


Re: [Tutor] x%2

2012-01-09 Thread Noah Hall
2012/1/8 emin oldcowboyro...@gmail.com:
 http://www.youtube.com/watch?src_vid=QaYAOR4Jq2Efeature=ivannotation_id=annotation_149056v=M3g1GEkmyrw
 in this tutorial what does mean x%2 ?
 i think: i * 2% = always even number
 but why not 4,6 or 8? but  i * 4(6,8,10,12...)% = always even number too
 for example: 100 * 2(4,6,8,10,12...)% = 2(4,6,8,10,12...)
 even numbers = {2,4,6,8,10,12...}
 and how pyton understanding 0(even or odd number or it is an exception?)?

While it is true that all numbers divisible by 4 are even, not all
even numbers are divisible by 4. All even numbers, however, are
divisible by 2.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] x%2

2012-01-09 Thread Steven D'Aprano

emin wrote:

http://www.youtube.com/watch?src_vid=QaYAOR4Jq2Efeature=ivannotation_id=annotation_149056v=M3g1GEkmyrw
in this tutorial what does mean x%2 ?


x % 2 gives the remainder when you divide x by 2.

i think: i * 2% = always even number 


What does that mean? i*2% does not work in Python. In Python, % does not mean 
percentage and so you can't multiply by 2%.


I guess you mean

i % 2 == 0 means i is an even number

and this would be correct. If i % 2 == 0 then i is even. If it equals 1 then i 
is odd.



but why not 4,6 or 8? but  i * 4(6,8,10,12...)% = always even number too


The remainder i % n will be a number between 0 and n-1. i%2 is useful because 
there are only two possible results, 0 (i is even) or 1 (i is odd).


i%4 is less useful, because there are four possibilities:

0 (i is even)
1 (i is odd)
2 (i is even)
3 (i is odd)

i%10 is less useful still, because there are ten possibilities:

0,2,4,6,8: i is even
1,3,5,7,9: i is odd



for example: 100 * 2(4,6,8,10,12...)% = 2(4,6,8,10,12...)
even numbers = {2,4,6,8,10,12...}


I don't understand what you are trying to say here.



and how pyton understanding 0(even or odd number or it is an exception?)?


0 is always an even number, because it has 0 remainder when you divide by 2.



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


Re: [Tutor] USB volume unique volume info

2012-01-09 Thread Steven D'Aprano

Jeff Peery wrote:

Hello, I am writing a python script to install a program onto a customer
computer from a USB drive. To prevent piracy, I want to know if the user
has copied my install program to another USB drive. Do USB drives have some
unique volume info or another feature that I might query to detect if the
program is still operating on the original USB drive?



I'm sorry, this is a mailing list about learning the Python programming 
language, not about the minutia of technical details about USB drives. Have 
you tried googling for USB drive unique identifier?


https://duckduckgo.com/html/?q=usb%20drive%20unique%20identifier



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