Re: [Tutor] (no subject)

2005-05-26 Thread Pujo Aji
Actually you can do the same way:

# Convert intAscii to charAscii
S = [chr(x) for x in range(0,256)]
for x in S: print x

#Convert charAscii to intAscii
AsciiInt = [ord(x) for x in S]
for x in AsciiInt: print x

Best Regards,
pujo

On 5/26/05, John Carmona <[EMAIL PROTECTED]> wrote:
> With the help of Pujo Aji I have written this little script that print every
> single ASCII code>>
> 
> S = [chr(x) for x in range (0,256)]
> for x in S:
> print x,
> 
> The next step is to use the built-in functin ord() in order to convert each
> character to an ASCII integer. I have had a look at the ord() function but
> it says that it only take one argument i.e. ord('a'). How could I execute to
> convert each character into an ASCII integer?
> 
> Thanks in advance
> JC
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python won't play .wav file

2005-05-26 Thread Alan G
> And if I'm on Linux or programing for Mac?

Sadly sound is one of those things that tends to be 
system dependant. There is a Sun audio module somewhere too...
On the Mac I believe its a Quicktime thing using the MacPython 
libraries.

The good news is that I think PyGame provides a platform independant 
sound capability so I'd recommend that route, although I've never 
used it. In fact, come to think of it, in my 30 years of programming,
sound is one of the few things I've never, ever worked with! 
(I once built a midi player on Windows using the Delphi player 
control but that hardly counts!)

Alan G.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] (no subject)

2005-05-26 Thread Alan G
> S = [chr(x) for x in range (0,256)]
> for x in S:
> print x,

for x in range(256): print chr(x),

> The next step is to use the built-in functin ord() in order to
convert each
> character to an ASCII integer.

That will give you the numbers 0..255

ord() is simply the inverse of chr()

so that

x == ord(chr(x))

> it says that it only take one argument i.e. ord('a'). How could I
execute to
> convert each character into an ASCII integer?

The same as you did for chr(), use a loop:

S = [chr(x) for x in range (0,256)]
for x in S:
 print ord(x),

But it will simply print out the list of numbers from 0..255.

Do you have a purpose in mind or are you simply wanting to
observe chr() and ord() in action?

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] (no subject)

2005-05-26 Thread Ben Vinger
Or like this:

for x in range (0,256):
print ord(chr(x)), ': ', chr(x)

(you could just print x, instead of ord(chr(x)), but
then you would not be using ord)

Ben

--- Pujo Aji <[EMAIL PROTECTED]> wrote:
> Actually you can do the same way:
> 
> # Convert intAscii to charAscii
> S = [chr(x) for x in range(0,256)]
> for x in S: print x
> 
> #Convert charAscii to intAscii
> AsciiInt = [ord(x) for x in S]
> for x in AsciiInt: print x
> 
> Best Regards,
> pujo
> 
> On 5/26/05, John Carmona <[EMAIL PROTECTED]>
> wrote:
> > With the help of Pujo Aji I have written this
> little script that print every
> > single ASCII code>>
> > 
> > S = [chr(x) for x in range (0,256)]
> > for x in S:
> > print x,
> > 
> > The next step is to use the built-in functin ord()
> in order to convert each
> > character to an ASCII integer. I have had a look
> at the ord() function but
> > it says that it only take one argument i.e.
> ord('a'). How could I execute to
> > convert each character into an ASCII integer?
> > 
> > Thanks in advance
> > JC
> > 
> > 
> > ___
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 



___ 
Can't remember an address in your address book? Enter the first few letters and 
Address AutoComplete will automatically finish it. 
Get Yahoo! Mail http://uk.mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Am I making this harder than it needs to be?

2005-05-26 Thread Ron Phillips


Ron Phillips wrote:>> short version: I  need a way to get max and min E and N out of >> [(E0,N0),(E1,N1)...(En,Nn)] without reordering the list>>> Kent Johnson [EMAIL PROTECTED]> 5/25/2005 10:19 AM >>>For Python < 2.4 you need another set of [ ] e.g. min([e for e,n in coordList])

I knew I was working too hard!! That's exactly what I needed -- for some reason, ESRI (big name in geographic work) uses Python 2.2.
 
