Re: [Tutor] Class-based generator

2013-02-18 Thread Oscar Benjamin
On 18 February 2013 07:36, Michael O'Leary  wrote:
> I wrote some code to create tasks to be run in a queue based system last
> week. It consisted of a big monolithic function that consisted of two parts:
> 1) read data from a file and create dictionaries and lists to iterate
> through
> 2) iterate through the lists creating a job data file and a task for the
> queue one at a time until all of the data is dealt with
>
> My boss reviewed my code and said that it would be more reusable and
> Pythonic if I refactored it as a generator that created job data files and
> iterated by calling the generator and putting a task on the queue for each
> job data file that was obtained.
>
> This made sense to me, and since the code does a bunch of conversion of the
> data in the input file(s) to make it easier and faster to iterate through
> the data, I decided to create a class for the generator and put that
> conversion code into its __init__ function. So the class looked like this:

It's not a "generator" if you create a class for it. Your class is
(trying to be) an iterator.

> class JobFileGenerator:
> def __init__(self, filedata, output_file_prefix, job_size):
> 
>
> def next(self):
> while :
> 

next() should return a single item not a generator that yields items.
If you perhaps rename the next function as __iter__ then it will be a
proper iterator.

I suspect however that your boss just wants you to write a single
generator function rather than an iterator class. For example:

def generate_jobs():

while :
yield 

for job in generate_jobs():

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


Re: [Tutor] Class-based generator

2013-02-18 Thread Peter Otten
Michael O'Leary wrote:

> I wrote some code to create tasks to be run in a queue based system last
> week. It consisted of a big monolithic function that consisted of two
> parts: 1) read data from a file and create dictionaries and lists to
> iterate through
> 2) iterate through the lists creating a job data file and a task for the
> queue one at a time until all of the data is dealt with
> 
> My boss reviewed my code and said that it would be more reusable and
> Pythonic if I refactored it as a generator that created job data files and
> iterated by calling the generator and putting a task on the queue for each
> job data file that was obtained.
> 
> This made sense to me, and since the code does a bunch of conversion of
> the data in the input file(s) to make it easier and faster to iterate
> through the data, I decided to create a class for the generator and put
> that conversion code into its __init__ function. So the class looked like
> this:
> 
> class JobFileGenerator:
> def __init__(self, filedata, output_file_prefix, job_size):
> 
> 
> def next(self):
> while :
> 
> 
> The problem is that the generator object is not created until you call
> next(), so the calling code has to look like this:
> 
> gen = JobFileGenerator(data, "output_", 20).next()
> for datafile in gen.next():
> 
> 
> This code works OK, but I don't like that it needs to call next() once to
> get a generator and then call next() again repeatedly to get the data for
> the jobs. If I were to write this without a class as a single generator
> function, it would not have to do this, but it would have the monolithic
> structure that my boss objected to.
> 
> Would it work to do this:
> 
> for datafile in JobFileGenerator(data, "output_", 20).next():
> 
> 
> or would that cause the JobFileGenerator's __init__ function to be called
> more than once? Are there examples I could look at of generator functions
> defined on classes similar to this, or is it considered a bad idea to mix
> the two paradigms?
> Thanks,
> Mike


You are abusing the next method; it is called once to build a generator. The 
convention for that is to use either a descriptive name (jobs() or somesuch) 
or __iter__():

class JobFile:
def __init__(self, filedata, output_file_prefix, job_size):

def __iter__(self):
while :



for job in JobFile(data, "output_", 20):


Here the generator is created by the implicit call to JobFile.__iter__() at 
the start of the for loop. Subsequent iterations call next() on the 
generator returned by that call.

If you want the class itself to generate items you need a different 
approach:

class JobFileIter:
def __init__(self, filedata, output_file_prefix, job_size):

self._done = False
def __iter__(self):
return self
def next(self):
if self._done or :
self._done = True
raise StopIteration
return 


for job in JobFileIter(data, "output_", 20):


Here __iter__() returns the JobFileIter instance, so for every iteration of 
the for loop JobFileIter.next() will be called -- until a StopIteration is 
raised.

That said, it is often sufficient to refactor complex code into a few 
dedicated functions -- Python is not Java, after all.

PS I'm assuming Python 2 -- for Python 3 the next() method must be replaced 
by __next__().

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


Re: [Tutor] following on

2013-02-18 Thread Alan Gauld

On 18/02/13 17:30, Matthew Ngaha wrote:


i can attempt to use these libraries/frameworks/modules provided but
how can i build or get better at creating/building my own tools?


practice.
Just build a lot of stuff. Its the only way.


and i did that!" or hey i built my own engine, my own language!!


That doesn't happen all that often, usually people just extend
the existing language.


etc... i become absolutely lost and thought i would like to do that!


What? Get absolutely lost?! :-)


wondering if anyone could point me out to some good books or online
tutorials that can teach me how to create my own modules, libraries
that others could use.


In python this is almost trivial. Just create a Python script that has 
functions or classes in it. That's it. Make it available for import by 
putting it somewhere in your import path. job done.


There is a little bit more to creating a package rather than a module 
but its easy once you've  got the hang  of making modules.



This is what is called software engineering


No, its a part of software engineering. Software engineering is an 
attempt to apply traditional engineering discipline to software 
development in the hope that it will make the development process more 
predictable, reliable and consistent. So far it's pretty much failed 
because most "software engineers" don't really like doing hard math when 
they could be writing code, and doing hard math before building anything 
is pretty much what makes it engineering... (This is why some US states 
refuse to acknowledge software engineering as 'real' engineering.)


But it also includes things like creating modular designs and performing 
comprehensive tests and documenting the design in standard formats etc. 
Those aspects have caught on in varying degrees.




create custom instances and have these instances behave like normal
python objects.


Thats good.



I want my program to basically do EVETYTHING that
python already does but in my own way. On the objects i'd like to
perform stuff like __add__ or __cmp__ on them, but without actually
using __add__ or __cmp__, get it?.


No, that's probably not good.


I'll attempt my own built-ins also,


And that's almost certainly even worse.



like summed() instead of sum()... is this a constructive exercise or a
waste of time?


Its a waste of time given where you are on your learning journey. Maybe 
some day you will have a valid need to create your own language but this 
is almost certainly not it!



understanding of how everything works.


Use it., Experiment with it. Break it.
Thats the best way. Read the source code its all available in
Python or C.


like how can i make my own custom container and not have it

> subclass list(), and still act like a list()?

That's a more sensible option especially if you make it a type of 
container that Python doesn't already have. Maybe a bag or a stack

or even a circular list. But there is no shame in inheriting from
list if that makes sense. That would be good software engineering - 
don't reinvent the wheel!



would i need to use stuff like __next__ and __iter__ or could
i create my own that acts the same way


You need to implement all the special methods that give Python objects 
their built-in behaviour. Or at least all the ones that matter, or make 
sense, for your data type.



ill drop the project if its a bad idea but if it isnt, its simply a
work in progress(barely started).


I'd forget trying to rewrite Python in python. Even as a learning 
project it has limited value and as a practical proposition no value at 
all. Better to build something meaningful that pushes the envelope with 
python.



so please point me out to some good books or online tutorials that can
teach me how to create my own software that others could use. Also
what is the correct term for this type of programming?


Just programming. Its what we should all be doing all the time!


have any personal advice you could offer me, please do.. Thanks


When building  reusable code start from the perspective of the user. 
Write some tests that use the new creation even though it doesn't exist 
yet. Make the code as easy to use as possible. Once you know how to use 
it, start building the thing so that it can be used the way you want it 
to work. The hard effort should be in building the reusable code not in 
using it.


--
Alan G
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] if/else option for making a choice

2013-02-18 Thread Niclas Rautenhaus
Hello folks,

 

I would be very pleased if someone is able to help me.

I wrote a small programm, to calculate the grand total for a car.

A basic price can be entered. Given that basic price, a tax peercentage is
calculated and added tot he grand total.


But unfortunately I am stuck with giving the user the choice to select an
additional option!
The extra items have got set values, but at the moment they all get
summarized, but i want to choose.

 

AFAIK I already tried:

 

Print  raw_input ((“Would you like to have leather?“) à then yes or no

If raw_input == yes

  Print Leather (Leather) ß displaying the variables value

 

Plus adding the item at the end to concatenate it with the basic value.

 

I hope it is clear where my problem is.

 

Regards,

 

Niclas

 

My code I already wrote:

-

 

 

#Car price calculator

 

# Set variables for additional items

Leather = (500)

int (Leather)

Clima = (1500)

int (Clima)

Hifi = (250)

int (Hifi)

Electrics = (750)

int (Electrics)

Comfort = (2500)

int (Comfort)

 

 

print "\t\t\tWelcome to the car price calculator!"

print "\t\t\tCredits belong to Niclas Rautenhaus"

print "\n\nPlease enter the basic price: "

basic = int (raw_input())

# Tax is a percentage of the basic car price

Tax = basic * 5 / 100

int (Tax)

 

print "\nAdded items:"

# Here the user should have the possibility to pick either yes or no

print "\n\nLeather",(Leather)

print "Clima",(Clima)

print "Hifi", (Hifi)

print "Electrics", (Electrics)

print "Comfort", (Comfort)

print "Tax", (Tax)

 

# Last step: the picked items shall be summarized

 

print "\n\nTotal grand:", basic + Leather + Clima + Hifi \

  + Electrics + Comfort + Tax

 

raw_input ("\nPress the enter key to exit!")

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


Re: [Tutor] following on

2013-02-18 Thread Matthew Ngaha
>> understanding of how everything works.
>
> Use it., Experiment with it. Break it.
> Thats the best way. Read the source code its all available in
> Python or C.
>

Hey can you please tell me which source code youre referring too? The
initial files that come with Python? also the C code, where can i
locate this? is C something worth learning? why is Python code
available in C?

>
>> like how can i make my own custom container and not have it
>> subclass list(), and still act like a list()?
>
> That's a more sensible option especially if you make it a type of container
> that Python doesn't already have. Maybe a bag or a stack
> or even a circular list. But there is no shame in inheriting from
> list if that makes sense. That would be good software engineering - don't
> reinvent the wheel!
> When building  reusable code start from the perspective of the user. Write

yes i will try not to reinvent the wheel, a lot of tutorials ive read
seem to always point this out. Just out of curiousity what is a bag,
stack or circular list? what is needed to create something like this?
i sort of heard about a stack its a C/C++ thing i think?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] following on

2013-02-18 Thread Steve Willoughby
On Mon, Feb 18, 2013 at 07:01:02PM +, Matthew Ngaha wrote:
> >> understanding of how everything works.
> >
> > Use it., Experiment with it. Break it.
> > Thats the best way. Read the source code its all available in
> > Python or C.
> >
> 
> Hey can you please tell me which source code youre referring too? The
> initial files that come with Python? also the C code, where can i
> locate this? is C something worth learning? why is Python code
> available in C?

C is definitely worth learning.  So is LISP.  And a handful of other
languages we could enumerate.  It's worth learning if it adds something
useful to your understanding of how computers operate on the instructions you
give them, or if it changes your approach to creating software in a fundamental
way.  Actually using those languages in daily life is not the point.

C, however, is still very useful in a number of situations.  Python programmers
can get leverage from its strengths by--for example--writing 
performance-critical
modules in C and then calling them from their Python programs.

Python itself (well, the standard implementation of it anyway) is written in C.
It has to be written in something that ultimately compiles down to the machine
language which the computer actually uses.  That C code interprets your Python
code so you have a much nicer, high-level programming environment to work with.
But the computer itself doesn't directly understand Python.

> yes i will try not to reinvent the wheel, a lot of tutorials ive read
> seem to always point this out. Just out of curiousity what is a bag,
> stack or circular list? what is needed to create something like this?
> i sort of heard about a stack its a C/C++ thing i think?

None of these are C/C++ things.  They are basic building-blocks of Computer
Science and data structures you'll use regardless of language.  I'd really
recommend investing some time reading up on these and other fundamental
data structures.
-- 
Steve Willoughby|  Using billion-dollar satellites
st...@alchemy.com   |  to hunt for Tupperware.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] if/else option for making a choice

2013-02-18 Thread Steve Willoughby
On Mon, Feb 18, 2013 at 07:22:54PM +0100, Niclas Rautenhaus wrote:
> Hello folks,
> I hope it is clear where my problem is.

Not completely, but let's take a look at what you have so far.

> # Set variables for additional items
> Leather = (500)
> int (Leather)
> Clima = (1500)
> int (Clima)
...

You don't need to put parens around the values, but more importantly
note that the "int" lines don't do anything here.  When you say:

> Hifi = (250)
> int (Hifi)

You set Hifi to the value 250 in the first line, which is fine, but
then you call int() to turn the integer 250 into the integer 250, and
then promptly discard that value.  If you had, for example, a string
"250" and wanted to turn it into an integer, you could do something
like

Hifi = int("250")

But simply saying

int(250)

accomplishes nothing.  Do you see why?

> print "\n\nPlease enter the basic price: "
> 
> basic = int (raw_input())

I'm curious why the price of the car has to be an integer.
Couldn't it be something like 15999.95?

> # Tax is a percentage of the basic car price
> 
> Tax = basic * 5 / 100

You would be better off using floats here if you want a real number
as the answer.  You're working in integers.

> int (Tax)

Or maybe you really want integers only to keep the numbers easier
or something.  Fair enough, but again this line does absolutely
nothing.  You truncate Tax to an integer but then discard that integer
value as soon as it's created.

> print "\nAdded items:"
> # Here the user should have the possibility to pick either yes or no
> print "\n\nLeather",(Leather)

The parentheses are unnecessary here.

To have them choose, you need some kind of "if" statement.  Maybe something
like

choice = raw_input("Would you like windows?")
if choice == 'yes':
windows = 150

There are certainly more sophisticated ways to do this as well, when you get
past the basics of how conditionals (if statements) work.  For example, making
a function that asks each question and handles all the different ways the user
might answer other than typing precisely "yes".  Or putting the choices in a
list instead of repeating the choice-asking code over and over.

-- 
Steve Willoughby|  Using billion-dollar satellites
st...@alchemy.com   |  to hunt for Tupperware.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] following on

2013-02-18 Thread Alan Gauld

On 18/02/13 19:01, Matthew Ngaha wrote:


initial files that come with Python? also the C code, where can i
locate this?


Look in the python.org download page and you will find a link to the 
source for that release. For 3.3 it's here:


http://www.python.org/download/releases/3.3.0/

Look at the first 3 bullets item under the Download header.


is C something worth learning? why is Python code
available in C?


Because the core Python language is written in C. And many of the 
library modules have been written in C for performance reasons.

Its worth getting a basic understanding of C, enough to read
it if not enough to write it(much harder).


seem to always point this out. Just out of curiousity what is a bag,
stack or circular list?


Use Wikipedia, it will explain what these standard computer science 
containers are and how they work. Wikipedia is a fantastic learning tool 
for would be programmers.



what is needed to create something like this?


Any general purpose programming language, such as Python.


i sort of heard about a stack its a C/C++ thing i think?


No, its a computing thing. It can be done in any common
programming language.

--
Alan G
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] if/else option for making a choice

2013-02-18 Thread Alan Gauld

On 18/02/13 18:22, Niclas Rautenhaus wrote:


But unfortunately I am stuck with giving the user the choice to select
an additional option!
The extra items have got set values, but at the moment they all get
summarized, but i want to choose.

AFAIK I already tried:

Print  raw_input ((“Would you like to have leather?“) à then yes or no

If raw_input == yes

   Print Leather (Leather) ß displaying the variables value



This isn't Python code and nothing like it appears in the code you 
posted below. There are several problems with the code above:


- print does not start with uppercase P.
- You have two (( at the raw_input
- if starts lower case i
- raw_input is a function so you shouldn't compare it to yes
- you don't assign the input value to a variable(although you
  do in the code below so you know you should...)
- you compare to yes but it probably should be "yes" - a string.
- The Print Leather line makes no sense at all.

If you are going to post code post real code.
Or else make it clear its pseudo code.

Now on to the code you did post...


I hope it is clear where my problem is.


Not really.
Did you try running it? Did you get an error?
If so what?


#Car price calculator

# Set variables for additional items
Leather = (500)


Why in ()? you don;t need them


int (Leather)


And this does nothing - its already an int - and you ignore the result.


Clima = (1500)
int (Clima)
Hifi = (250)
int (Hifi)
Electrics = (750)
int (Electrics)
Comfort = (2500)
int (Comfort)


See above for all of these.


print "\t\t\tWelcome to the car price calculator!"
print "\t\t\tCredits belong to Niclas Rautenhaus"
print "\n\nPlease enter the basic price: "
basic = int (raw_input())


Now the last line is what your code above should have looked like.


# Tax is a percentage of the basic car price
Tax = basic * 5 / 100
int (Tax)


The int() gets thrown away but thats probably good because you
don't want to lose the fractional part of the tax value, which
is what int() would do.


print "\nAdded items:"
# Here the user should have the possibility to pick either yes or no
print "\n\nLeather",(Leather)
print "Clima",(Clima)
print "Hifi", (Hifi)
print "Electrics", (Electrics)

> print "Comfort", (Comfort)
> print "Tax", (Tax)

Again you don't need those parentheses


# Last step: the picked items shall be summarized
print "\n\nTotal grand:", basic + Leather + Clima + Hifi \
   + Electrics + Comfort + Tax

raw_input ("\nPress the enter key to exit!")


Personally I'd have split it at the comma:

print "\n\nTotal grand:", \
   basic + Leather + Clima + Hifi + Electrics + Comfort + Tax

But that's a minor quibble.

Ironically I might also have used parentheses to group the additions, 
like so:


print "\n\nTotal grand:", (basic + Leather +
   Clima + Hifi +
   Electrics + Comfort +
   Tax)

Or perhaps better still put them in a list and used sum()

items = [basic, Leather, Clima, Hifi, Electrics, Comfort, Tax]
print "\n\nTotal grand:", sum(items)

HTH

--
Alan G
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] Fw: if/else option for making a choice

2013-02-18 Thread ALAN GAULD
forwarding to the group.
please use reply all to respond to list messages.
 
Alan Gauld
Author of the Learn To Program website
http://www.alan-g.me.uk/


- Forwarded Message -
>From: Niclas Rautenhaus 
>To: 'Alan Gauld'  
>Sent: Monday, 18 February 2013, 21:09
>Subject: Re: [Tutor] if/else option for making a choice
> 
>Thanks for your corrections :)
>
>Well, first at all I sticked to my book, minor mistakes are my bad, I tried
>to write it on my own.
>
>>This isn't Python code and nothing like it appears in the code you posted
>below. There are several problems with the code above:
>
>
>The typos are a result of my e-mail program, I do know, that these commands
>do not start with a capital letter.
>
>>- You have two (( at the raw_input
>Thanks.
>
>> raw_input is a function so you shouldn't compare it to yes
>That is not the point. I do know so. The result oft he user input (yes or
>no) shall trigger a 'yes' or a 'no" event.
>
>> you don't assign the input value to a variable(although you
>   do in the code below so you know you should...)
>The input value is clearly working. I can type in different amounts.
>
>> The Print Leather line makes no sense at all.
>The programm should give me a hint that the 'yes' input has been recogniced.
>
>>Did you try running it? Did you get an error?
>I did'nt get any errors. The programm runs fine.
>
>Trying to clarify what I want to do:
>The programm runs fine. The items (leather, hifi and so on) have set values.
>I
>Think of customizing a car for your needs. What do you want, interior, hifi,
>engine; at each step the configurator asks you what you want to pick.
>So to put it in a nutshell, the programm should ask, whether an option is
>chosen, or not.
>
>Each optional feature (the ones with the set values) shall print a line: "Do
>you want this item?" "Yes or no" and demanding an user input.
>Referring to this user input, the programm adds (ore take it easy, prints
>the item) or skips it.
>
>I appreciate your help,
>
>Niclas
>
>
>
>
>-Ursprüngliche Nachricht-
>Von: Tutor [mailto:tutor-bounces+n.rautenhaus=gmx...@python.org] Im Auftrag
>von Alan Gauld
>Gesendet: Montag, 18. Februar 2013 21:08
>An: tutor@python.org
>Betreff: Re: [Tutor] if/else option for making a choice
>
>
>This isn't Python code and nothing like it appears in the code you posted
>below. There are several problems with the code above:
>
>- print does not start with uppercase P.
>- You have two (( at the raw_input
>- if starts lower case i
>- raw_input is a function so you shouldn't compare it to yes
>- you don't assign the input value to a variable(although you
>   do in the code below so you know you should...)
>- you compare to yes but it probably should be "yes" - a string.
>- The Print Leather line makes no sense at all.
>
>If you are going to post code post real code.
>Or else make it clear its pseudo code.
>
>Now on to the code you did post...
>
>> I hope it is clear where my problem is.
>
>Not really.
>Did you try running it? Did you get an error?
>If so what?
>
>> #Car price calculator
>>
>> # Set variables for additional items
>> Leather = (500)
>
>Why in ()? you don;t need them
>
>> int (Leather)
>
>And this does nothing - its already an int - and you ignore the result.
>
>> Clima = (1500)
>> int (Clima)
>> Hifi = (250)
>> int (Hifi)
>> Electrics = (750)
>> int (Electrics)
>> Comfort = (2500)
>> int (Comfort)
>
>See above for all of these.
>
>> print "\t\t\tWelcome to the car price calculator!"
>> print "\t\t\tCredits belong to Niclas Rautenhaus"
>> print "\n\nPlease enter the basic price: "
>> basic = int (raw_input())
>
>Now the last line is what your code above should have looked like.
>
>> # Tax is a percentage of the basic car price Tax = basic * 5 / 100 int 
>> (Tax)
>
>The int() gets thrown away but thats probably good because you don't want to
>lose the fractional part of the tax value, which is what int() would do.
>
>> print "\nAdded items:"
>> # Here the user should have the possibility to pick either yes or no 
>> print "\n\nLeather",(Leather) print "Clima",(Clima) print "Hifi", 
>> (Hifi) print "Electrics", (Electrics)
>> print "Comfort", (Comfort)
>> print "Tax", (Tax)
>
>Again you don't need those parentheses
>
>> # Last step: the picked items shall be summarized print "\n\nTotal 
>> grand:", basic + Leather + Clima + Hifi \
>>        + Electrics + Comfort + Tax
>>
>> raw_input ("\nPress the enter key to exit!")
>
>Personally I'd have split it at the comma:
>
>print "\n\nTotal grand:", \
>        basic + Leather + Clima + Hifi + Electrics + Comfort + Tax
>
>But that's a minor quibble.
>
>Ironically I might also have used parentheses to group the additions, like
>so:
>
>print "\n\nTotal grand:", (basic + Leather +
>                            Clima + Hifi +
>                            Electrics + Comfort +
>                            Tax)
>
>Or perhaps better still put them in a list and used sum()
>
>items = [basic, Leather, Clima, Hifi, Electrics, Comfort, Tax

Re: [Tutor] if/else option for making a choice

2013-02-18 Thread wesley chun
On Mon, Feb 18, 2013 at 10:22 AM, Niclas Rautenhaus wrote:

> Hello folks,
>
> ** **
>
> I would be very pleased if someone is able to help me.
>
> I wrote a small programm, to calculate the grand total for a car.
>
> A basic price can be entered. Given that basic price, a tax peercentage is
> calculated and added tot he grand total.
>
>
> But unfortunately I am stuck with giving the user the choice to select an
> additional option!
> The extra items have got set values, but at the moment they all get
> summarized, but i want to choose.
>
> **
>


greetings Niclas, and welcome to Python! while i'll let the others comment
on your code specifically, i can give some overall suggestions/guidance.

you're trying to create an overall price calculator correct? while it's
straightforward providing a base price, the complexity in your app (and
real life) is the set of options that customers can choose from.

in your situation, i think it would be more "Pythonic" to maintain the
extras as a vector of options and prices. you then loop through those,
prompting the user to enter Yes or No, and add either the cost or zero,
respectively. that will help keep your code less complex as well. you would
just be maintaining a running total until the user is done with all their
selections.

good luck!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"A computer never does what you want... only what you tell it."
+wesley chun : wescpy at gmail : @wescpy
Python training & consulting : http://CyberwebConsulting.com
"Core Python" books : http://CorePython.com
Python blog: http://wescpy.blogspot.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] if/else option for making a choice

2013-02-18 Thread akleider
> On Mon, Feb 18, 2013 at 10:22 AM, Niclas Rautenhaus
> wrote:
>
>> Hello folks,
>>
>> ** **
>>
>> I would be very pleased if someone is able to help me.
>>
>> I wrote a small programm, to calculate the grand total for a car.
>>
>> A basic price can be entered. Given that basic price, a tax peercentage
>> is
>> calculated and added tot he grand total.
>>
>>
>> But unfortunately I am stuck with giving the user the choice to select
>> an
>> additional option!
>> The extra items have got set values, but at the moment they all get
>> summarized, but i want to choose.
>>
>> **
>>
>
>
> greetings Niclas, and welcome to Python! while i'll let the others comment
> on your code specifically, i can give some overall suggestions/guidance.
>
> you're trying to create an overall price calculator correct? while it's
> straightforward providing a base price, the complexity in your app (and
> real life) is the set of options that customers can choose from.
>
> in your situation, i think it would be more "Pythonic" to maintain the
> extras as a vector of options and prices. you then loop through those,
> prompting the user to enter Yes or No, and add either the cost or zero,
> respectively. that will help keep your code less complex as well. you
> would
> just be maintaining a running total until the user is done with all their
> selections.
>
> good luck!
> -- wesley

I'd be interested in knowing exactly what you mean by the term "vector" in
the above discussion.  When I saw the problem I thought dict would serve
as in

options = { "leather" : 1600, "alloy_wheels" : 1200,
# and so on
}

Comments?


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


Re: [Tutor] if/else option for making a choice

2013-02-18 Thread wesley chun
> in your situation, i think it would be more "Pythonic" to maintain the

> > extras as a vector of options and prices. you then loop through those,
> > prompting the user to enter Yes or No, and add either the cost or zero,
> > respectively. that will help keep your code less complex as well. you
> > would
> > just be maintaining a running total until the user is done with all their
> > selections.
> >
> > good luck!
> > -- wesley
>
> I'd be interested in knowing exactly what you mean by the term "vector" in
> the above discussion.  When I saw the problem I thought dict would serve
> as in
>
> options = { "leather" : 1600, "alloy_wheels" : 1200,
> # and so on
> }



perfectly fine if order doesn't matter. if it does, then a tuple will serve
a similar purposes however, it's easy to move back-n-forth between either
via dict(), dict.items(), iter/zip(), etc.

cheers,
-- wesley

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"A computer never does what you want... only what you tell it."
+wesley chun : wescpy at gmail : @wescpy
Python training & consulting : http://CyberwebConsulting.com
"Core Python" books : http://CorePython.com
Python blog: http://wescpy.blogspot.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] following on

2013-02-18 Thread Dave Angel

On 02/18/2013 02:01 PM, Matthew Ngaha wrote:

   



i sort of heard about a stack its a C/C++ thing i think?



A stack is fundamental to modern programming languages.  The only two 
machines I've used that didn't have a stack implemented at the machine 
level were the CDC 6000 series, and the IBM 360.  Both products of the 60's.


Processors like the Intel Pentium series have a stack microcoded into 
their lowest level instruction set.  When the processor itself is 
calling a subroutine, the current address is pushed onto the stack, and 
the return instruction pops it back off and jumps there.  Other 
instructions for manipulating it exist at the same level.  Typically all 
local variables in a function are stored in the current stack frame. 
The stack makes recursive functions practical, at a minimum.  Having 
multiple stacks makes multithreading practical as well.


Recursion and multithreading was very tricky on the CDC, since it stored 
return addresses right into the running code.


Usually a programming language will provide a stack-like data structure, 
for manipulating data that needs similar behavior.



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


Re: [Tutor] following on

2013-02-18 Thread Robert Sjoblom
>> i sort of heard about a stack its a C/C++ thing i think?
>
> A stack is fundamental to modern programming languages.  The only two
> machines I've used that didn't have a stack implemented at the machine level
> were the CDC 6000 series, and the IBM 360.  Both products of the 60's.

A while back, while researching something else entirely, I came upon a
useful article explaining recursion. In it he briefly touches on how a
stack functions, and the explanation is well worth reading (as is the
article!). You can find it here:
http://inventwithpython.com/blog/2011/08/11/recursion-explained-with-the-flood-fill-algorithm-and-zombies-and-cats/
If you just want to read the stack explanation, scroll down to
"Recursion is Just a Fancy Way of Using a Stack"

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


Re: [Tutor] following on

2013-02-18 Thread Steven D'Aprano

On 19/02/13 06:01, Matthew Ngaha wrote:

understanding of how everything works.


Use it., Experiment with it. Break it.
Thats the best way. Read the source code its all available in
Python or C.



Hey can you please tell me which source code youre referring too? The
initial files that come with Python? also the C code, where can i
locate this? is C something worth learning? why is Python code
available in C?



If you want to learn Python programming, read other Python programs.

You will find many Python programs, of greatly variable quality, here:

http://code.activestate.com/recipes/langs/python/

You might also like to read this book:

http://shop.oreilly.com/product/9780596007973.do

You can see the source code used by Python modules by searching for them on 
your computer, then opening them in a text editor. But beware that you don't 
accidentally modify them, because you may break your Python installation.

Another alternative is to read the source on-line. Many pages in the Python 
docs link directly to the source code. E.g. this page:

http://docs.python.org/2/library/string.html

links directly to the source code:

http://hg.python.org/cpython/file/2.7/Lib/string.py

You can learn a lot from the source code of Python modules.


The source code of the Python interpreter, on the other hand, is much, much 
more complicated. You will probably have a lot of trouble with it, since you 
are unfamiliar with C, and unfamiliar with the internal C libraries used in the 
interpreter. But if you are feeling masochistic and want to give it a go, you 
will find the source code here:

http://hg.python.org/cpython

Click the "Browse" link to get to the latest version.

Many people will advise that learning C is a good idea. I understand their 
arguments, but as an old curmudgeon I can say I don't like C and I think the 
world would be much better without it :-)

The Python interpreter may be written in many different languages. C is only the most 
common language. The "Big Four" Python interpreters are written in four 
different languages:

CPython (what you probably think of when you say "Python"): C
Jython: Java
IronPython: Microsoft .Net CLR
PyPy: RPython ("Restricted Python")

but there are others:

CLPython: Common Lisp
Burp and Hope: Haskell
Nuitka: C++
Pynie: Parrot
Vyper: Ocaml
Skulpt: Javascript

although some of these may be experimental, obsolete or abandoned.




yes i will try not to reinvent the wheel, a lot of tutorials ive read
seem to always point this out. Just out of curiousity what is a bag,
stack or circular list? what is needed to create something like this?
i sort of heard about a stack its a C/C++ thing i think?



You can read about data structures like stack, bag (multiset) and circular list 
here:

http://en.wikipedia.org/wiki/List_of_data_structures



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


Re: [Tutor] following on

2013-02-18 Thread Matthew Ngaha
Hey, a Big thanks to everyone that has offered their input. I really
appreciate it. Also thanks for all the additional links, i will
definately start to read up on data structures , source code and
everything else that was suggested
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Query String

2013-02-18 Thread Sunil Tech
Hi All,

I thank you all, for all the responses.
& teaching us to learn Python.

Here i request, can you tell me what is Query String with some examples?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor