[Tutor] Performing an union of two files containing keywords

2014-02-17 Thread Aaron Misquith
I have 2 different text files.

File1.txt contains:

file
RAMPython
parser

File2.txt contains:

file1234
program

I want to perform an union of these both files such that i get an output
file3.txt which contains:

file
RAMPython
parser1234
program



The program that i'm working on just combines two files. Any help on
performing an union such that a keyword would not be repeated would be
appreciated.


Code:
with open("C:\\File1.txt") as fin1: lines = fin1.readlines()
with open("C:\\File2.txt") as fin2: lines.extend(fin2.readlines())
with open("D:\\file3.txt", "r+") as fout: fout.write('\n'.join(lines))
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Performing an union of two files containing keywords

2014-02-17 Thread Mark Lawrence

On 17/02/2014 09:07, Aaron Misquith wrote:

I have 2 different text files.

File1.txt contains:

|file
RAM
Python
parser|

File2.txt contains:

|file
1234
program|

I want to perform an union of these both files such that i get an output
file3.txt which contains:

|file
RAM
Python
parser
1234
program
|
The program that i'm working on just combines two files. Any help on
performing an union such that a keyword would not be repeated would be
appreciated.


Code:
with open("C:\\File1.txt") as fin1: lines = fin1.readlines()
with open("C:\\File2.txt") as fin2: lines.extend(fin2.readlines())
with open("D:\\file3.txt", "r+") as fout: fout.write('\n'.join(lines))



Use sets http://docs.python.org/3/library/stdtypes.html#set

--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection 
is active.
http://www.avast.com


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


Re: [Tutor] Performing an union of two files containing keywords

2014-02-17 Thread Peter Otten
Aaron Misquith wrote:

> I have 2 different text files.
> 
> File1.txt contains:
> 
> file
> RAMPython
> parser
> 
> File2.txt contains:
> 
> file1234
> program
> 
> I want to perform an union of these both files such that i get an output
> file3.txt which contains:
> 
> file
> RAMPython
> parser1234
> program
> 
> 
> 
> The program that i'm working on just combines two files. Any help on
> performing an union such that a keyword would not be repeated would be
> appreciated.

Have a look at the set class and its update() method.
 
> Code:
> with open("C:\\File1.txt") as fin1: lines = fin1.readlines()
> with open("C:\\File2.txt") as fin2: lines.extend(fin2.readlines())
> with open("D:\\file3.txt", "r+") as fout: fout.write('\n'.join(lines))

fout.write("\n".join(lines))

will add extra newlines to the already existing ones.

Use 

fout.writelines(lines)

instead.

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


Re: [Tutor] Performing an union of two files containing keywords

2014-02-17 Thread spir

On 02/17/2014 10:07 AM, Aaron Misquith wrote:

I have 2 different text files.

File1.txt contains:

file
RAMPython
parser

File2.txt contains:

file1234
program

I want to perform an union of these both files such that i get an output
file3.txt which contains:

file
RAMPython
parser1234
program


I don't understand the logic of your "union" (???) at all. Is your example 
correct? A naive union in the naive sense would _here_ keep all items, since 
they are all different, and certainly not compose a non-existant one "parser1234".


d

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


Re: [Tutor] Performing an union of two files containing keywords

2014-02-17 Thread Oscar Benjamin
On 17 February 2014 09:07, Aaron Misquith  wrote:
>
> The program that i'm working on just combines two files. Any help on
> performing an union such that a keyword would not be repeated would be
> appreciated.
>
> Code:
> with open("C:\\File1.txt") as fin1: lines = fin1.readlines()
> with open("C:\\File2.txt") as fin2: lines.extend(fin2.readlines())
> with open("D:\\file3.txt", "r+") as fout: fout.write('\n'.join(lines))

Something like this:

with open(r'D:\file3.txt', 'r+') as fout:
keywords_seen = set()
for filename in r'C:\File1.txt', r'C:\File2.txt':
with open(filename) as fin:
for line in fin:
keyword = line.strip()
if keyword not in keywords_seen:
fout.write(line)
keywords.add(keyword)

Why are you opening the output file with mode 'r+'? Are you intending
to only overwrite part of the file? I think you probably should use
mode 'w' instead.


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


Re: [Tutor] Performing an union of two files containing keywords

2014-02-17 Thread Alan Gauld

On 17/02/14 10:29, spir wrote:


I want to perform an union of these both files such that i get an output
file3.txt which contains:

file
RAMPython
parser1234
program


I don't understand the logic of your "union" (???) at all. Is your
example correct?


It's an email formatting issue, I saw the same thing originally 
(Android) but in Thunderbird all became clear!


RAMPython ->
RAM
Pyhon

and
parser1234 ->
parser
1234

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

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


Re: [Tutor] Performing an union of two files containing keywords

2014-02-17 Thread Dave Angel

On 02/17/2014 06:12 AM, Oscar Benjamin wrote:

On 17 February 2014 09:07, Aaron Misquith  wrote:


The program that i'm working on just combines two files. Any help on
performing an union such that a keyword would not be repeated would be
appreciated.

Code:
with open("C:\\File1.txt") as fin1: lines = fin1.readlines()
with open("C:\\File2.txt") as fin2: lines.extend(fin2.readlines())
with open("D:\\file3.txt", "r+") as fout: fout.write('\n'.join(lines))


Something like this:

with open(r'D:\file3.txt', 'r+') as fout:
 keywords_seen = set()
 for filename in r'C:\File1.txt', r'C:\File2.txt':
 with open(filename) as fin:
 for line in fin:
 keyword = line.strip()
 if keyword not in keywords_seen:
 fout.write(line)
 keywords.add(keyword)

Why are you opening the output file with mode 'r+'? Are you intending
to only overwrite part of the file? I think you probably should use
mode 'w' instead.



You forgot to append the newline to strings in the write() method calls.

 fout.write(line + "\n")


--
DaveA

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


Re: [Tutor] Performing an union of two files containing keywords

2014-02-17 Thread Dave Angel
 Aaron Misquith  Wrote in message:
> As two others have said, a set is the simplest solution to avoid duplicates. 

There are other questions to ask, however.   Primary is whether
 order matters.

If it does not, then observe that list(set(mylist)) will produce a
 list from a list without duplicates. 

If order does matter, then I ask whether there was some constraint
 that forced you to do it 3 lines.

If not, I would suggest that you write the output one line at a
 time,  after checking each line against a set. If the line is not
 in the set, add it to the set and write it to the file.
 

And watch out for newlines.  What happens if one or both input
 files are missing their final newlines?



-- 
DaveA

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


[Tutor] Regarding Exceptions

2014-02-17 Thread Khalid Al-Ghamdi
Hi,
in the following snippet, why is it I don't need to create an Exception
object and I can use the class directly in raise my custom exception?

http://pastebin.com/embed_iframe.php?i=7ANcvLHR";
style="border:none;width:100%">
Thanks
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Regarding Exceptions

2014-02-17 Thread Khalid Al-Ghamdi
Hi,

Why is it i can use mu custom class exception without creating an exception
object first?

Thanks


   1. class ShortInputException(Exception): def __init__(self, length,
atleast):
   2. Exception.__init__(self)
   3. self.length = length
   4. self.atleast = atleast
   5. try:
   6. text = input() if len(text) < 3:
   7. raise ShortInputException(len(text), 3) # Other work can continue as
   usual here
   8. except EOFError:
   9. print()
   10. except ShortInputException as ex:
   11. print(\
   12. .format(ex.length, ex.atleast)) else:
   13. print()
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Performing an union of two files containing keywords

2014-02-17 Thread Dave Angel

On 02/17/2014 05:29 AM, spir wrote:

On 02/17/2014 10:07 AM, Aaron Misquith wrote:

I have 2 different text files.

File1.txt contains:

file
RAMPython
parser

File2.txt contains:

file1234
program

I want to perform an union of these both files such that i get an output
file3.txt which contains:

file
RAMPython
parser1234
program


I don't understand the logic of your "union" (???) at all. Is your
example correct? A naive union in the naive sense would _here_ keep all
items, since they are all different, and certainly not compose a
non-existant one "parser1234".

d



AHA, the mysterious "html" conversion error strikes again.  Since Aaron 
erroneously (and presumably unknowingly) posted in html, and you view it 
(as most do, and all should) as text, Aaron's email program erroneously 
lost some of the whitespace markings, and totally distorted the meaning.


But "that ship has already sailed", so we have to live with it without 
mention.


--
DaveA

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


[Tutor] Beginner - Clarifying 'understanding randint arguments'

2014-02-17 Thread Marc Eymard
Hi Tutor,

The previous elements I sent to the mailing list were incomplete and needs no 
answer from Tutor.

To clarify and re-phrase my script issue:

I want to code a game whereby the computer guesses either by luck or deduction 
a number I pick within [0, 100].

In attached machine_guess_number.py in which line 22 statement number = 
random.randint(low_range + 1, high_range - 1) doesn't narrow the range:

- to allow number deduction...
- ...  avoiding ValueError: empty range for randrange()

I also attached the output of the script leading to the error showing that the 
loop does not exit in time to avoid error by working out the answer based on a 
logical narrowing of the guessing range.

Hope this clarifies my previous email (see further down this note).

Thanks,
Marc


> Subject: Your message to Tutor awaits moderator approval
> From: tutor-ow...@python.org
> To: marc_eym...@hotmail.com
> Date: Sat, 15 Feb 2014 17:26:41 +0100
> 
> Your mail to 'Tutor' with the subject
> 
> Beginner - understanding randint arguments
> 
> Is being held until the list moderator can review it for approval.
> 
> The reason it is being held:
> 
> Post by non-member to a members-only list
> 
> Either the message will get posted to the list, or you will receive
> notification of the moderator's decision.  If you would like to cancel
> this posting, please visit the following URL:
> 
> 
> https://mail.python.org/mailman/confirm/tutor/8ee9ed6d473cbc6d77ddc7af36237a9cc3b1d4b3
> 

From: marc_eym...@hotmail.com
To: tutor@python.org
Subject: Beginner - understanding randint arguments
Date: Sat, 15 Feb 2014 16:25:34 +




Hello Tutor,

I need to generate a random integer between 0 and 100.

The range is supposed to be adjusted by my two variables:
low_range and high_range.

The
 logic of using the variables as part of the function arguments is to 
manage to get a smaller range each time the function is called excluding the 
possible repeat of the return value of randint.

Here is what happens in my script:

>>> import random
>>> low_range = -1
>>> high_range = 101
>>> random.randint(low_range + 1, high_range - 1)
56
>>> low_range
-1
>>> high_range
101

I was rather expecting:

 >>> low_range
0
>>> high_range
100

Can somebody explain why both low_range and high_range are still returning 
their initial values ?

Thanks,
Marc
  
  #Guess my Number game
#The computer has to guess the player's number by either luck or deduction
#number has to be within [0, 100]

import random

print('Pick a number between and 0 and 100...')
print('...and let me try to guess in as few attempts as possible.\n')

input('Press ENTER when ready to play.\n')

# set variables
clue = ''
count_guess = 0
low_range = -1
high_range = 101


while clue != 'correct' and low_range != high_range:


number = random.randint(low_range + 1, high_range - 1)
	
count_guess += 1

print('\nIs your number', number, end= '? ')
clue = input('\n\nEnter one of the following clues:\n\n' \
+ '\t\thigher\n'
+ '\t\tlower\n'
+ '\t\tcorrect\n\n'
+ 'Type here and press ENTER: ')

if clue == 'lower':
 high_range = number			

elif clue == 'higher':
 low_range = number

elif clue == 'correct':

if count_guess > 1:
 print('\nYou picked', number,'and it took me only', count_guess, 'attempts to find out.')

else: print('\nI could read your mind at once and saw you picked', number)


if low_range == high_range:
print('\nYou picked', number,'and it took me only', count_guess, 'attempts to find out.')


Python 3.3.2+ (default, Oct  9 2013, 14:50:09) 
[GCC 4.8.1] on linux
Type "copyright", "credits" or "license()" for more information.
>>>  RESTART 
>>> 
Pick a number between and 0 and 100...
...and let me try to guess in as few attempts as possible.

Press ENTER when ready to play.


Is your number 92? 

Enter one of the following clues:

		higher
		lower
		correct

Type here and press ENTER: higher

Is your number 93? 

Enter one of the following clues:

		higher
		lower
		correct

Type here and press ENTER: higher

Is your number 97? 

Enter one of the following clues:

		higher
		lower
		correct

Type here and press ENTER: lower

Is your number 95? 

Enter one of the following clues:

		higher
		lower
		correct

Type here and press ENTER: higher

Is your number 96? 

Enter one of the following clues:

		higher
		lower
		correct

Type here and press ENTER: lower


Traceback (most recent call last):
  File "/home/marc/Ubuntu One/Python/machine_guess_number.py", line 22, in 
number = random.randint(low_range + 1, high_range - 1)
  File "/usr/lib/python3.3/random.py", line 214, in randint
r

Re: [Tutor] Regarding Exceptions

2014-02-17 Thread Chris “Kwpolska” Warrick
On Mon, Feb 17, 2014 at 12:44 PM, Khalid Al-Ghamdi  wrote:
> Hi,
>
> Why is it i can use mu custom class exception without creating an exception
> object first?
>
> Thanks
>
> class ShortInputException(Exception): def __init__(self, length, atleast):
> Exception.__init__(self)
> self.length = length
> self.atleast = atleast
> try:
> text = input() if len(text) < 3:
> raise ShortInputException(len(text), 3) # Other work can continue as usual
> here
> except EOFError:
> print()
> except ShortInputException as ex:
> print(\
> .format(ex.length, ex.atleast)) else:
> print()

Hello there!

Unfortunately, the code you provided is invalid, and it seems not to
contain required indentation, newlines, and has some other things that
should break (eg. line 12)

Please reply (using Reply All!) in plaintext, with the correct code.

Also, do you mind phrasing your question more precisely?  Do you need
help with inheritance?  What is the problem you’re having?

-- 
Chris “Kwpolska” Warrick 
PGP: 5EAAEA16
stop html mail | always bottom-post | only UTF-8 makes sense
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Upload a file using python

2014-02-17 Thread rakesh sharma
Greetings!!
Hi Alan,
The error code was that of success. 200.Donno if the site supports a post 
method. It doesn't but is there any other way of automating the process.The 
issue of chrome 32 with selenium webdriver has stalled the show for me. Hence i 
had to fend for some other ways.
thanks,rakesh

> To: tutor@python.org
> From: alan.ga...@btinternet.com
> Date: Sun, 16 Feb 2014 15:17:52 +
> Subject: Re: [Tutor] Upload a file using python
> 
> On 16/02/14 09:15, rakesh sharma wrote:
> 
> > But I feel if i can use the http methods without involving any UI level
> > automation things would be better.
> 
> To use http the server at the other end needs to know what you are 
> trying to do. Given it already publishes an Ajax interface I'd have 
> thought it was unlikely to also have a raw http interface for file uploads.
> 
> > file. I performed it using post method. But dint get the required result.
> > The uploaded file dint appear in the site.
> 
> Does the server have a page that accepts post requests to upload files?
> If not it won't recognize your post request and possibly just ignore
> it or hopefully send you an http error back. Did you check the http 
> error code returned?
> 
> HTH
> -- 
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.flickr.com/photos/alangauldphotos
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Regarding Exceptions

2014-02-17 Thread Alan Gauld

On 17/02/14 11:44, Khalid Al-Ghamdi wrote:

Why is it i can use mu custom class exception without creating an
exception object first?


There are formatting problems when I try to copy your code.
I've tried to fix them below, apologies if I got it wrong.

However you do create an instance when you raise it...

> class ShortInputException(Exception):
>def __init__(self, length, atleast):
>Exception.__init__(self)
>self.length = length
>self.atleast = atleast
>try:
>text = input()
>if len(text) < 3:
>  raise ShortInputException(len(text), 3)

You have your class name followed by parens.
That's how you create an instance.
So you are raising an instance of your class.

except ShortInputException as ex:
print(...

And you catch the instance as ex here...

But remember that in Python classes are objects too...
So you don't always need to create an instance to
use a class.

HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

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


Re: [Tutor] Beginner - Clarifying 'understanding randint arguments'

2014-02-17 Thread Alan Gauld

On 17/02/14 11:39, Marc Eymard wrote:

Hi Tutor,

The previous elements I sent to the mailing list were incomplete and
needs no answer from Tutor.


But you got several nonetheless.
Did you read them?

If so you will know that calling randint() does not change the two 
xxx_range variables. It simply returns a random number as requested.

You will need to modify the range values yourself.


Hope this clarifies my previous email (see further down this note).



 >>> import random
 >>> low_range = -1
 >>> high_range = 101
 >>> random.randint(low_range + 1, high_range - 1)
56
 >>> low_range
-1
 >>> high_range
101*


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

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


Re: [Tutor] Upload a file using python

2014-02-17 Thread Alan Gauld

On 17/02/14 14:29, rakesh sharma wrote:

Greetings!!

Hi Alan,

The error code was that of success. 200.
Donno if the site supports a post method. It doesn't but is there any
other way of automating the process.
The issue of chrome 32 with selenium webdriver has stalled the show for me.
Hence i had to fend for some other ways.


OK I misunderstood your previous mail.
Its not that you don't want to use Ajax its that you don't
want to drive it via the browser UI?

In that case you should be able to call the Ajax API directly using 
urllib or cgi possibly some higher level Ajax module - I suspect one 
will exist somewhere!


But at this point we probably need to see some code to see how you are 
trying to do it.



--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

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


Re: [Tutor] Upload a file using python

2014-02-17 Thread Mark Lawrence

On 17/02/2014 15:38, Alan Gauld wrote:


In that case you should be able to call the Ajax API directly using
urllib or cgi possibly some higher level Ajax module - I suspect one
will exist somewhere!



First port of call for such things https://pypi.python.org/pypi

--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection 
is active.
http://www.avast.com


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


[Tutor] for: how to skip items

2014-02-17 Thread Gabriele Brambilla
Hi,

I'm wondering how I can (if I can) make a for loop in which I don't use all
the elements.

for example

a100 = list(range(100))

for a in a100:
 print(a)

it print out to me all the numbers from 0 to 99
But if I want to display only the numbers 0, 9, 19, 29, 39, ...(one every
10 elements) how can I do it WITHOUT defining a new list (my real case is
not so simple) and WITHOUT building a list of indexes?

thank you

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


Re: [Tutor] Performing an union of two files containing keywords

2014-02-17 Thread Oscar Benjamin
On 17 February 2014 13:16, Dave Angel  wrote:
> On 02/17/2014 06:12 AM, Oscar Benjamin wrote:
>>
>> Something like this:
>>
>> with open(r'D:\file3.txt', 'r+') as fout:
>>  keywords_seen = set()
>>  for filename in r'C:\File1.txt', r'C:\File2.txt':
>>  with open(filename) as fin:
>>  for line in fin:
>>  keyword = line.strip()
>>  if keyword not in keywords_seen:
>>  fout.write(line)
>>  keywords.add(keyword)

The line above should obviously be keywords_seen.add(keyword)

> You forgot to append the newline to strings in the write() method calls.
>
>  fout.write(line + "\n")

Iterating over a file keeps the newlines:

>>> list(open('file1.txt'))
['file1\n', 'file2\n']


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


Re: [Tutor] for: how to skip items

2014-02-17 Thread Oscar Benjamin
On 17 February 2014 16:05, Gabriele Brambilla
 wrote:
> Hi,
>
> I'm wondering how I can (if I can) make a for loop in which I don't use all
> the elements.
>
> for example
>
> a100 = list(range(100))
>
> for a in a100:
>  print(a)
>
> it print out to me all the numbers from 0 to 99
> But if I want to display only the numbers 0, 9, 19, 29, 39, ...(one every 10
> elements) how can I do it WITHOUT defining a new list (my real case is not
> so simple) and WITHOUT building a list of indexes?

for a in a100:
if a % 10 == 9:
print(a)

Alternatively:

for a in a100:
if a % 10 == 9:
continue
print(a)


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


Re: [Tutor] for: how to skip items

2014-02-17 Thread Joel Goldstick
On Feb 17, 2014 11:06 AM, "Gabriele Brambilla" <
gb.gabrielebrambi...@gmail.com> wrote:
>
> Hi,
>
> I'm wondering how I can (if I can) make a for loop in which I don't use
all the elements.
>
> for example
>
> a100 = list(range(100))
>
> for a in a100:
>  print(a)
>
> it print out to me all the numbers from 0 to 99
> But if I want to display only the numbers 0, 9, 19, 29, 39, ...(one every
10 elements) how can I do it WITHOUT defining a new list (my real case is
not so simple) and WITHOUT building a list of indexes?
>
> thank you
>
> Gabriele
>
> __since this is homework, study if statements and the modulo
operator (%)_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] for: how to skip items

2014-02-17 Thread Gabriele Brambilla
Excuse me for the bad english:
not "a random float numbers" but "random float numbers"

Gabriele


2014-02-17 11:13 GMT-05:00 Gabriele Brambilla <
gb.gabrielebrambi...@gmail.com>:

> No sorry,
>
> it's because my problem is not so simple:
> imagine that in a100 contains not integer sorted in a good way but a
> random float numbers.
> How could I display only one item every 10?
>
> thanks
>
> Gabriele
>
>
> 2014-02-17 11:08 GMT-05:00 Oscar Benjamin :
>
> On 17 February 2014 16:05, Gabriele Brambilla
>>  wrote:
>> > Hi,
>> >
>> > I'm wondering how I can (if I can) make a for loop in which I don't use
>> all
>> > the elements.
>> >
>> > for example
>> >
>> > a100 = list(range(100))
>> >
>> > for a in a100:
>> >  print(a)
>> >
>> > it print out to me all the numbers from 0 to 99
>> > But if I want to display only the numbers 0, 9, 19, 29, 39, ...(one
>> every 10
>> > elements) how can I do it WITHOUT defining a new list (my real case is
>> not
>> > so simple) and WITHOUT building a list of indexes?
>>
>> for a in a100:
>> if a % 10 == 9:
>> print(a)
>>
>> Alternatively:
>>
>> for a in a100:
>> if a % 10 == 9:
>> continue
>> print(a)
>>
>>
>> Oscar
>>
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] for: how to skip items

2014-02-17 Thread David Palao
Hi Gabriele,
Without knowing the details of what you are trying, I guess you could
be interested in looking at how to define your own iterators.

Regards

2014-02-17 17:05 GMT+01:00 Gabriele Brambilla :
> Hi,
>
> I'm wondering how I can (if I can) make a for loop in which I don't use all
> the elements.
>
> for example
>
> a100 = list(range(100))
>
> for a in a100:
>  print(a)
>
> it print out to me all the numbers from 0 to 99
> But if I want to display only the numbers 0, 9, 19, 29, 39, ...(one every 10
> elements) how can I do it WITHOUT defining a new list (my real case is not
> so simple) and WITHOUT building a list of indexes?
>
> thank you
>
> Gabriele
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] for: how to skip items

2014-02-17 Thread Gabriele Brambilla
Doesn't exist a way in Python to do like in C

for i=0, i<100, i=i+10

? without creating a list of index?

Gabriele


2014-02-17 11:15 GMT-05:00 David Palao :

> Hi Gabriele,
> Without knowing the details of what you are trying, I guess you could
> be interested in looking at how to define your own iterators.
>
> Regards
>
> 2014-02-17 17:05 GMT+01:00 Gabriele Brambilla <
> gb.gabrielebrambi...@gmail.com>:
> > Hi,
> >
> > I'm wondering how I can (if I can) make a for loop in which I don't use
> all
> > the elements.
> >
> > for example
> >
> > a100 = list(range(100))
> >
> > for a in a100:
> >  print(a)
> >
> > it print out to me all the numbers from 0 to 99
> > But if I want to display only the numbers 0, 9, 19, 29, 39, ...(one
> every 10
> > elements) how can I do it WITHOUT defining a new list (my real case is
> not
> > so simple) and WITHOUT building a list of indexes?
> >
> > thank you
> >
> > Gabriele
> >
> > ___
> > Tutor maillist  -  Tutor@python.org
> > To unsubscribe or change subscription options:
> > https://mail.python.org/mailman/listinfo/tutor
> >
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] for: how to skip items

2014-02-17 Thread Oscar Benjamin
On 17 February 2014 16:13, Gabriele Brambilla
 wrote:
> No sorry,
>
> it's because my problem is not so simple:
> imagine that in a100 contains not integer sorted in a good way but a random
> float numbers.
> How could I display only one item every 10?

for n, a in enumerate(a100):
if n % 10 == 9:
print(a)


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


Re: [Tutor] for: how to skip items

2014-02-17 Thread Gabriele Brambilla
No sorry,

it's because my problem is not so simple:
imagine that in a100 contains not integer sorted in a good way but a random
float numbers.
How could I display only one item every 10?

thanks

Gabriele


2014-02-17 11:08 GMT-05:00 Oscar Benjamin :

> On 17 February 2014 16:05, Gabriele Brambilla
>  wrote:
> > Hi,
> >
> > I'm wondering how I can (if I can) make a for loop in which I don't use
> all
> > the elements.
> >
> > for example
> >
> > a100 = list(range(100))
> >
> > for a in a100:
> >  print(a)
> >
> > it print out to me all the numbers from 0 to 99
> > But if I want to display only the numbers 0, 9, 19, 29, 39, ...(one
> every 10
> > elements) how can I do it WITHOUT defining a new list (my real case is
> not
> > so simple) and WITHOUT building a list of indexes?
>
> for a in a100:
> if a % 10 == 9:
> print(a)
>
> Alternatively:
>
> for a in a100:
> if a % 10 == 9:
> continue
> print(a)
>
>
> Oscar
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] for: how to skip items

2014-02-17 Thread Oscar Benjamin
On 17 February 2014 16:17, Gabriele Brambilla
 wrote:
> Doesn't exist a way in Python to do like in C
>
> for i=0, i<100, i=i+10
>
> ? without creating a list of index?

You haven't said which Python version you're using. In Python 2 the
range function returns a list but the xrange function returns an
iterator. In Python 3 the range function returns an iterator.

Assuming you're using Python 3 then you can just do:

for i in range(0, 100, 10):
print(a100[i])


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


Re: [Tutor] for: how to skip items

2014-02-17 Thread Gabriele Brambilla
thanks,
in the end I'm using something like this and it works:

zipPARApha = zip(Pampli, Pgamma, Pecut, Pb, g)

for n, (a1,b1,c1,d1,pha) in enumerate(zipPARApha):

where the arguments of zip are lists of the same size.

Gabriele


2014-02-17 11:19 GMT-05:00 Oscar Benjamin :

> On 17 February 2014 16:13, Gabriele Brambilla
>  wrote:
> > No sorry,
> >
> > it's because my problem is not so simple:
> > imagine that in a100 contains not integer sorted in a good way but a
> random
> > float numbers.
> > How could I display only one item every 10?
>
> for n, a in enumerate(a100):
> if n % 10 == 9:
> print(a)
>
>
> Oscar
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] for: how to skip items

2014-02-17 Thread Mark Lawrence

On 17/02/2014 16:17, Gabriele Brambilla wrote:

Doesn't exist a way in Python to do like in C

for i=0, i<100, i=i+10

? without creating a list of index?

Gabriele


2014-02-17 11:15 GMT-05:00 David Palao mailto:dpalao.pyt...@gmail.com>>:

Hi Gabriele,
Without knowing the details of what you are trying, I guess you could
be interested in looking at how to define your own iterators.

Regards

2014-02-17 17:05 GMT+01:00 Gabriele Brambilla
mailto:gb.gabrielebrambi...@gmail.com>>:
 > Hi,
 >
 > I'm wondering how I can (if I can) make a for loop in which I
don't use all
 > the elements.
 >
 > for example
 >
 > a100 = list(range(100))
 >
 > for a in a100:
 >  print(a)
 >
 > it print out to me all the numbers from 0 to 99
 > But if I want to display only the numbers 0, 9, 19, 29, 39,
...(one every 10
 > elements) how can I do it WITHOUT defining a new list (my real
case is not
 > so simple) and WITHOUT building a list of indexes?
 >
 > thank you
 >
 > Gabriele
 >


Please don't top post on this list.

--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection 
is active.
http://www.avast.com


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


Re: [Tutor] for: how to skip items

2014-02-17 Thread emile

On 02/17/2014 08:05 AM, Gabriele Brambilla wrote:

Hi,

I'm wondering how I can (if I can) make a for loop in which I don't use all
the elements.

for example

a100 = list(range(100))

for a in a100:
  print(a)

it print out to me all the numbers from 0 to 99
But if I want to display only the numbers 0, 9, 19, 29, 39, ...(one every
10 elements) how can I do it WITHOUT defining a new list (my real case is
not so simple) and WITHOUT building a list of indexes?


Look into python's slice notation --

[root@whfw2 root]# python
Python 2.7.1 (r271:86832, Dec  6 2010, 13:47:21)
[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a100 = list(range(100))
>>> a100[::10]
[0, 10, 20, 30, 40, 50, 60, 70, 80, 90]
>>>


HTH,

Emile




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


[Tutor] Fwd: for: how to skip items

2014-02-17 Thread ugajin

 0, 9, 19, 29, 39, is not every 10th index

If you want to output every 10th. index try:

a100 = list(range(0,100,10))

for a in a100:
 print(a)



 

 

-Original Message-
From: Gabriele Brambilla 
To: python tutor 
Sent: Mon, 17 Feb 2014 16:06
Subject: [Tutor] for: how to skip items


Hi,


I'm wondering how I can (if I can) make a for loop in which I don't use all the 
elements.


for example


a100 = list(range(100))


for a in a100:
 print(a)


it print out to me all the numbers from 0 to 99
But if I want to display only the numbers 0, 9, 19, 29, 39, ...(one every 10 
elements) how can I do it WITHOUT defining a new list (my real case is not so 
simple) and WITHOUT building a list of indexes?


thank you


Gabriele

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

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


Re: [Tutor] for: how to skip items

2014-02-17 Thread Andreas Perstinger
Gabriele Brambilla  wrote:
>it's because my problem is not so simple:
>imagine that in a100 contains not integer sorted in a good way but a
>random float numbers.
>How could I display only one item every 10?

You can provide a step size if you slice a list:

>>> l = list(range(10))
>>> l[0:10:2]
[0, 2, 4, 6, 8]
>>> l[0:10:5]
[0, 5]

Is that what you want?

Bye, Andreas

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


Re: [Tutor] Regarding Exceptions

2014-02-17 Thread Dave Angel

On 02/17/2014 06:44 AM, Khalid Al-Ghamdi wrote:

Hi,

Why is it i can use mu custom class exception without creating an exception
object first?

Thanks


1. class ShortInputException(Exception): def __init__(self, length,
 atleast):
2. Exception.__init__(self)
3. self.length = length
4. self.atleast = atleast
5. try:
6. text = input() if len(text) < 3:
7. raise ShortInputException(len(text), 3) # Other work can continue as
usual here
8. except EOFError:
9. print()
10. except ShortInputException as ex:
11. print(\
12. .format(ex.length, ex.atleast)) else:
13. print()




Your code posted here is totally broken.  indentation is missing and 
lines are illegally combined.  And somehow you added colors and 
incorrect line numbers besides.


Please post here in plain text, not html, and without line numbers, 
colorizing or other nonsense.


Now to your question.   I don't know what you mean "without creating an 
exception object".  You create one in the raise statement.  It is 
dubious however to raise an exception in any __init__ routine, since 
that could perhaps mean that not all the intiialization has actually 
happened.  And it could lead to infinite recursion, if the exception is 
the same class as you're initializing.  I would also strenuously avoid 
doing input() or other blocking operations in the __init__ routine, but 
I'm not sure I know of any real reason why.


if this is real code, and not just an experiment, could you explain 
(after formatting it in a text message so we can actually read it) just 
what does happen, and what you expected/wished to happen?


(You might not see this message or my others for quite some time, as it 
seems I've been dropped from the approved posters by having joined the 
mailing list ???)


--
DaveA

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


Re: [Tutor] Performing an union of two files containing keywords

2014-02-17 Thread Dave Angel
 Oscar Benjamin  Wrote in message:
> On 17 February 2014 13:16, Dave Angel  wrote:
>> On 02/17/2014 06:12 AM, Oscar Benjamin wrote:
>>>
>>> Something like this:
>>>
>>> with open(r'D:\file3.txt', 'r+') as fout:
>>>  keywords_seen = set()
>>>  for filename in r'C:\File1.txt', r'C:\File2.txt':
>>>  with open(filename) as fin:
>>>  for line in fin:
>>>  keyword = line.strip()
>>>  if keyword not in keywords_seen:
>>>  fout.write(line)
>>>  keywords.add(keyword)
> 
> The line above should obviously be keywords_seen.add(keyword)
> 
>> You forgot to append the newline to strings in the write() method calls.
>>
>>  fout.write(line + "\n")
> 
> Iterating over a file keeps the newlines:
> 
 list(open('file1.txt'))
> ['file1\n', 'file2\n']
> 

Sorry,  I read your code too quickly and figured you had done an
 in-place strip(). Since you did not, you run the risk of
 encountering a file without trailing newline. 

I prefer to at least rstrip each line,  then add the newline back
 in when writing. 

-- 
DaveA

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


Re: [Tutor] for: how to skip items

2014-02-17 Thread Dave Angel
 Gabriele Brambilla  Wrote in message: 

> in the end I'm using something like this and it works:

> zipPARApha = zip(Pampli, Pgamma, Pecut, Pb, g)
> for n, (a1,b1,c1,d1,pha) in enumerate(zipPARApha):

> where the arguments of zip are lists of the same size.

Simpler would be:

for a1,b1,c1,d1,pha in zipPARApha[9::10]:

or if the list is huge,  the islice equivalent. 

The [9::10] syntax says you want to slice out elements 9, 19, etc.


-- 
DaveA

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


[Tutor] constructing semi-arbitrary functions

2014-02-17 Thread André Walker-Loud
Hello python tutors,

I am utilizing a 3rd party numerical minimization routine.  This routine 
requires an input function, which takes as arguments, only the variables with 
which to solve for.  But I don’t want to define all possible input functions, 
in a giant switch, but rather, if I know I am fitting a polynomial, I would 
like to just pass a list of parameters and have the code know how to construct 
this function.

To construct for example, a chisq function, you must pass not only the 
variables to solve for, but also the data, uncertainties, and perhaps other 
arguments.  So it requires a little hacking to get it to work.  With the help 
of my friends and looking at similar code, I have come up with two ways that 
work under my simple test cases, and I have a few questions about them.

The 3rd party minimizer utilizes the .func_code.co_varnames and 
.func_code.co_argcount to determine the name and number of variables to 
minimize.  eg.

> g = lambda x,c_0,c_1: c_0 + c_1 * x
> g.func_code.co_varnames
('x', 'c_0', 'c_1’)
> g.func_code.co_argcount
3

so what is needed is a function
> def f(c_0,c_1):
> …#construct chi_sq(c_0,c_1,x,y,…)

=
METHOD 1: make classes to define functions
#create a function_structure which can be used to make the necessary func_code
class FunctionStruct:
def __init__(self, **kwds):
self.__dict__.update(kwds)
def __str__(self):  
  
return self.__dict__.__str__()
def __repr__(self):
return self.__str__()
def __getitem__(self, s):
return self.__dict__[s]

# then make a polynomial class which constructs a polynomial of order len(pars)
class PolynomialFunc:
def __init__(self,x,pars):
self.pars = pars
self.func_code = FunctionStruct(#create the variables used by minimizer
co_argcount = len(pars),
co_varnames = tuple(['c_%i'%(i) for i in range(len(pars))])
)
def __call__(self):
self.poly = 0.
for n in range(self.func_code.co_argcount):
self.poly += self.pars[n] * x **n
return self.poly
# eg create a polynomial of order (1) which can be used in the minimizer
f = PolynomialFunc(x,[c_0,c_1])
f.func_code.co_varnames
('c_0', 'c_1’)

=
METHOD 2: use strings, exec and globals to construct function
def minimize(pars,x,y,dy):
global g_x, g_y, g_dy
g_x = x; g_y = y; g_dy = dy
argnames = ['c_%i'%(i) for i in range(len(pars))]
funcargs = ", ".join(argnames)
funcdef = 'def chisq_mn(' + funcargs + '):\n'
funcdef += 'global g_x, g_y, g_dy\n'
funcdef += 'return chisq(['+funcargs+'],g_x,g_y,g_dy)\n’ #chisq is 
defined in same file
# evaluate string and build function
print "funcdef=", funcdef
exec funcdef in globals()

m = ThirdParty.setup_minimize(chisq_mn)
for i in range(len(pars)):
m.values[argnames[i]] = pars[i]
m.errors[argnames[i]] = 0.1 * pars[i]
return m
# then we can define
f = minimize(pars,x,y,dy,chisq_func)

Question 1:
Is there a better way to accomplish (my hopefully clear) goals?

Question 2:
In method 1, is there a way to combine the two classes, or is the 
FunctionStruct necessary?  I want to do something *like*

self.func_code.co_varnames = tuple(['c_%i'%(i) for i in range(len(pars))])
self.func_code.co_argcount = len(pars)

but I was not sure how to first define ‘self.func_code’.  Can you define it as 
an empty container somehow (is that the right way to describe it)?


Question 3:
The 2nd method is in some sense simpler - I just hack together a string, but I 
need to use global variables, which I know can be problematic.  Which method, 1 
or 2, is in general better?  One answer, is whatever works, but I am trying to 
also improve my programming skills, so I am wondering from a software 
perspective, which is preferable?


Thanks,

Andre

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


Re: [Tutor] constructing semi-arbitrary functions

2014-02-17 Thread Oscar Benjamin
On Feb 17, 2014 7:24 PM, ""André Walker-Loud
""
 wrote:
>
> Question 1:
> Is there a better way to accomplish (my hopefully clear) goals?

I'm not sure that there is given the constraints you're under from the
third party function.

> Question 2:
> In method 1, is there a way to combine the two classes, or is the
FunctionStruct necessary?  I want to do something *like*
>
> self.func_code.co_varnames = tuple(['c_%i'%(i) for i in range(len(pars))])
> self.func_code.co_argcount = len(pars)
>
> but I was not sure how to first define 'self.func_code'.  Can you define
it as an empty container somehow (is that the right way to describe it)?

I'm not sure about the details of this. Maybe Eryksun will chime in there...

> Question 3:
> The 2nd method is in some sense simpler - I just hack together a string,
but I need to use global variables, which I know can be problematic.  Which
method, 1 or 2, is in general better?  One answer, is whatever works, but I
am trying to also improve my programming skills, so I am wondering from a
software perspective, which is preferable?

Either solution if it works for this case seems fine to me. I'd be inclined
to in for the simpler compile/exec approach.

The "right" solution is to change the interface of the third party
function. It is poorly designed and should not be inspecting those function
attributes or it should at least provide an option for you to provide that
information in a different way. Assuming it's open source you could try
sending a patch. In the mean time you could copy that code and modify it to
suit your purposes.

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


Re: [Tutor] constructing semi-arbitrary functions

2014-02-17 Thread Peter Otten
André Walker-Loud  wrote:

> Hello python tutors,
> 
> I am utilizing a 3rd party numerical minimization routine.  This routine
> requires an input function, which takes as arguments, only the variables
> with which to solve for.  But I don’t want to define all possible input
> functions, in a giant switch, but rather, if I know I am fitting a
> polynomial, I would like to just pass a list of parameters and have the
> code know how to construct this function.
> 
> To construct for example, a chisq function, you must pass not only the
> variables to solve for, but also the data, uncertainties, and perhaps
> other arguments.  So it requires a little hacking to get it to work.  With
> the help of my friends and looking at similar code, I have come up with
> two ways that work under my simple test cases, and I have a few questions
> about them.
> 
> The 3rd party minimizer utilizes the .func_code.co_varnames and
> .func_code.co_argcount to determine the name and number of variables to
> minimize.  eg.
> 
>> g = lambda x,c_0,c_1: c_0 + c_1 * x
>> g.func_code.co_varnames
> ('x', 'c_0', 'c_1’)
>> g.func_code.co_argcount
> 3
> 
> so what is needed is a function
>> def f(c_0,c_1):
>> …#construct chi_sq(c_0,c_1,x,y,…)
> 
> =
> METHOD 1: make classes to define functions
> #create a function_structure which can be used to make the necessary
> #func_code
> class FunctionStruct:
> def __init__(self, **kwds):
> self.__dict__.update(kwds)
> def __str__(self):
> return self.__dict__.__str__()
> def __repr__(self):
> return self.__str__()
> def __getitem__(self, s):
> return self.__dict__[s]
> 
> # then make a polynomial class which constructs a polynomial of order
> # len(pars)
> class PolynomialFunc:
> def __init__(self,x,pars):
> self.pars = pars
> self.func_code = FunctionStruct(#create the variables used by
> minimizer
> co_argcount = len(pars),
> co_varnames = tuple(['c_%i'%(i) for i in range(len(pars))])
> )
> def __call__(self):
> self.poly = 0.
> for n in range(self.func_code.co_argcount):
> self.poly += self.pars[n] * x **n
> return self.poly
> # eg create a polynomial of order (1) which can be used in the minimizer
> f = PolynomialFunc(x,[c_0,c_1])
> f.func_code.co_varnames
> ('c_0', 'c_1’)
> 
> =
> METHOD 2: use strings, exec and globals to construct function
> def minimize(pars,x,y,dy):
> global g_x, g_y, g_dy
> g_x = x; g_y = y; g_dy = dy
> argnames = ['c_%i'%(i) for i in range(len(pars))]
> funcargs = ", ".join(argnames)
> funcdef = 'def chisq_mn(' + funcargs + '):\n'
> funcdef += 'global g_x, g_y, g_dy\n'
> funcdef += 'return chisq(['+funcargs+'],g_x,g_y,g_dy)\n’ #chisq is
> defined in same file
> # evaluate string and build function
> print "funcdef=", funcdef
> exec funcdef in globals()
> 
> m = ThirdParty.setup_minimize(chisq_mn)
> for i in range(len(pars)):
> m.values[argnames[i]] = pars[i]
> m.errors[argnames[i]] = 0.1 * pars[i]
> return m
> # then we can define
> f = minimize(pars,x,y,dy,chisq_func)
> 
> Question 1:
> Is there a better way to accomplish (my hopefully clear) goals?

I think you are looking for closures:

def make_poly(coeff):
def poly(x):
return sum(c * x ** n for n, c in enumerate(coeff))
return poly

def minimize(x, y, dy):
def chisq_mn(*args):
return chisq(args, x, y, dy)
return chisq_mn


>>> def make_poly(coeff):
... def poly(x):
... return sum(c * x ** n for n, c in enumerate(coeff))
... return poly
... 
>>> p = make_poly([1, 0, 1])
>>> for i in range(-4, 5):
... x = i*.25
... print x, "->", p(x)
... 
-1.0 -> 2.0
-0.75 -> 1.5625
-0.5 -> 1.25
-0.25 -> 1.0625
0.0 -> 1.0
0.25 -> 1.0625
0.5 -> 1.25
0.75 -> 1.5625
1.0 -> 2.0


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


Re: [Tutor] when is "pythondontwritebytecode" useful?

2014-02-17 Thread Ben Finney
Albert-Jan Roskam  writes:

> I know what it does
> (http://docs.python.org/2/using/cmdline.html#envvar-PYTHONDONTWRITEBYTECODE),
> i.e. no pyc or pyo fiules are written, but WHY is that sometimes a
> good thing?

There are numerous reasons why one might not want files to suddenly be
written when a source file gets compiled.

> The only useful scenario I can think of is when you don't have write
> rights to create pyc files but you want to use a package anyway.

Another reason: The file is named such that adding the letter “c” at the
end is unhelpful.

When writing a program to be invoked by a command name, on Unix the
convention is to name the program file with the name of the command. So
the command “foo” is implemented in a file named ‘foo’, no suffix.

This program file needs unit tests, and the test runner will import that
file. At which point Python will compile it, and normally create a
bytecode file. The bytecode file will be named by appending “c” to the
name of the source file, resulting in the filename ‘fooc’.

That filename makes no sense, so I want to disable the writing of that
bytecode file when that source file is imported.


That is just one example, of the more general case that one doesn't want
a file arbitrarily appearing in a directory with properties partially
outside one's control. Can you think of others?

-- 
 \ “Buy not what you want, but what you need; what you do not need |
  `\ is expensive at a penny.” —Cato, 234–149 BCE, Relique |
_o__)  |
Ben Finney

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


Re: [Tutor] constructing semi-arbitrary functions

2014-02-17 Thread Oscar Benjamin
On 17 February 2014 20:13, Peter Otten <__pete...@web.de> wrote:
> André Walker-Loud  wrote:
>>
>> The 3rd party minimizer utilizes the .func_code.co_varnames and
>> .func_code.co_argcount to determine the name and number of variables to
>> minimize.  eg.
>>
>> =
>> METHOD 2: use strings, exec and globals to construct function
>> def minimize(pars,x,y,dy):
>> global g_x, g_y, g_dy
>> g_x = x; g_y = y; g_dy = dy
>> argnames = ['c_%i'%(i) for i in range(len(pars))]
>> funcargs = ", ".join(argnames)
>> funcdef = 'def chisq_mn(' + funcargs + '):\n'
>> funcdef += 'global g_x, g_y, g_dy\n'
>> funcdef += 'return chisq(['+funcargs+'],g_x,g_y,g_dy)\n' #chisq is
>> defined in same file
>> # evaluate string and build function
>> print "funcdef=", funcdef
>> exec funcdef in globals()
>
> I think you are looking for closures:

Closures would be a good fit for this if the third-party minimisation
routine didn't assume that it always receives a function whose formal
parameters are the variables to be minimised and have the names that
are intended for display.

This one would work (assuming that we want to minimise over x):

> def make_poly(coeff):
> def poly(x):
> return sum(c * x ** n for n, c in enumerate(coeff))
> return poly

This one won't work:

> def minimize(x, y, dy):
> def chisq_mn(*args):
> return chisq(args, x, y, dy)
> return chisq_mn

>>> def minimize(x, y, dy):
... def chisq_mn(*args):
... return chisq(args, x, y, dy)
... return chisq_mn
...
>>> f = minimize(2, 4, 0.1)
>>> f.func_code.co_varnames
('args',)
>>> f.func_code.co_argcount
0

The OP wants to pass this function to a routine that looks like:

def minimise(func):
num = func.func_code.co_argcount
names = func.func_code.co_varnames
# Actual minimisation code
return _minimise(func, num, names)

If it's as simple as above then it may be straightforward to just
import the underlying _minimise routine and rewrite the minimise
function so that it looks like:

def minimise(func, parameter_names=None):
if parameter_names is not None:
num = func.func_code.co_argcount
names = func.func_code.co_varnames
else:
num = len(parameter_names)
# Actual minimisation code
return _minimise(func, num, names)


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


Re: [Tutor] constructing semi-arbitrary functions

2014-02-17 Thread André Walker-Loud
Hi Oscar,

Let me clear up my description of one point - I don’t want to pick on the third 
party software guys.

> The "right" solution is to change the interface of the third party function. 
> It is poorly designed and should not be inspecting those function attributes 
> or it should at least provide an option for you to provide that information 
> in a different way. Assuming it's open source you could try sending a patch. 
> In the mean time you could copy that code and modify it to suit your purposes.

The issue has to do with making a general polynomial function, not passing the 
args of a “simple” function.  If I define

def f(c_0,c_1):
return chisq(c_0,c_1,x,y,dy)

there is no problem using this function, the variables are all passed correctly.

What I am trying to avoid is having to write a special case for each order of 
polynomial I want.  I tried the following

def poly(x,pars):
val = 0.
for i,ci in enumerate(pars):
val += x**i * ci
return val
def f_lambda(x,pars):
return lambda x,*pars: poly(x,*pars)
x = np.array([1,2,3])
f = f_lambda(x,[-.5,2.])
f(x,[-.5,2.])
array([ 1.5,  3.5,  5.5])

f(x,[-.5,3.])
array([ 2.5,  5.5,  8.5])
etc. 

This function correctly gives me a polynomial of arbitrary order, but

f.func_code.co_varnames
('x', 'pars’)

so I can not pass this to the third party minimizer, as it does not know how to 
interpret ‘pars'.  You can probably see trying to replicate the lambda function 
method for an arbitrary polynomial is what naturally led to the string/exec 
option.


Thanks,

Andre

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


Re: [Tutor] constructing semi-arbitrary functions

2014-02-17 Thread Steven D'Aprano
On Mon, Feb 17, 2014 at 07:58:00PM +, Oscar Benjamin wrote:

> The "right" solution is to change the interface of the third party
> function. It is poorly designed and should not be inspecting those function
> attributes or it should at least provide an option for you to provide that
> information in a different way. Assuming it's open source you could try
> sending a patch. In the mean time you could copy that code and modify it to
> suit your purposes.

This may be a good case for some of the Design Patterns so popular in 
the Java world.

I think what you want is an Adaptor:

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

only written for a function rather than a class.



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


Re: [Tutor] constructing semi-arbitrary functions

2014-02-17 Thread Dave Angel
 Peter Otten <__pete...@web.de> Wrote in message:
> André Walker-Loud  wrote:
> 
>> Hello python tutors,
>> 
>> I am utilizing a 3rd party numerical minimization routine.  This routine
>> requires an input function, which takes as arguments, only the variables
>> with which to solve for.  But I don’t want to define all possible input
>> functions, in a giant switch, but rather, if I know I am fitting a
>> polynomial, I would like to just pass a list of parameters and have the
>> code know how to construct this function.
>> 
>> To construct for example, a chisq function, you must pass not only the
>> variables to solve for, but also the data, uncertainties, and perhaps
>> other arguments.  So it requires a little hacking to get it to work.  With
>> the help of my friends and looking at similar code, I have come up with
>> two ways that work under my simple test cases, and I have a few questions
>> about them.
>> 
>> The 3rd party minimizer utilizes the .func_code.co_varnames and
>> .func_code.co_argcount to determine the name and number of variables to
>> minimize.  eg.
>> 
>>> g = lambda x,c_0,c_1: c_0 + c_1 * x
>>> g.func_code.co_varnames
>> ('x', 'c_0', 'c_1’)
>>> g.func_code.co_argcount
>> 3
>> 
>> so what is needed is a function
>>> def f(c_0,c_1):
>>> …#construct chi_sq(c_0,c_1,x,y,…)
>> 
>> 
>> 
>> Question 1:
>> Is there a better way to accomplish (my hopefully clear) goals?
> 
> I think you are looking for closures:
> 
> def make_poly(coeff):

> 

I would also recommend closures,  but the particular case seems to
 fit partial pretty well. 


 http://docs.python.org/2/library/functools.html#functools.partial


-- 
DaveA

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


[Tutor] Assignment, references, binding (was: is an alias a variable)

2014-02-17 Thread Ben Finney
Ian D  writes:

> is 
> import longModuleName  as lmn
>  
> or 
>  
> lmn = longModuleName
>  
> creating an alias or assigning to a variable. or both?

Assignment and import both bind a name to an object. NAmes are one
(common) kind of reference. The only way to get at objects is by using a
reference.

So, this distinction you're drawing doesn't exist in Python. When you
use a name, you're using a reference to an object.

When you assign (or, when ‘import’ assigns) the same object to a
different name, the names are both references to the same object,
without any name having a special status.

-- 
 \ “True greatness is measured by how much freedom you give to |
  `\  others, not by how much you can coerce others to do what you |
_o__)   want.” —Larry Wall |
Ben Finney

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


Re: [Tutor] constructing semi-arbitrary functions

2014-02-17 Thread Oscar Benjamin
On 17 February 2014 21:18, "André Walker-Loud "
 wrote:
>
> What I am trying to avoid is having to write a special case for each order of 
> polynomial I want.  I tried the following
>
> def poly(x,pars):
> val = 0.
> for i,ci in enumerate(pars):
> val += x**i * ci
> return val
> def f_lambda(x,pars):
> return lambda x,*pars: poly(x,*pars)
> x = np.array([1,2,3])
> f = f_lambda(x,[-.5,2.])
> f(x,[-.5,2.])
> array([ 1.5,  3.5,  5.5])
>
> f(x,[-.5,3.])
> array([ 2.5,  5.5,  8.5])
> etc.
>
> This function correctly gives me a polynomial of arbitrary order, but
>
> f.func_code.co_varnames
> ('x', 'pars')
>
> so I can not pass this to the third party minimizer, as it does not know how 
> to interpret 'pars'.  You can probably see trying to replicate the lambda 
> function method for an arbitrary polynomial is what naturally led to the 
> string/exec option.

This particular case is easily solved:

def f_lambda(x,pars):
 return lambda x: poly(x,*pars)

You let the closure take care of pars and return a function that takes
exactly one argument x.


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


Re: [Tutor] constructing semi-arbitrary functions

2014-02-17 Thread André Walker-Loud
> This particular case is easily solved:
> 
> def f_lambda(x,pars):
> return lambda x: poly(x,*pars)
> 
> You let the closure take care of pars and return a function that takes
> exactly one argument x.

Hi Oscar,

This is the opposite of what I am trying to do.  In the example, x represents 
the data and pars represent the parameters I want to determine, so it is the 
pars which I need passed into the “func_code.co_varnames” part of f.

Maybe your suggestion gets me in that direction, but I don’t see how.

Thanks,

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


Re: [Tutor] constructing semi-arbitrary functions

2014-02-17 Thread Oscar Benjamin
On 17 February 2014 22:15, "André Walker-Loud "
 wrote:
>> This particular case is easily solved:
>>
>> def f_lambda(x,pars):
>> return lambda x: poly(x,*pars)
>>
>> You let the closure take care of pars and return a function that takes
>> exactly one argument x.
>
> Hi Oscar,
>
> This is the opposite of what I am trying to do.  In the example, x represents 
> the data and pars represent the parameters I want to determine, so it is the 
> pars which I need passed into the "func_code.co_varnames" part of f.
>
> Maybe your suggestion gets me in that direction, but I don't see how.

No, you're right. I misunderstood this example.

Are you able to see/alter the source code of the 3rd party function?
As I said earlier my preferred solution would be to rewrite the
outermost part of that.

The core inner minimisation routine will (I'm guessing) be something
that really doesn't care about the names of these parameters and just
needs to know the dimensionality of the space it is exploring. If you
can access that routine directly then you can bypass the (IMO
unfortunate) interface that you're currently trying to contort your
problems into.


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


Re: [Tutor] constructing semi-arbitrary functions

2014-02-17 Thread Oscar Benjamin
On 17 February 2014 22:15, "André Walker-Loud "
 wrote:
>> This particular case is easily solved:
>>
>> def f_lambda(x,pars):
>> return lambda x: poly(x,*pars)
>>
>> You let the closure take care of pars and return a function that takes
>> exactly one argument x.
>
> Hi Oscar,
>
> This is the opposite of what I am trying to do.  In the example, x represents 
> the data and pars represent the parameters I want to determine, so it is the 
> pars which I need passed into the "func_code.co_varnames" part of f.

BTW if you're trying to fit the coefficients of a polynomial then a
general purpose optimisation function is probably not what you want to
use. I would probably solve (in a least squares sense and after
suitable scaling) the Vandermonde matrix.

(I can explain that more if desired.)


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


Re: [Tutor] constructing semi-arbitrary functions

2014-02-17 Thread André Walker-Loud
Hi Oscar,

On Feb 17, 2014, at 6:02 PM, Oscar Benjamin  wrote:

> On 17 February 2014 22:15, "André Walker-Loud "
>  wrote:
>>> This particular case is easily solved:
>>> 
>>> def f_lambda(x,pars):
>>>return lambda x: poly(x,*pars)
>>> 
>>> You let the closure take care of pars and return a function that takes
>>> exactly one argument x.
>> 
>> Hi Oscar,
>> 
>> This is the opposite of what I am trying to do.  In the example, x 
>> represents the data and pars represent the parameters I want to determine, 
>> so it is the pars which I need passed into the "func_code.co_varnames" part 
>> of f.
>> 
>> Maybe your suggestion gets me in that direction, but I don't see how.
> 
> No, you're right. I misunderstood this example.
> 
> Are you able to see/alter the source code of the 3rd party function?
> As I said earlier my preferred solution would be to rewrite the
> outermost part of that.
> 
> The core inner minimisation routine will (I'm guessing) be something
> that really doesn't care about the names of these parameters and just
> needs to know the dimensionality of the space it is exploring. If you
> can access that routine directly then you can bypass the (IMO
> unfortunate) interface that you're currently trying to contort your
> problems into.

Thanks for the advice.  At the moment, that sounds a bit too daunting to look 
into, but worth the effort in the long run.  The code is all available.  It is 
in fact a python wrapper around a sophisticated c++ minimizer (Minuit if you’ve 
heard of it).  I am not sure if the python wrapper design was just a choice, or 
forced upon the designer by the interface to Minuit.  That will involve a bit 
of research.


Thanks,

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


Re: [Tutor] constructing semi-arbitrary functions

2014-02-17 Thread André Walker-Loud
Hi Oscar,


On Feb 17, 2014, at 7:03 PM, Oscar Benjamin  wrote:

> On 17 February 2014 22:15, "André Walker-Loud "
>  wrote:
>>> This particular case is easily solved:
>>> 
>>> def f_lambda(x,pars):
>>>return lambda x: poly(x,*pars)
>>> 
>>> You let the closure take care of pars and return a function that takes
>>> exactly one argument x.
>> 
>> Hi Oscar,
>> 
>> This is the opposite of what I am trying to do.  In the example, x 
>> represents the data and pars represent the parameters I want to determine, 
>> so it is the pars which I need passed into the "func_code.co_varnames" part 
>> of f.
> 
> BTW if you're trying to fit the coefficients of a polynomial then a
> general purpose optimisation function is probably not what you want to
> use. I would probably solve (in a least squares sense and after
> suitable scaling) the Vandermonde matrix.
> 
> (I can explain that more if desired.)

That looks interesting (just reading the wikipedia entry on the Vandermonde 
matrix).
Is there a good algorithm for picking the values the polynomial is evaluated at 
to construct the matrix?
Is there a benefit to this method vs a standard linear least squares?
Given the Vandermonde matrix, how do you determine the uncertainty in the 
resulting parameters?
I guess yes, I am interested to learn more.

The most common problem I am solving is fitting a sum of real exponentials to 
noisy data, with the model function

C(t) = sum_n A_n e^{- E_n t}

the quantities of most interest are E_n, followed by A_n so I solve this with 
non-linear regression.
To stabilize the fit, I usually do a linear-least squares for the A_n first, 
solving as a function of the E_n, and then do a non-linear fit for the E_n.  
Often, we construct multiple data sets which share values of E_n but have 
different A_n, where A_n is promoted to a matrix, sometimes square, sometimes 
not.  So I want a wrapper on my chisq which can take the input parameter values 
and deduce the fit function, and pass the variable names to my current 
minimizer - hence my original question, and why I don’t want to write a special 
case for each possible combination of n_max, and the shape of A_n.


Cheers,

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