Ron Phillips wrote:
>> long version:>>  >> I would like a list of geographic coordinates (Easting, Northing) to >> maintain a "bounding box" >> [(minEasting,minNorthing),(maxEasting,maxNorthing)] attribute.I did it like this:class geoCoordinateList(UserList):>>def __init__(self):>>UserList.__init__(self)>>self.boundingBox = geoBox(minEN=geoCoordinate(),>>maxEN=geoCoordinate())>>def append(self, geoCoord):>>self.extend([geoCoord])>>self.boundingBox.challenge(geoCoord)#bumps the max and min if >>necessary>>but I'd have to override almost all the UserList methods, unless there's >>a way to call boundingBox.challenge() on any change to the list.>If you don't need too much list functionality it might be easier to wrap a list and delegate the >things you need:>class geoCoordinateList(object):>def __init__(self):>self.list = list()>self.boundingBox = geoBox(minEN=geoCoordinate(),>maxEN=geoCoordinate())>def append(self, geoCoord):>self.list.append(geoCoord) # No need for extend() here...>self.boundingBox.challenge(geoCoord)#bumps the max and min if necessary>I think you will want at least __getitem__() and __len__() as well.>Kent
Yep, that was my problem; I had "too much list functionality" to delegate -- your way is much better for the purpose. I can expose that class without promising all the functionality of a list.
 
In most languages there are a half-dozen stupid, painful ways to do a job, and maybe one that's merely painful, but in Python there's a half-dozen "ok" ways to do a job, and at least one that's a real pleasure!
 
Thanks for the answers!
 
Ron


Header
Description: Binary data
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Program to lock folders under win32

2005-05-26 Thread Mark Kels
Hi list.

I want to make a program to lock folders (so the user can only access
them if he knows the password) under win32, the only problem is that I
have no idea how to start or what to do. Do you guys have any ideas of
how to do it?

Thanks!


-- 
1. The day Microsoft makes something that doesn't suck is probably the
day they start making vacuum cleaners.
2. Unix is user friendly - it's just picky about it's friends.
3. Documentation is like sex: when it is good, it is very, very good.
And when it is bad, it is better than nothing. - Dick Brandon
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] python problem

2005-05-26 Thread Feziwe Mpondo
hi
i'm trying to write a code that handle errors help

def print_menu():
print '1. Print Phone Numbers'
print '2. Add a Phone Number'
print '3. Remove a Phone Number'
print '4. Lookup a Phone Number'
print '5. Quit'
print
numbers = {}
menu_choice = 0
print_menu()


while menu_choice != 5:
menu_choice = input("Type in a number (1-5):")
break
except(TypeError,NameError):
if menu_choice == 1:
print "Telephone Numbers:"
for x in numbers.keys():
print "Name: ",x," \tNumber: ",numbers[x]
print
elif menu_choice == 2:
print "Add Name and Number"
name = raw_input("Name:")
phone = raw_input("Number:")
numbers[name] = phone
elif menu_choice == 3:
print "Remove Name and Number"
name = raw_input("Name:")
if numbers.has_key(name):
del numbers[name]
else:
print name," was not found"
elif menu_choice == 4:
print "Lookup Number"
name = raw_input("Name:")
if numbers.has_key(name):
print "The number is",numbers[name]
else:
print name," was not found"
else:

   print_menu()
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Are Empty "Placeholder" Functions possible?

2005-05-26 Thread Gooch, John
Is there a way to create an empty function definition with no lines of code
in it? In my coding style I will often do this ( in other languages ) for
RAD just to remind myself that I will need to implement the function later. 

Example: For a file handling class, I may need functions such as
copy,delete,move,etc so I want to start off with:

class FileHandler:
def __init__(self):

def copy(self):

def delete(self):
.
.
.


then in my driver/aka testing file:

import FileHandler

def main()
MyHandler = FileHandler()
print "Handler Created."

main()




Right now, this won't work as the compliler will error on functions with
nothing in them. Is there a "no-op", "do nothing", or some similar command I
can put in the empty functions to keep the compiler/interpreter from
erroring on them? This would really help speed up my development so that I
can create the class skeleton quickly and then implement each function and
test it as I go.


Thank You, 


John A. Gooch
Systems Administrator
IT - Tools
EchoStar Satellite L.L.C.
9601 S. Meridian Blvd.
Englewood, CO  80112
Desk: 720-514-5708 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Are Empty "Placeholder" Functions possible?

2005-05-26 Thread Kent Johnson
Gooch, John wrote:
> Is there a way to create an empty function definition with no lines of code
> in it? In my coding style I will often do this ( in other languages ) for
> RAD just to remind myself that I will need to implement the function later. 

A block cannot be empty but there is a placeholder statement 'pass' that does 
nothing.
> 
> Example: For a file handling class, I may need functions such as
> copy,delete,move,etc so I want to start off with:
> 
> class FileHandler:
>   def __init__(self):
> 
>   def copy(self):
> 
>   def delete(self):

class FileHandler:
def __init__(self):
pass

def copy(self):
pass

def delete(self):
pass

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Expression order problem

2005-05-26 Thread William O'Higgins
I am running into problems with script evaluation order - specifically,
the Python interpreter seems to read and execute scripts line by line.
This is a problem if you are used to Perl, where the whole script is
parsed first (Perl also auto-vivifies variables, but that's a different
problem for a different day).  Here is a test script:

#!/usr/bin/python

import sys

class test:
variable = "is this a variable" + other_argument

if sys.argv[1:]:
argument = sys.argv[1]
other_argument = sys.argv[2]
instance = test()
print instance.variable
else:
sys.exit()

I am only beginning to get my head around OO programming, so please bear
with me.  In Perl, I would define all of my subs (functions or classes)
at the end of the program, because the Perl parser will read the whole
program, and then execute it.  So I'm having trouble from the start
because I can't do that - the interpreter whines about undefined calls
when things are defined further down the page.  In my example though - I
want to use variables from one code block in another, but whichever one
is first, something is undefined.  I'm not necessarily looking for a
fix, but pointers as to technique to avoid this would be appreciated.
Thanks.
-- 

yours,

William



signature.asc
Description: Digital signature
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Are Empty "Placeholder" Functions possible?

2005-05-26 Thread Gooch, John
Thanks Kent!

-Original Message-
From: Kent Johnson [mailto:[EMAIL PROTECTED] 
Sent: Thursday, May 26, 2005 9:52 AM
To: Gooch, John
Cc: tutor@python.org
Subject: Re: [Tutor] Are Empty "Placeholder" Functions possible?


Gooch, John wrote:
> Is there a way to create an empty function definition with no lines of 
> code in it? In my coding style I will often do this ( in other 
> languages ) for RAD just to remind myself that I will need to 
> implement the function later.

A block cannot be empty but there is a placeholder statement 'pass' that
does nothing.
> 
> Example: For a file handling class, I may need functions such as 
> copy,delete,move,etc so I want to start off with:
> 
> class FileHandler:
>   def __init__(self):
> 
>   def copy(self):
> 
>   def delete(self):

class FileHandler:
def __init__(self):
pass

def copy(self):
pass

def delete(self):
pass

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Are Empty "Placeholder" Functions possible?

2005-05-26 Thread Nick Lunt
Hi John,

you can use 'pass' . This works the same as with exceptions, eg

try:
open('somefile')
except IOError:
pass

so we can have

class doNothing:
def __init__(self):
pass
def boring(self, other):
pass

Hope that helps :)
Nick

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
Behalf Of Gooch, John
Sent: 26 May 2005 16:43
To: tutor@python.org
Subject: [Tutor] Are Empty "Placeholder" Functions possible?


Is there a way to create an empty function definition with no lines of code
in it? In my coding style I will often do this ( in other languages ) for
RAD just to remind myself that I will need to implement the function later.

Example: For a file handling class, I may need functions such as
copy,delete,move,etc so I want to start off with:

class FileHandler:
def __init__(self):

def copy(self):

def delete(self):
.
.
.


then in my driver/aka testing file:

import FileHandler

def main()
MyHandler = FileHandler()
print "Handler Created."

main()




Right now, this won't work as the compliler will error on functions with
nothing in them. Is there a "no-op", "do nothing", or some similar command I
can put in the empty functions to keep the compiler/interpreter from
erroring on them? This would really help speed up my development so that I
can create the class skeleton quickly and then implement each function and
test it as I go.


Thank You,


John A. Gooch
Systems Administrator
IT - Tools
EchoStar Satellite L.L.C.
9601 S. Meridian Blvd.
Englewood, CO  80112
Desk: 720-514-5708


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


--
No virus found in this incoming message.
Checked by AVG Anti-Virus.
Version: 7.0.322 / Virus Database: 266.11.17 - Release Date: 25/05/2005


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Expression order problem

2005-05-26 Thread Andrei
William O'Higgins  utoronto.ca> writes:

> This is a problem if you are used to Perl, where the whole script is
> parsed first (Perl also auto-vivifies variables, but that's a different
> problem for a different day).  Here is a test script:

The Python compiler parses the whole script too, can't compile otherwise. Of
course a variable must exist if its value is requested. I don't know what
vivifying a variable means, but surely Perl doesn't pull a non-existing variable
out of its sleeves? 

> class test:
> variable = "is this a variable" + other_argument
> 
> if sys.argv[1:]:
> other_argument = sys.argv[2]
> instance = test()
> print instance.variable
> else:
> sys.exit()
> I am only beginning to get my head around OO programming, so please bear
> with me.  In Perl, I would define all of my subs (functions or classes)
> at the end of the program, because the Perl parser will read the whole
> program, and then execute it.  So I'm having trouble from the start

That's what Python does too. But the variable assignment above is not in a
function, so it's executed immediately like any other statement.

> because I can't do that - the interpreter whines about undefined calls
> when things are defined further down the page.

It whines if they're used before they're defined. It's perfectly fine to have
them, undefined and all, as long as they aren't used.

> In my example though - I
> want to use variables from one code block in another, but whichever one
> is first, something is undefined.  I'm not necessarily looking for a
> fix, but pointers as to technique to avoid this would be appreciated.
> Thanks.

I can think of several possibilities:

1. assign a value to your variable before using it in the class definition. You
can do this by putting the assignment code above the class definition or putting
the assignment code in a separate module and importing it at the top of the
module which defines the class.

2. assign the attribute to the class only once its value is known:

>>> class tst:
... var = None
...
>>> myvar = 5
>>> tst.var = myvar
>>> inst = tst()
>>> inst.var
5

3. Make it an attribute of the instance rather than of the class:

>>> class tst2:
... def __init__(self):
... self.var = newvar
... print 'tst2.__init__ executed'
...
>>> tst2()
Traceback (most recent call last):
  File "", line 1, in ?
  File "", line 3, in __init__
NameError: global name 'newvar' is not defined
>>> newvar = 57
>>> tst2().var
tst2.__init__ executed
57

It would be better to pass the extra argument as function parameter to __init__,
but anyway.

4. Generate the class after the value of the variable is known:

>>> def generateclass():
... class test4:
... var = newervar
... return test4
...
>>> test = generateclass()
Traceback (most recent call last):
  File "", line 1, in ?
  File "", line 2, in generateclass
  File "", line 3, in test4
NameError: name 'newervar' is not defined
>>> newervar = 100
>>> test = generateclass()
>>> instance = test()
>>> instance.var
100

5. Use a class method instead of an attribute:

>>> class test5:
... @classmethod
... def var(cls):
... return dummy
...
>>> test5.var()
Traceback (most recent call last):
  File "", line 1, in ?
  File "", line 4, in var
NameError: global name 'dummy' is not defined
>>> dummy = 234
>>> test5.var()
234
>>> test5().var()
234

Which soultion is right, depends on your needs. I'm tempted to say (3) is the
best one, unless you really need that to be a class attribute. 

Tip: http://zephyrfalcon.org/labs/python_pitfalls.html

Yours,

Andrei


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] better resolution on time.sleep()?

2005-05-26 Thread Roger Merchberger
Rumor has it that Alan G may have mentioned these words:

> > I'm running an application that has a polling loop to check a serial
>port
> > for certain signals, and on my laptop I can get about 6700 samples
>per
> > second, which (of course) consumes 100% CPU; which may impact
>battery life.
>
>Consuming 100% CPU won't make much difference, the CPU is running all
>the time anyway (when its not asleep).

Not mine... my laptop runs a Crusoe processor, and it can automatically 
scale itself from 300Mhz to 933Mhz. It's called "LongRun" technology, and 
it works quite well, too. On my extended battery at "near idle" ... say, 
playing Spider, I get 5+ hours of battery life (and the battery's 2 years 
old, just like the laptop). Running the program at full tilt, I get just 
over 2 hours (but admittedly, it's scanning my USB->RS232 dongle, and I 
don't know how much current that may take... However, it's not 
transmitting, so it shouldn't be an overly huge amount.) I can run more 
tests if anyone's interested, but...

Since the first Pentium (and possibly '486s) the CPU could put itself in a 
wait state to conserve CPU cycles that weren't being used -- and "wake 
itself up" automatically when needed. WinNT 4.0 and newer (and most *nixen) 
could use that, altho it didn't make *as much* difference for thermal or 
power aspects back then; the ability was there mainly for multiprocessor 
applications. Now they've improved it for both thermal and power usage 
functions as well.

>  What will consume batteries much
>faster is if you are accessing hardware such as disk or modem or other 
>power hungry devices.

I would only get about 1.5 hours (when the battery was new) compiling my 
LinuxFromScratch -- 100% CPU, HD getting hammered and snagging packages off 
of the CDROM... yea, the battery (and external charger) got quite a workout 
those few weeks... ;-)

>I've never tried in Python but can you use select()?
>I've used it in C for short duration intervals.

Never used it yet (altho I saw references that Python's time.sleep() 
actually *uses* select instead of usleep/nanosleep.

Not being a C hacker, I don't know much about it, but I'll check it out. 
Thanks!

=-=-=

I was also looking into trying the usleep implementation in wxPython -- 
well, I couldn't get the rascal compiled on my old Linux From Scratch 
version 4 installation (my main working Linux for that laptop) no matter 
what I turned off in .configure... I guess I should prioritize my LFS6 
install a bit higher. ;-)

Thanks one and all, and back to "quiet mode" I go... ;-)
Roger "Merch" Merchberger
--

Roger "Merch" Merchberger   | "Profile, don't speculate."
SysAdmin, Iceberg Computers | Daniel J. Bernstein
[EMAIL PROTECTED]  |

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python problem

2005-05-26 Thread Andrei
Feziwe Mpondo  sanbi.ac.za> writes:

> i'm trying to write a code that handle errors help

I think the indentation was screwed up.

> while menu_choice != 5:
> menu_choice = input("Type in a number (1-5):")

I'd avoid using input. Use raw_input instead.

> break

break stops a loop. So you display the menu once and stop immediately after the
user has selected an action. That can't be right.

> except(TypeError,NameError):

Exceptions are caught in a try except block, where the code that might
malfunction comes after try, and the code to handle the malfunctioning comes
after except. It therefore looks something like this:

  userinput = raw_input('Choice: ')
  try: # userinput may or may not be valid
  choice = int(userinput) # might fail if userinput is random text
  except (ValueError): # what to do if couldn't convert to integer
  print 'Not a number'
  choice = 5

Yours,

Andrei


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Are Empty "Placeholder" Functions possible?

2005-05-26 Thread Danny Yoo


> Is there a way to create an empty function definition with no lines of
> code in it? In my coding style I will often do this ( in other languages
> ) for RAD just to remind myself that I will need to implement the
> function later.

Hi Nick,

I agree with the others; using the no-op statement 'pass' is probably the
way to go here.

If I'm paranoid, I sometimes use a body that just raises a
NotImplementedError exception:

##
>>> def functionIHaventWrittenYet():
... raise NotImplementedError
...
>>> functionIHaventWrittenYet()
Traceback (most recent call last):
  File "", line 1, in ?
  File "", line 2, in functionIHaventWrittenYet
NotImplementedError
##

but, often, just putting in 'pass' stubs is enough.


Best of wishes!

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] pattern matching problem

2005-05-26 Thread cgw501
Hi,

I have to write a function that will return the index of a line like this:

gvcdgvcgdvagTVTVTVTVTVTHUXHYGSXUHXSU

where it first becomes capital letters. I've had about a hundred different 
ideas of the best way to do this, but always seem to hit a fatal flaw. Any 
thoughts?

Thanks,

Chris
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Expression order problem

2005-05-26 Thread Danny Yoo


On Thu, 26 May 2005, William O'Higgins wrote:

> I am running into problems with script evaluation order - specifically,
> the Python interpreter seems to read and execute scripts line by line.

Hi William,


Ah!  This happens to me too; one common idiom to avoid definition-order
issues like this is to create some central main() function:

##
def main():
...
##


and then, at the very bottom of the file, add a bit of code to start
things up:

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

The reason this works is because main() is called only after all the other
definitions are in place.  That is, we start things up only after the name
bindings are done.



> This is a problem if you are used to Perl, where the whole script is
> parsed first (Perl also auto-vivifies variables, but that's a different
> problem for a different day).

Perl does do that first pass to collect symbols, but it can also be
fraught with gotchas.  For example, the Perl code here:

### Perl ###
print count(), "\n";
print count(), "\n";
print count(), "\n";

{ my $x = 42;
  sub count {
  $x++;
  $x;
  }
}
##

exposes a similar kind of gotcha with evaluation order.


It's usually best to avoid the situation altogether, and get the
definitions in front first.  Using the "main()/if __name__ == '__main__'"
idiom is one way to avoid the definition sequence issues.


Best of wishes!

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Are Empty "Placeholder" Functions possible?

2005-05-26 Thread Lloyd Kvam
An alternative to pass is
raise NotImplementedError

This has the advantage/disadvantage of providing a meaningful exception
when you start testing and forget to provide some processing logic for a
stub function.  pass will simply return None when used for a function
body.  I nearly always use the NotImplementedError exception now in my
code as the place holder.


-- 
Lloyd Kvam
Venix Corp

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] pattern matching problem

2005-05-26 Thread Danny Yoo


On 26 May 2005 [EMAIL PROTECTED] wrote:

> I have to write a function that will return the index of a line like this:
>
> gvcdgvcgdvagTVTVTVTVTVTHUXHYGSXUHXSU
>
> where it first becomes capital letters. I've had about a hundred
> different ideas of the best way to do this, but always seem to hit a
> fatal flaw.


Hi Chris,

It might be interesting (or amusing) to bring up one of those
fatally-flawed schemes on the Tutor list, so that we know what not to do.
*grin*

In seriousness, your ideas might not be so bad, and one of us here might
be able to point out a way to correct things and make the approach more
reasonable.  Show us what you've thought of so far, and that'll help
catalize the discussion.



> Any thoughts?

Have you looked into using a regular expression pattern matcher?  A.M.
Kuchling has written a tutorial on regular expressions here:

http://www.amk.ca/python/howto/regex/

Would they be applicable to your program?

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] pattern matching problem

2005-05-26 Thread cgw501
One of the worst I think was doing loads of real spazzy stuff trying to 
split whole files in to lists of letters and use string methods to find the 
first uppercase one.

The re tutorial has sorted it out for me. I figured this was the way to go, 
I just couldn't work out how to get the index value back...but now I can. 
Thanks!

Chris


On May 26 2005, Danny Yoo wrote:

> 
> 
> On 26 May 2005 [EMAIL PROTECTED] wrote:
> 
> > I have to write a function that will return the index of a line like 
> > this:
> >
> > gvcdgvcgdvagTVTVTVTVTVTHUXHYGSXUHXSU
> >
> > where it first becomes capital letters. I've had about a hundred
> > different ideas of the best way to do this, but always seem to hit a
> > fatal flaw.
> 
> 
> Hi Chris,
> 
> It might be interesting (or amusing) to bring up one of those
> fatally-flawed schemes on the Tutor list, so that we know what not to do.
> *grin*
> 
> In seriousness, your ideas might not be so bad, and one of us here might
> be able to point out a way to correct things and make the approach more
> reasonable.  Show us what you've thought of so far, and that'll help
> catalize the discussion.
> 
> 
> 
> > Any thoughts?
> 
> Have you looked into using a regular expression pattern matcher?  A.M.
> Kuchling has written a tutorial on regular expressions here:
> 
> http://www.amk.ca/python/howto/regex/
> 
> Would they be applicable to your program?
> 
> 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Program to lock folders under win32

2005-05-26 Thread Danny Yoo


On Thu, 26 May 2005, Mark Kels wrote:

> I want to make a program to lock folders (so the user can only access
> them if he knows the password) under win32, the only problem is that I
> have no idea how to start or what to do. Do you guys have any ideas of
> how to do it?

Hi Mark,

Out of curiosity, are you looking for something out-of-the-box, or are you
trying to develop a system just to learn more about it?  I ask this
because I'm sure something's out there that does this already, such as the
frontends for the GNU Privacy Guard (GPG) program:

http://www.gnupg.org/(en)/related_software/frontends.html#win


Also, this sounds a bit specific to the particular operating system you're
using, and I'm not sure if we can answer your question well here on Tutor.
You might want to bring up your question on the python-win32 list:

http://mail.python.org/mailman/listinfo/python-win32

Hmmm, but I'll try thinking of random possibilities.  *grin*

You could probably do something that ties into the File Explorer, like the
TortoiseSVN extension, and there's probably hooks that can be manipulated
with Python, but I'm don't have enough experience with the win32api to
know what to look at.  The folks on the win32 list should be better
equipped to help you if you go this route.

One easier alternative might be to use a WebDAV server, since Windows can
talk with WebDAV, and I believe it should be passwordable without much
trouble.  There are WebDAV servers written in Python, so you can probably
take one of those and extend it to your needs.


Best of wishes!

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] pattern matching problem

2005-05-26 Thread Danny Yoo


On 26 May 2005 [EMAIL PROTECTED] wrote:

> One of the worst I think was doing loads of real spazzy stuff trying to
> split whole files in to lists of letters and use string methods to find
> the first uppercase one.

Hi Chris,

An approach like this might work.  Rather than read the whole thing into a
honking big list, though, we can just iterate through it, letter by
letter, by using read(1).  Here's one way we might do it that way:

### Pseudocode ###
currentLine = 1
while True:
   nextChar = someFile.read(1)
   if not nextChar:
   break
   elif isNewline(nextChar):
   currentLine += 1
   elif isUppercased(nextChar):
   break
print currentLine
##

This approach avoids sucking the whole file into memory, and is a
reasonable approach too.


Best of wishes!

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python problem

2005-05-26 Thread Alan G
> i'm trying to write a code that handle errors help
>
> def print_menu(): ...

Personally I try to get the menu function to return the value selected
that way the meniu options and the code to check it is all wrapped
up in the same  place.
But that is only a side issue...

> while menu_choice != 5:
> menu_choice = input("Type in a number (1-5):")
> break
> except(TypeError,NameError):

You need to use try: before an except.

So if you start your while loop body with a try

while menu_choice != 5:
   try:
  menu_choice = input(...)
  if menu_choice == 1:
etc...
   except NameError, TypeError:
 # handle error here
break  # only if you really need to..
   print_menu()

HTH,

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Are Empty "Placeholder" Functions possible?

2005-05-26 Thread Alan G
> Is there a way to create an empty function definition with no lines
of code
> in it? In my coding style I will often do this ( in other
languages ) for
> RAD just to remind myself that I will need to implement the function
later.
>

Use pass

def f(): pass

also works for classes:

class MyClass: pass

I use that a lot.


Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] (no subject). Ord and Chr query

2005-05-26 Thread John Carmona
Pujo, Alan, John and Ben, thanks for the input.

Sorry I should have been clearer with the query perhaps.
Alan I am just going through the book "Learning Python" written by Lutz and 
Ascher, and this is part of one exercise. I am still having problem in 
putting my head around in writting even very simple script. I can see the 
whole picture but have enormous problem in putting it down to paper (or 
screen). Once I see the solution I can really understand. I am actually 
archiving everything that I am learning. Problem is I am not a programmer, I 
don't use Python on a regular/daily basis so of course it seems that the 
learing curve is very steep but I am really enjoying it.

Pujo you were spot on again, thanks. Ben I could not get your script working 
I am probably screwing up somewhere.

John can you see now the reason why I was asking those questions, sorry for 
not being clearer (i have even forgoten to put a line on the subject...

Regards
JC
See you soon


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] (no subject). Ord and Chr query

2005-05-26 Thread Alan G
Hi John,

> Alan I am just going through the book "Learning Python" written by
Lutz and
> Ascher, and this is part of one exercise.

I suspected it might be something you were doing just to explore.
Lutz and Ascher is a great book but they do assume some knowledge
of programming and computer concepts.

> putting my head around in writting even very simple script. I can
see the
> whole picture but have enormous problem in putting it down to paper

Learning to think in the minute level of detail that a computer
'thinks' is half the battle of programming. Humans are intelligent
but slow, computers are stupid but fast. You have to break every
action down to the simplest level and get the sequence just right.
But once you do get this stupid box responding to your every whim
then it is a grat buzz!

Alan G.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] better resolution on time.sleep()?

2005-05-26 Thread Alan G

> Rumor has it that Alan G may have mentioned these words:
>
> >Consuming 100% CPU won't make much difference, the CPU is running
all
> >the time anyway (when its not asleep).
>
> Not mine... my laptop runs a Crusoe processor, and it can
automatically
> scale itself from 300Mhz to 933Mhz. It's called "LongRun"
technology,

Ah yes indeed, I confess I forgot about that little trick.

But the real point I was trying to make was that the CPU itself is not
usually the biggest drain on battery compared to HD and modems, WiFi,
BlueTooth etc.

If I activate my PCMCIA(??) modem my laptop life drops from 3-4 hours
down to 45 minutes or so! And leaving my WiFi connection enabled on
a train means I pick up any other WiFi enabled laptop and it cuts
battery life by an hour compared to when I disable the WiFi...

Alan G.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] pattern matching problem

2005-05-26 Thread Alan G
> I have to write a function that will return the index of a line like
this:
>
> gvcdgvcgdvagTVTVTVTVTVTHUXHYGSXUHXSU
>
> where it first becomes capital letters.

I'd go with a regex search for that one...

Alan g.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Program to lock folders under win32

2005-05-26 Thread jfouhy
Quoting Mark Kels <[EMAIL PROTECTED]>:

> I want to make a program to lock folders (so the user can only access
> them if he knows the password) under win32, the only problem is that I
> have no idea how to start or what to do. Do you guys have any ideas of
> how to do it?

Is your program going to be running all the time?

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65203 is a recipe
showing how to lock files on POSIX and Win32.  So possibly you could do
something like that, where your program acquires locks on all the files and then
releases the lock when given the password...

-- 
John.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] (no subject). Ord and Chr query

2005-05-26 Thread Kent Johnson
John Carmona wrote:
> Sorry I should have been clearer with the query perhaps.
> Alan I am just going through the book "Learning Python" written by Lutz and 
> Ascher, and this is part of one exercise. I am still having problem in 
> putting my head around in writting even very simple script. I can see the 
> whole picture but have enormous problem in putting it down to paper (or 
> screen). Once I see the solution I can really understand. I am actually 
> archiving everything that I am learning. Problem is I am not a programmer, I 
> don't use Python on a regular/daily basis so of course it seems that the 
> learing curve is very steep but I am really enjoying it.

There are many Python learning resources for non-programmers, you might want to 
look at some of 
them. This page has a list:
http://wiki.python.org/moin/BeginnersGuide/NonProgrammers

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] (no subject). Ord and Chr query

2005-05-26 Thread Tim Johnson
* Kent Johnson <[EMAIL PROTECTED]> [050526 14:16]:
> John Carmona wrote:
> > Sorry I should have been clearer with the query perhaps.
> > Alan I am just going through the book "Learning Python" written by Lutz and 
> > Ascher, and this is part of one exercise. I am still having problem in 
> > putting my head around in writting even very simple script. I can see the 
> > whole picture but have enormous problem in putting it down to paper (or 
> > screen). Once I see the solution I can really understand. I am actually 
> > archiving everything that I am learning. Problem is I am not a programmer, 
> > I 
> > don't use Python on a regular/daily basis so of course it seems that the 
> > learing curve is very steep but I am really enjoying it.
> 
> There are many Python learning resources for non-programmers, you might want 
> to look at some of 
> them. This page has a list:
> http://wiki.python.org/moin/BeginnersGuide/NonProgrammers
 
  The Lutz book is a good one, but when I set up an online
  curriculum for programming for a local school district, I 
  chose python for the second semester and the book that we
  used was "Learn to Program Using Python", by Alan Gauld.

  I'm going to recommend it, even tho' many features have been
  added to python since (2001), it does a good job on the
  basics. 
 --
 tj
  

-- 
Tim Johnson <[EMAIL PROTECTED]>
  http://www.alaska-internet-solutions.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor