Re: [Tutor] string conversion according to the terminal

2010-08-13 Thread Alan Gauld

"ANKUR AGGARWAL"  wrote

Hey- suppose we have a file name-"my file number"
if we want to execute it into the terminal it would be like - my\ 
file\

number


It depends what  you are trying to do.

so wondering is there any one in the python that change the enter 
string

into the terminal string one- like if user enter the file name with
path- "my file number". i want to automatically convert it into "my\ 
file\ number"


If you use raw_input to capture the filename then the escaping will be 
done

for you when you come to use it.


s = raw_input('?')

?name with spaces

s

'name with spaces'

f = open(s,'w')
f.write('hello world\n')
f.close()
f = open(s)
f.read()

'hello world\n'

f.close()
^Z



C:\Documents and Settings\Alan Gauld>dir n*
Volume in drive C is SYSTEM
Volume Serial Number is 7438-BB1D

Directory of C:\Documents and Settings\Alan Gauld

13/08/2010  09:0313 name with spaces


So are you sure you need to do this?

--
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] is it possible to call a setter property during classinstantiation?

2010-08-13 Thread Alan Gauld

"Serdar Tumgoren"  wrote

Does anyone know if it's possible to call a property setter inside 
of a
class's init method?  Below is a code sample of what I'm trying to 
do.


Yes and that bit has been answered.
But...


I have a class with an init method that is getting bloated with
error-checking guard clauses.


Thats very strange. We don't usually have to check types in Python,
it is a dynamic language and we can use duck-typing and exceptions
so we don't usually care too much about types. Are you sure you
really need all that type checking code? Or are you simply
carrying bad habits over from statically typed programming
experience?

I can't tell the context so it may be that you really do need to check
a lot of types, but it is quite unusual and does usually lead to a lot 
of

extra work. Python is not Java or C++ etc...

Just a thought,

--
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] Callbacks and exception handling

2010-08-13 Thread Alan Gauld


"Pete"  wrote


One thing though - I noticed that when an exception is raised in the
callback function, that exception doesn't actually "show up" in the
calling program.


Yes it does and Steven has answered that so maybe you are clear now.

But just to clarify, when you set a callback there is no guarantee 
that
the callback gets used immediately so the exception will not 
necessarily
appear in the setting function, it will appear in the calling 
function.



cb = []

def setcb(f):
   cb.append(f)

def fireworks():
   try:
  for f in cb: print f()# call them all now
  except:
   print 'a callback failed'

def myFunc()
setcb('lambda : len(cb))
setcb('lambda : 42)
setcb('lambda : 66)
setcb('lambda : 12/0)   #oops!

myFunc()
fireworks()

Here any exceptions in the callback functions won't appear until
the fireworks function, they will not show up in myFunc.

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] elif statement

2010-08-13 Thread Alan Gauld


"Sudarshana Banerjee"  wrote


Could you take a look at this please:

x=3
if x==0:

print "x is 0"


elif x&1==1:

SyntaxError: invalid syntax

See, the moment I am pressing Enter the >>> is coming.. not ...


IDLE doesn't give you a ... prompt it gives you spaces.
So you mistake is that ypu are hitting Enter after your print line. 
It needs to look like:



x=3
if x==0:

   print "x is 0"
elif x&1==1:
   print

Which looks horrible and I wish Idle were fixed so it didn't do this.
I keep meaning to try and find a way to patch it myself!


Essentially you need to ignore the >>> offset in the if line.

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


[Tutor] Learning Python ...Books

2010-08-13 Thread Stephen Farthing
Hi everyone,

I am going to learn both Python and Ruby so I can see what suits me best. My
ultimate aim is to do some open source cross platform development for my ham
radio and robotics hobbies. One of the key requirements is to be able to
control hardware, via USB, using a GUI.

 I am an old school programmer - PL/1, Cobol, RTL/2, Algol 60 and I
currently use C to program microcontrollers. I like Jackson Structured
Programming (the other Michael Jackson) and am not up to speed with Object
Orientation.  I also learn stuff best from books.

What I was wondering is if there is a Python equivalent to "The C
Programming language" by Kernighan and Richie which is the best text on
programming i have ever read.

Thanks in advance,

Steve

-- 
It is vain to do with more that which can be done with less.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Learning Python ...Books

2010-08-13 Thread Joel Goldstick
I like "Learning Python" (O'reilly)

Its thicker, but excellent.  There isn't really an equivalent to K & R.
But, you should go to python.org and read the docs.  They are very good

On Fri, Aug 13, 2010 at 6:22 AM, Stephen Farthing wrote:

> Hi everyone,
>
> I am going to learn both Python and Ruby so I can see what suits me best.
> My ultimate aim is to do some open source cross platform development for my
> ham radio and robotics hobbies. One of the key requirements is to be able to
> control hardware, via USB, using a GUI.
>
>  I am an old school programmer - PL/1, Cobol, RTL/2, Algol 60 and I
> currently use C to program microcontrollers. I like Jackson Structured
> Programming (the other Michael Jackson) and am not up to speed with Object
> Orientation.  I also learn stuff best from books.
>
> What I was wondering is if there is a Python equivalent to "The C
> Programming language" by Kernighan and Richie which is the best text on
> programming i have ever read.
>
> Thanks in advance,
>
> Steve
>
> --
> It is vain to do with more that which can be done with less.
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


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


[Tutor] Learning Python ...Books

2010-08-13 Thread Bill Allen
On Fri, Aug 13, 2010 at 5:22 AM, Stephen Farthing wrote:

>
> What I was wondering is if there is a Python equivalent to "The C
> Programming language" by Kernighan and Richie which is the best text on
> programming i have ever read.
>
> Thanks in advance,
>
> Steve
>

Steve,

There are several books out there, some for Python 2.x, some for Python
3.x.   None that are quite like K&R in my opinion.
However, here are some resources that may be useful for you, web based but
some providing PDF download so you can build a book if you like.

A Byte of Python 

Dive into Python v.2    Dive into
Python v.3

The Learning to Program Website (teaches using Python)

How to Think Like a Computer Scientist (also teaches using
Python)

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


Re: [Tutor] Learning Python ...Books

2010-08-13 Thread Bill Allen
On Fri, Aug 13, 2010 at 7:29 AM, Bill Allen  wrote:

>
>
> There are several books out there, some for Python 2.x, some for Python
> 3.x.   None that are quite like K&R in my opinion.
> However, here are some resources that may be useful for you, web based but
> some providing PDF download so you can build a book if you like.
>
> A Byte of Python 
>
> Dive into Python v.2    Dive into Python
> v.3 
>
> The Learning to Program Website (teaches using 
> Python)
>
> How to Think Like a Computer Scientist (also teaches using 
> Python)
>
> Bill
>
>
> I also forgot to mention that the Python book that I am reading right now
is Programming in Python 3 by Mark
Summerfield


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


Re: [Tutor] is it possible to call a setter property during classinstantiation?

2010-08-13 Thread Serdar Tumgoren
>
>
>
>  I have a class with an init method that is getting bloated with
>> error-checking guard clauses.
>>
>
> Thats very strange. We don't usually have to check types in Python,
> it is a dynamic language and we can use duck-typing and exceptions
> so we don't usually care too much about types. Are you sure you
> really need all that type checking code?


Hmm...for my scenario I thought I needed to bend that rule, but if I explain
the use case perhaps you can offer alternatives. Much of my error-checking
aims to ensure that a class is "fed" the correct type of data.  For
instance, I included a type check for a string to make sure that a future
programmer (or myself) wouldn't try to pass in a list of lines after a text
has been split. Other code internal to the Question class depends on the
fact that the incoming data arrives in the form of a single string. If the
wrong data type is fed, then I have a custom exception that gets raised to
specify the problem. I guess the program would just crash with a TypeError
if a list of lines was fed rather than a string, so even without the guard
clause the processing would not take place.

Is it better in such cases to just let the program die a natural death,
without cluttering things up with such anticipatory guard clauses? I have to
admit, it certainly does clutter up the code and seems to be leading to a
lot of additional unit testing overhead. Is there a better way to approach
these issues?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Learning Python ...Books

2010-08-13 Thread Wayne Werner
On Fri, Aug 13, 2010 at 5:22 AM, Stephen Farthing wrote:

> Hi everyone,
>
> I am going to learn both Python and Ruby so I can see what suits me best.
> My ultimate aim is to do some open source cross platform development for my
> ham radio and robotics hobbies. One of the key requirements is to be able to
> control hardware, via USB, using a GUI.
>

Using the pyserial module: http://pyserial.sourceforge.net/, and depending
on how advanced you want to get, Tkinter, pyGTK, wxPython, or PyQT (as the
main choices) are great ways to do such a thing. I've used pyserial with my
Arduino under Ubuntu and it worked great.


> What I was wondering is if there is a Python equivalent to "The C
> Programming language" by Kernighan and Richie which is the best text on
> programming i have ever read.
>

 This post:
http://stackoverflow.com/questions/947942/advanced-python-programming-book-like-effective-c
has
several books. I've heard that Wesley Chun's book (who also happens to
contribute here every so often ;) Core Python Programming is a great one.

I'm a big tutorial reader - reading tutorials with plenty of examples is how
I learn best - I've done a lot of eBooks. One great resource that
surprisingly few people mention (mainly because it's "for kids" - that just
means it's a lot more entertaining :) is Snake Wrangling for Kids:
http://www.briggs.net.nz/log/writing/snake-wrangling-for-kids/

It covers all the basic stuff that's easy to blitz through if you're
familiar with programming, and then towards the end it gets a little into
programming with Tkinter.

Anyhow, HTH, good luck, and welcome!
If you get stuck, there are plenty of people here willing to help (which you
may find par for the course when dealing with Pythonistas).
-Wayne
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Lost in the control flow

2010-08-13 Thread Adam Bark

On 11/08/10 18:14, Eduardo Vieira wrote:

On Tue, Aug 10, 2010 at 1:56 PM, Adam Bark  wrote:

   

The problem is you don't call make_dict unless there's a "FUEL SURCHARGE" or
multiple pins. Also you don't add the first pin to mydict["tracking"] unless
there's a "FUEL SURCHARGE".

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

 

Hi, I made these changes, got rid of the functions, and changed some logic:
here is the link: http://pastebin.com/F19vKUjr

Now mydict will be changing, of course with every loop, as the output below:
{'company': 'CITY SIGNS', 'tracking': ['600775301143'], 'id': '1'}
{'city': 'MEDICINE HAT', 'company': 'CITY SIGNS', 'tracking':
['600775301143'], 'id': '1', 'prov': 'AB'}
{'city': 'MEDICINE HAT', 'company': 'TRIMLINE', 'tracking':
['600775301150'], 'id': '2', 'prov': 'AB'}
{'city': 'ROCKY MOUNTAIN HOUSE', 'company': 'TRIMLINE', 'tracking':
['600775301150'], 'id': '2', 'prov': 'AB'}
{'city': 'ROCKY MOUNTAIN HOUSE', 'company': 'TS SIGNS PRINTING&
PROMO', 'tracking': ['600775301168'], 'id': '3', 'prov': 'AB'}
{'city': 'FORT MCMURRAY', 'company': 'TS SIGNS PRINTING&  PROMO',
'tracking': ['600775301168'], 'id': '3', 'prov': 'AB'}
{'city': 'FORT MCMURRAY', 'company': 'TS SIGNS PRINTING&  PROMO',
'tracking': ['600775301168', '600775301168'], 'id': '3', 'prov': 'AB'}
{'city': 'FORT MCMURRAY', 'company': 'TS SIGNS PRINTING&  PROMO',
'tracking': ['600775301168', '600775301168', '600775301176'], 'id':
'3', 'prov': 'AB'}
{'city': 'FORT MCMURRAY', 'company': 'TS SIGNS PRINTING&  PROMO',
'tracking': ['600775301168', '600775301168', '600775301176',
'600775301184'], 'id': '3', 'prov': 'AB'}
{'city': 'FORT MCMURRAY', 'company': 'TS SIGNS PRINTING&  PROMO',
'tracking': ['600775301168', '600775301168', '600775301176',
'600775301184', '600775301192'], 'id': '3', 'prov': 'AB'}
{'city': 'FORT MCMURRAY', 'company': 'TS SIGNS PRINTING&  PROMO',
'tracking': ['600775301168', '600775301168', '600775301176',
'600775301184', '600775301192', '600775301200'], 'id': '3', 'prov':
'AB'}
so I appended everything to a bigdata list and used it to update the
dictionary data_dict

I can't understand why I get only one value:
{'18': {'city': 'ESTEVAN',
 'company': 'BRAKE&  DRIVE SYSTEMS',
 'id': '18',
 'prov': 'SK',
 'tracking': ['600775301515', '600775301515', '600775301523']}}

Regards,

Eduardo
   
It looks to me like you keep overwriting the previous data, you keep 
using mydict. Doing an append does not copy the dictionary it just 
copies a reference to the underlying data structure.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] elif statement

2010-08-13 Thread Sudarshana Banerjee
Hi Alan: Yay! I pressed Enter after the print, and then delete. Which
brought the cursor back to the starting position at the beginning of the
line, and no more elif syntax errors. This is good to know. Thank you very
much.

I also took the liberty of checking out your computing website. It is really
helpful; and I have added it to my bookmarks.

Thanks a ton!

Sudarshana.

On Fri, Aug 13, 2010 at 1:27 AM, Alan Gauld wrote:

>
> "Sudarshana Banerjee"  wrote
>
>
>  Could you take a look at this please:
>>
>>> x=3
> if x==0:
>
 print "x is 0"
>>
>>  elif x&1==1:
>
 SyntaxError: invalid syntax
>>
>> See, the moment I am pressing Enter the >>> is coming.. not ...
>>
>
> IDLE doesn't give you a ... prompt it gives you spaces.
> So you mistake is that ypu are hitting Enter after your print line. It
> needs to look like:
>
>
>  x=3
 if x==0:

>>>   print "x is 0"
> elif x&1==1:
>   print
>
> Which looks horrible and I wish Idle were fixed so it didn't do this.
> I keep meaning to try and find a way to patch it myself!
>
>
> Essentially you need to ignore the >>> offset in the if line.
>
> 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
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor