[Tutor] Splitting a string

2011-02-08 Thread tee chwee liong

hi all,
 
i have a function which returns a string. for eg: X='101110'. i want to search 
for 0 and highlight the location. 
i am thinking of defining X as a list. but how can i split 101110 as there are 
no spaces in between? i'm thinking if i can put it into a list: 
X=['1','0','1','1','1','0'], then i can use index to point out the locations of 
the 0. pls advise. 
i'm using Python2.5 and WinXP.
 
thanks
tcl   ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Splitting a string

2011-02-08 Thread col speed
On 8 February 2011 16:20, tee chwee liong  wrote:

>  hi all,
>
> >i have a function which returns a string. for eg: X='101110'. i want to
> search for 0 and highlight the location.
> i> am thinking of defining X as a list. but how can i split 101110 as there
> are no spaces in between? i'm thinking if i can put it into a list:
> >X=['1','0','1','1','1','0'], then i can use index to point out the
> locations of the 0. pls advise.
> i>'m using Python2.5 and WinXP.
>
> >thanks
> >tcl
>
> You can use x.find("0"), but this will only give you the first position.
How about our friend 'enumerate'?:
for index, i in enumerate(x):
if i == "0":
print index

Cheers


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


Re: [Tutor] Splitting a string

2011-02-08 Thread Christian Witts

On 08/02/2011 11:20, tee chwee liong wrote:

hi all,

i have a function which returns a string. for eg: X='101110'. i want 
to search for 0 and highlight the location.
i am thinking of defining X as a list. but how can i split 101110 as 
there are no spaces in between? i'm thinking if i can put it into a 
list: X=['1','0','1','1','1','0'], then i can use index to point out 
the locations of the 0. pls advise.

i'm using Python2.5 and WinXP.

thanks
tcl


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


In Python strings are lists of characters so you can use indexes already.
You can look at the .find() function to return you index numbers for the 
location of your search criteria, it only returns the first within your 
parameters, but you can create your own function utilising it to return 
all occurrences.


--
Kind Regards,
Christian Witts


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


Re: [Tutor] zipfile error message

2011-02-08 Thread Steven D'Aprano

Eric Stevens wrote:

Hi:

I am relatively new to Python and have been recently trying to experiment
with its zipfile capabilities. However, everytime I try to open a zip (
using method zipfile.ZipFile(open('zipfile.zip','r')) ) I continue to get an
error message that states:error: unpack requires a string argument of length
46. Would anyone be able to help me figure out what is wrong? I am currently
running Python 2.7 on Windows (not sure if that matters). Thank you.



When posting error messages, please copy and paste the *entire* error 
message. Do not retype it, paraphrase it, or summarize it.


But in this case, I can guess the error. You're opening the zip file in 
text mode:


open('zipfile.zip','r')

which will cause corruption of binary files. Zip files are binary and 
must be either opened in binary mode:


open('zipfile.zip','rb')


or better still, just pass the file name to ZipFile and let it open it:

zipfile.ZipFile('zipfile.zip','r')



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


Re: [Tutor] Splitting a string

2011-02-08 Thread Steven D'Aprano

tee chwee liong wrote:

hi all,

i have a function which returns a string. for eg: X='101110'. i want
to search for 0 and highlight the location. i am thinking of defining
X as a list. but how can i split 101110 as there are no spaces in
between? i'm thinking if i can put it into a list:
X=['1','0','1','1','1','0'], then i can use index to point out the
locations of the 0. pls advise. i'm using Python2.5 and WinXP.


Strings are indexable, and have a find method:

>>> s = "abcd"
>>> s.find('c')
2
>>> s[2]
'c'

To get a list, just ask for one:

>>> list("abcd")
['a', 'b', 'c', 'd']



--
Steven

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


Re: [Tutor] Splitting a string

2011-02-08 Thread David Hutto
> To get a list, just ask for one:
>
 list("abcd")
> ['a', 'b', 'c', 'd']
>


or., and this isn't to argue with anyone;), you could:

>>> x = 'abcd'
>>> y = []
>>> for letter in x:
... y.append(letter)
...
>>> print y
['a', 'b', 'c', 'd']

which explains that the list is derived from the string.

x is 'abcd', and list is the list seperated at the characters, and can
be split further with string
'tags'.
>
>
> --
> Steven
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
According to theoretical physics, the division of spatial intervals as
the universe evolves gives rise to the fact that in another timeline,
your interdimensional counterpart received helpful advice from me...so
be eternally pleased for them.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] vim as a python editor

2011-02-08 Thread Paul Griffiths
On 8 February 2011 04:44, Alexander Fairley wrote:

> Some high profile ruby hackers have put together a pretty snazzy set of
> vim/gvim configs together on github at
>
> https://github.com/carlhuda/janus
>
>
Thank you, but I think this only works on OSX?  I use Ubuntu and if I
understand your link correctly, gvim has the equivalent functionality.



> On the topic of configuring Capslock to be an escape key, it's because
> that's where the "meta" key used to be on old school unix keyboards, and so
> it makes you double plus unix if you reconfigure things that way(also has
> the plus of rendering emacs a lot more usable).
>


Sorry, I don't know what 'meta' key and 'double plus' means.   What I've now
done, using Preferences > Keyboard, is to swap the functionality of the Esc
and Caps Lock keys.  This helps me because I'm a fairly competent touch
typist.  Every time I need to press Esc on a default keyboard, I have to
lift my left hand from the asdf home keys to get at it.  This 'breaks the
flow' and gets quite annoying after a while.


>
> On Fri, Jan 7, 2011 at 7:22 PM, Steven D'Aprano wrote:
>
>> Alan Gauld wrote:
>>
>>> "Paul Griffiths"  wrote
>>>
 I've learned that:
 ...

 - re-configuring the Caps Lock to be an extra Esc saves time

>>>
>>> Huh? How do you use that? Its a new one on me. Why would two escape keys
>>> be useful?
>>>
>>
>> What if you want to escape the escape, so that (say) esc-C is the same as
>> just C?
>>
>>
>> Not-very-helpfully y'rs,
>>
>> --
>> 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] Converting From Unicode to ASCII!!

2011-02-08 Thread Steven D'Aprano

Nevins Duret wrote:


A good friend of mine locked herself out of her computer and forgot her
password.  I pretty much scoured the internet as 
a resource only to hit a brick wall.  I tried using ophcrack version 2.3.1
in order to obtain the password and felt completely at home 
being that it was Linux,  but then towards the end it failed and the output

of this file which seems to contain the Users and passwords
but in Unicode format:

 


Administrator:500::31d6cfe0d16ae931b73c59d7e0c089c 0:::
Guest:501::31d6cfe0d16ae931b73c59d7e0c089c0:::
SUPPORT_388945a0:1002::881037e0b6909b04b6900f7c806 dca6e:::
HelpAssistant:1004:b209ce7e3ff7aea1131906e9f5df481
9:d83f663c50bcd5815ccb94f9e38a9a4b:::
Beverly:1005:6395b1acd69aaad3b435b51404ee:992a
c78ffb08204c592c6e47b916f85d:::


I see no Unicode there. It looks like plain ASCII. Are you sure you 
understand how to use ophcrack? Perhaps you should be asking on a 
ophcrack mailing list (if they have one).




And it didn't complete as a result of this error message:

Tables found:
/mnt/hdc/tables/xp_free_small

Found one partition that contains hashes:
/mnt/hda1/WINDOWS/system32/config

Starting Ophcrack
QIconvCodec::convertFromUnicode: using ASCII for conversion, iconv_open
failed
QIconvCodec::convertToUnicode: using ASCII for conversion, iconv_open failed
/home/tux/launch.sh: line 100: 1044 Killed
ABLES_INLINE -w $FOUND/ -n $numcpu -o /tmp/ophcrack.txt $opts
Press a key to exit::


Looks to me like a bug in ophcrack, although I could be wrong.



Now , I remember reading somewhere that Python is very good for converting
Unicode
data into ASCII and admittedly know nothing about this:  


Before asking any more questions about converting Unicode to ASCII, I 
think you need to read this article:


http://www.joelonsoftware.com/articles/Unicode.html

You can't just "convert" Unicode to ASCII -- what would you expect to 
get if you convert (say) "πΣՋوຜ₦ℵ↷"? For starters, you need to know what 
encoding(s) are being used.




Is there a work around in Python where I can simply import the file like and
convert it to readable string using 
A for loop.


Import *which* file?

What does ophcrack consider "readable"?

How do you know that you're not making the problem worse?

No, don't go down this track. You'll probably end up making your 
friend's computer completely unusable without a complete re-build. My 
advice is to start with Microsoft's instructions:


http://support.microsoft.com/kb/321305

and if they aren't helpful, then try using a Linux live cd to reset the 
password. You can find instructions on the internet, such as:


http://www.ehow.com/how_6532691_change-xp-password-linux.html
http://www.psychocats.net/ubuntucat/resetwindowspassword/


--
Steven

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


Re: [Tutor] Splitting a string

2011-02-08 Thread tee chwee liong

hi all, 
 
thanks for the advice. i modified my code to be:
 
c=('01101')
i=-1
try:
while 1:
i=c.index('0',i+1)
print "Lane fail",i
except ValueError:
print "All Lanes PASS"
pass
 
when i run, the result is:
 
>>> 
Lane fail 0
Lane fail 3
All Lanes PASS
 
Question: How do i prevent the code from executing the except ValueError part. 
it seems to be checking bit by bit and when is see 1 it will execute the except 
part.
i just want the results to be:
>>> 
Lane fail 0
Lane fail 3
 
 
thanks
tcl76 ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Splitting a string

2011-02-08 Thread David Hutto
Don't use the try and except, use a if else. I'm not as good as an
explainer as the pros, but I can make enough sense if you respond back
with a useful set of examples you've tried.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Splitting a string

2011-02-08 Thread Christian Witts

On 08/02/2011 15:04, tee chwee liong wrote:

hi all,

thanks for the advice. i modified my code to be:

c=('01101')
i=-1
try:
while 1:
i=c.index('0',i+1)
print "Lane fail",i
except ValueError:
print "All Lanes PASS"
pass

when i run, the result is:

>>>
Lane fail 0
Lane fail 3
All Lanes PASS

Question: How do i prevent the code from executing the except 
ValueError part. it seems to be checking bit by bit and when is see 1 
it will execute the except part.

i just want the results to be:
>>>
Lane fail 0
Lane fail 3


thanks
tcl76


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


`while i < len(c)` instead of `while 1`

--
Kind Regards,
Christian Witts


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


Re: [Tutor] Splitting a string

2011-02-08 Thread David Hutto
On Tue, Feb 8, 2011 at 8:36 AM, Christian Witts  wrote:
> On 08/02/2011 15:04, tee chwee liong wrote:
>>
>> hi all,
>>
>> thanks for the advice. i modified my code to be:
>>
>> c=('01101')
>> i=-1
>> try:
>>    while 1:
>>        i=c.index('0',i+1)
>>        print "Lane fail",i
>> except ValueError:
>>    print "All Lanes PASS"
>>    pass
>>
>> when i run, the result is:
>>
>> >>>
>> Lane fail 0
>> Lane fail 3
>> All Lanes PASS
>>
>> Question: How do i prevent the code from executing the except ValueError
>> part. it seems to be checking bit by bit and when is see 1 it will execute
>> the except part.
>> i just want the results to be:
>> >>>
>> Lane fail 0
>> Lane fail 3
>>
>>
>> thanks
>> tcl76
>>
>>
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
> `while i < len(c)` instead of `while 1`

You remind me of me...like it was yesterday. Explanations with no
thought. Unimpressive isn't it?

>
> --
> Kind Regards,
> Christian Witts
>
>
>



-- 
According to theoretical physics, the division of spatial intervals as
the universe evolves gives rise to the fact that in another timeline,
your interdimensional counterpart received helpful advice from me...so
be eternally pleased for them.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Splitting a string

2011-02-08 Thread Christian Witts

On 08/02/2011 15:38, David Hutto wrote:

On Tue, Feb 8, 2011 at 8:36 AM, Christian Witts  wrote:
   

On 08/02/2011 15:04, tee chwee liong wrote:
 

hi all,

thanks for the advice. i modified my code to be:

c=('01101')
i=-1
try:
while 1:
i=c.index('0',i+1)
print "Lane fail",i
except ValueError:
print "All Lanes PASS"
pass

when i run, the result is:

   
 

Lane fail 0
Lane fail 3
All Lanes PASS

Question: How do i prevent the code from executing the except ValueError
part. it seems to be checking bit by bit and when is see 1 it will execute
the except part.
i just want the results to be:
   
 

Lane fail 0
Lane fail 3


thanks
tcl76

   

`while i<  len(c)` instead of `while 1`
 

You remind me of me...like it was yesterday. Explanations with no
thought. Unimpressive isn't it?
   


While the index is smaller than the length of the string do your 
processing.  It reads like that.  But thank you for your commentary.


--
Kind Regards,
Christian Witts


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


Re: [Tutor] Splitting a string

2011-02-08 Thread Peter Otten
Christian Witts wrote:

> On 08/02/2011 15:04, tee chwee liong wrote:
>> hi all,
>>
>> thanks for the advice. i modified my code to be:
>>
>> c=('01101')
>> i=-1
>> try:
>> while 1:
>> i=c.index('0',i+1)
>> print "Lane fail",i
>> except ValueError:
>> print "All Lanes PASS"
>> pass
>>
>> when i run, the result is:
>>
>> >>>
>> Lane fail 0
>> Lane fail 3
>> All Lanes PASS
>>
>> Question: How do i prevent the code from executing the except
>> ValueError part. it seems to be checking bit by bit and when is see 1
>> it will execute the except part.
>> i just want the results to be:
>> >>>
>> Lane fail 0
>> Lane fail 3

> `while i < len(c)` instead of `while 1`

You have an off-by-one bug. But even if you fix that you'll still enter the 
except suite if the string c doesn't end with a "0". You need to keep track 
of the failed lanes, e. g.: 

for c in "011010", "111", "000", "", "1", "0", "001":
print c.center(20, "-")

i = -1
all_passed = True
try:
while True:
i = c.index('0', i+1)
print "Lane fail", i
all_passed = False
except ValueError:
pass
if all_passed:
print "All Lanes PASS"


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


Re: [Tutor] Splitting a string

2011-02-08 Thread Hugo Arts
On Tue, Feb 8, 2011 at 2:47 PM, Christian Witts 
>>> `while i<  len(c)` instead of `while 1`
>>>
>>
>> You remind me of me...like it was yesterday. Explanations with no
>> thought. Unimpressive isn't it?
>>
>
> While the index is smaller than the length of the string do your processing.
>  It reads like that.  But thank you for your commentary.
>

The goal of this list is not to criticize, David, but to help. Why not
provide some explanations yourself?

EIther way, even with this correction a ValueError is still raised at
least *once* if the string doesn't end with a 0, so it won't actually
help a bit. A quick fix is to set some variable if you find a 0, then
check on that variable after the loop (See Peter's example).

Now, find is nice if you care about finding one instance, but if you
need to find *all* instances you'll have to check the entire string
anyway, and find becomes a rather cumbersome tool. Interestingly,
enumerate was suggested all the way at the beginning of the thread,
and I think it's the best answer:

zeroes = ["lane fail {0}".format(i) for i, val in enumerate(c) if val == '0']
print '\n'.join(zeroes) if zeroes else "All Lanes pass"

Ok, That looks a little advanced, but strip away all that syntactic
sugar and you arrive at basically this:

zeroes = []
for index, value in enumerate(c):
if value == '0':
zeroes.append(index)

if zeroes:
for x in zeroes:
print "lane fail", x
else:
print "all lanes pass"

Since we have to scan the whole string anyway, this is equally
efficient as using find (theoretically!!! if you care about
performance, *measure*).

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


[Tutor] CrackTheInterview

2011-02-08 Thread Polisetti, Vinay
Visit http://www.cracktheinterview.org/ for more interview preparation tips and 
interview experiences of various people

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


Re: [Tutor] zipfile error message

2011-02-08 Thread Wayne Werner



On Mon, 7 Feb 2011, Eric Stevens wrote:


Hi:
I am relatively new to Python and have been recently trying to experiment
with its zipfile capabilities. However, everytime I try to open a zip (
using method zipfile.ZipFile(open('zipfile.zip','r')) ) I continue to get an
error message that states:error: unpack requires a string argument of length
46. Would anyone be able to help me figure out what is wrong? I am currently
running Python 2.7 on Windows (not sure if that matters). Thank you.


zip files are not ascii text, so in order to send it to the zipfile you 
need to open it with the 'rb' flag to indicate you want to read in binary 
mode.


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


[Tutor] Apologies, mea culpa

2011-02-08 Thread Alan Gauld
Sorry folks, 
I meant to hit discard on this one but clicked Accept by mistake.


Alan G.
List moderator

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


Re: [Tutor] Splitting a string

2011-02-08 Thread Alan Gauld


"tee chwee liong"  wrote

i have a function which returns a string. for eg: X='101110'. 
i want to search for 0 and highlight the location. 


I'm not sure what you mean by highlight the location. 
Is it a GUI? Are you colour coding the characters?


i am thinking of defining X as a list. 


You shouldn't need to, you can treat the string as a 
sequence directly


indexes = [i for i, c in enumerate(X) if char == '0']

Will give you the list of indexes you need. 
How you use that to highlight the zeros will depend 
on your UI I guess.


HTH,

--
Alan Gauld
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] Splitting a string

2011-02-08 Thread tee chwee liong


> I'm not sure what you mean by highlight the location. 
> Is it a GUI? Are you colour coding the characters?
> 

hi,
 
no it is not GUI. i just want to know where is the location of the 0 in the 
returned string. For eg: 10111, i want to say 0 is at lane 3 (calculating 
location from right to left).
 
thanks___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Splitting a string

2011-02-08 Thread tee chwee liong

> `while i < len(c)` instead of `while 1`
> 

hi, 
 
i modified codes to be i>> 
Lane fail 0
Lane fail 3
All Lanes PASS
>>> 
 
  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Splitting a string

2011-02-08 Thread Alan Gauld


"tee chwee liong"  wrote
###
c=('01101')
i=-1
try:
   while i   print "Lane fail",i
except ValueError:
   print "All Lanes PASS"

   pass
# 
The first thing is that you never increment i so the 
while condition is never going to terminate, so its 
probably just as well you get the exception.


The next thing is that you get the exception whenever 
index() can't find a zero. So the exception does not 
mean *ALL* lanes pass, it means all the lanes from 
the last zero pass. You really want the exception
to stop the search. But it won't indicate all pass 
unless it happens the first time...


Using your logic it is easier written as a for loop:

s='01101'
found = False
for i,c in enumerate(s):
   if c == '0': 
  print 'Lane fail',i

  found = True
if not Found:  print 'All lanes PASS'

HTH,


--
Alan Gauld
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] Splitting a string

2011-02-08 Thread tee chwee liong

hi all,
 
the code works:

s='00101'
found = False
for i,c in enumerate(s):
if c == '0':
print 'Lane fail',i
found = True
if not found: print 'All lanes PASS
#
 
Result:
>>> 
Lane fail 0
Lane fail 1
Lane fail 3
>>> 
 
the enumerate is checking from left to right. is it possible check from right 
to left? 
for eg:s='00101'

Will give below result:
Lane fail 1
Lane fail 3
Lane fail 4

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