Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread Andreas Perstinger

On 2011-10-08 08:25, lina wrote:

 Still have a reading "multiple" files issue:


 Traceback (most recent call last):
   File "counter-vertically-WORKING.**py", line 26, in
 results[ch][col]+=1
 IndexError: list index out of range

 only one file ss_1.xpm was processed and wrote file, for the rest
 ss_2.xpm,
 ss_3.xpm and following keep intact,

 any suggestions?


https://docs.google.com/leaf?id=0B93SVRfpVVg3MjIxZGI5YWQtN2NiYy00MWFkLWE0ZTctNjExNGJiNjYzYjQw&hl=en_GB

https://docs.google.com/leaf?id=0B93SVRfpVVg3YjQ1ZWRkMjUtNmZhYS00MTA4LTg3YTktY2JjYzEyMDU5OTI2&hl=en_GB

https://docs.google.com/leaf?id=0B93SVRfpVVg3YjQ1ZWRkMjUtNmZhYS00MTA4LTg3YTktY2JjYzEyMDU5OTI2&hl=en_US

you may rename ss_1.xpm to the ss_2 or 3, keep them the same is okay, just
different names.


Ok, I just ran your script with this files and don't get an error. 
Except some stylistic issues the code looks fine. The resulting 
txt-files still have the problem with the leading '[' and trailing ']' 
but I've already suggested a solution earlier today.


Do you still get an error?

Bye, Andreas

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


Re: [Tutor] swapping list elements based on some criterion

2011-10-07 Thread عمـ نوفل ـاد
2011/10/8 Emad Nawfal (عمـ نوفل ـاد) 

> Hi Steven ans Tutors,
> Thanks for the code.
> Actually I'm not a student at all. I'm an assistant professor of Arabic at
> Suez Canal University in Egypt. I would appreciate more contributions from
> you and the other list members.
>
>
> 2011/10/8 Steven D'Aprano 
>
>> Emad Nawfal (عمـ نوفل ـاد) wrote:
>>
>>> Hello Tutors,
>>> It's been quite some time since I last posted something here, and now I'm
>>> back with a question:
>>>
>>> I want to re-structure English so that the adjectives appear after the
>>> nouns, instead of before.
>>> If I have a sentence like:
>>>
>>> The tall man plays well
>>> I need to change it to
>>> The man tall plays well
>>> So far, I have thought of the following function, but I'm not sure it's
>>> the
>>> best solution. It assumes that the sentence has been augmented with part
>>> of
>>> speech tags
>>> (Also plz note that there may be multiple adjectives)
>>>
>>
>> And what do you expect to happen with multiple adjectives?
>>
>> "the tall fat bald man"
>>
>> => "the man tall fat bald"
>> => "the man fat tall bald"
>> => "the man bald fat tall"
>> => "the man fat bald tall"
>> => something else?
>>
>> The approach I would take is this:
>>
>>
>> Walk along the sentence, inspecting each word.
>> Collect all consecutive adjectives into a list of adjectives
>> Other words get copied straight into the buffer.
>> When you reach a noun, copy the noun into the buffer, plus the list
>>  of adjectives (in whichever order you like!), then clear the list
>>  of adjectives ready for the next noun.
>>
>> Does that help?
>>
>> Something like this:
>>
>> def swap(sentence):
>>buffer = []
>>adjectives = []
>>for word in sentence.split():
>>if word.endswith('/ADJ'):
>>adjectives.append(word)
>>elif word.endswith('/N'):
>>buffer.extend(adjectives)
>>buffer.append(word)
>>adjectives = []
>>else:
>>buffer.append(word)
>>return ' '.join(buffer)
>>
>>
>> P.S. Since this looks like homework, I have left a deliberate mistake in
>> my code for you to fix. But it should be easy to fix if you study the code
>> and think about what it is doing.
>>
>>
>> --
>> Steven
>> __**_
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/**mailman/listinfo/tutor
>>
>
>
>
> --
> لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
> الغزالي
> "No victim has ever been more repressed and alienated than the truth"
>
> Emad Soliman Nawfal
> Indiana University, Bloomington
> 
>

Hi Steven,
Thanks again for the recipe and for leaving an error in the function. This
forced me to use my mind a little bit.

Here is the function as I used it, and it works fine:

def swap(sentence):
   buffer = []
   adjectives = []
   for word in sentence.split():
   if word.endswith('/ADJ'):
   adjectives.append(word)
   elif word.endswith('/N'):
   buffer.append(word)
   buffer.extend(adjectives)

   adjectives = []
   else:
   buffer.append(word)
   return ' '.join(buffer)

and SORRY for top-posting in the previous email.
-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington

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


Re: [Tutor] swapping list elements based on some criterion

2011-10-07 Thread عمـ نوفل ـاد
Hi Steven ans Tutors,
Thanks for the code.
Actually I'm not a student at all. I'm an assistant professor of Arabic at
Suez Canal University in Egypt. I would appreciate more contributions from
you and the other list members.

2011/10/8 Steven D'Aprano 

> Emad Nawfal (عمـ نوفل ـاد) wrote:
>
>> Hello Tutors,
>> It's been quite some time since I last posted something here, and now I'm
>> back with a question:
>>
>> I want to re-structure English so that the adjectives appear after the
>> nouns, instead of before.
>> If I have a sentence like:
>>
>> The tall man plays well
>> I need to change it to
>> The man tall plays well
>> So far, I have thought of the following function, but I'm not sure it's
>> the
>> best solution. It assumes that the sentence has been augmented with part
>> of
>> speech tags
>> (Also plz note that there may be multiple adjectives)
>>
>
> And what do you expect to happen with multiple adjectives?
>
> "the tall fat bald man"
>
> => "the man tall fat bald"
> => "the man fat tall bald"
> => "the man bald fat tall"
> => "the man fat bald tall"
> => something else?
>
> The approach I would take is this:
>
>
> Walk along the sentence, inspecting each word.
> Collect all consecutive adjectives into a list of adjectives
> Other words get copied straight into the buffer.
> When you reach a noun, copy the noun into the buffer, plus the list
>  of adjectives (in whichever order you like!), then clear the list
>  of adjectives ready for the next noun.
>
> Does that help?
>
> Something like this:
>
> def swap(sentence):
>buffer = []
>adjectives = []
>for word in sentence.split():
>if word.endswith('/ADJ'):
>adjectives.append(word)
>elif word.endswith('/N'):
>buffer.extend(adjectives)
>buffer.append(word)
>adjectives = []
>else:
>buffer.append(word)
>return ' '.join(buffer)
>
>
> P.S. Since this looks like homework, I have left a deliberate mistake in my
> code for you to fix. But it should be easy to fix if you study the code and
> think about what it is doing.
>
>
> --
> Steven
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>



-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington

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


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread lina
> Still have a reading "multiple" files issue:
>>
>> Traceback (most recent call last):
>>   File "counter-vertically-WORKING.**py", line 26, in
>> results[ch][col]+=1
>> IndexError: list index out of range
>>
>> only one file ss_1.xpm was processed and wrote file, for the rest
>> ss_2.xpm,
>> ss_3.xpm and following keep intact,
>>
>> any suggestions?
>>
>
> Could you provide links to your current script file
> (counter-vertically-WORKING.**py) and your data files(ss_1.xpm, ss_2.xpm,
> ...)?
>

https://docs.google.com/leaf?id=0B93SVRfpVVg3MjIxZGI5YWQtN2NiYy00MWFkLWE0ZTctNjExNGJiNjYzYjQw&hl=en_GB

https://docs.google.com/leaf?id=0B93SVRfpVVg3YjQ1ZWRkMjUtNmZhYS00MTA4LTg3YTktY2JjYzEyMDU5OTI2&hl=en_GB

https://docs.google.com/leaf?id=0B93SVRfpVVg3YjQ1ZWRkMjUtNmZhYS00MTA4LTg3YTktY2JjYzEyMDU5OTI2&hl=en_US

you may rename ss_1.xpm to the ss_2 or 3, keep them the same is okay, just
different names.

Thanks,



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


Re: [Tutor] vcf_files and strings

2011-10-07 Thread Andreas Perstinger

[I have already answered your first post but it seems you missed it]

On 2011-10-07 18:12, Anna Olofsson wrote:


Hi,

I'm a beginner at Python and would really appreciate some help in how
to extract information from a vcf file.


What does "beginner" mean?
Do you have experience in other languages?
Do you understand how different datatypes work (strings, integers,
lists, dictionaries, ...)?
Do you know the basic programming concepts (for-loops, if-then-else
conditions, ...)?


The important column is 7 where the ID is, i.e.
refseq.functionalClass=missense. It's a missense mutation, so then I
want to extract refseq.name=NM_003137492, or I want to extract only
the ID, which in this case is NM_003137492.

Then I want to do exactly the same thing for all the other mutations,
but only for the missense mutations not the other ones. How do I
accomplish that? Where do I start?


Can you show us some code snippets of your attempts?

Bye, Andreas

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


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread Andreas Perstinger

On 2011-10-08 06:07, lina wrote:

Still have a reading "multiple" files issue:

Traceback (most recent call last):
   File "counter-vertically-WORKING.py", line 26, in
 results[ch][col]+=1
IndexError: list index out of range

only one file ss_1.xpm was processed and wrote file, for the rest ss_2.xpm,
ss_3.xpm and following keep intact,

any suggestions?


Could you provide links to your current script file 
(counter-vertically-WORKING.py) and your data files(ss_1.xpm, ss_2.xpm, 
...)?


Bye, Andreas

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


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread Andreas Perstinger

On 2011-10-08 05:34, lina wrote:

Another minor derived questions:

 summary=[]
 for a,b in zip(results['E'],results['B']):
 summary.append(a+b)## now the summary is '[0,1, 3, 5,
6,0,0,0]'
 del summary[0]   ## here I wanna remove the first zero, which came
from the initial double quote "EB...E",
 summary.pop()## here I wanna remove the last three zeros
 summary.pop()
 summary.pop()
 print(summary)   ### output is [1,3,5,6]
 summary='\n'.join(str(summary).split(','))  ### wish the result in
one column,
 with open(base+OUTFILEEXT,"w") as f:
 f.write(str(summary))

the final result in the output.txt file is:
[1
3
5
6]

Q1: how can I remove the [1 "[" to only keep 1?


That's a job for a list comprehension (read 
http://docs.python.org/py3k/tutorial/datastructures.html#list-comprehensions 
for a short introduction):


new_summary = '\n'.join([str(element) for element in summary])


Q2 how can I improve above expressions?


-) As above, you could use a list comprehension for creating your 
summary list:


summary = [(a + b) for a, b in zip(results['E'], results['B'])]

-) Instead of

del summary[0]
summary.pop()
summary.pop()
summary.pop()

I suggest to slice your list:

summary = summary[1:-3]

But this assumes that your summary list will always have one leading 
zero and 3 trailing zeros.


-) In your last line

with open(base+OUTFILEEXT,"w") as f:
 f.write(str(summary))

you don't need to convert summary to a string because it is already one.

Bye, Andreas

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


Re: [Tutor] swapping list elements based on some criterion

2011-10-07 Thread Steven D'Aprano

Emad Nawfal (عمـ نوفل ـاد) wrote:

Hello Tutors,
It's been quite some time since I last posted something here, and now I'm
back with a question:

I want to re-structure English so that the adjectives appear after the
nouns, instead of before.
If I have a sentence like:

The tall man plays well
I need to change it to
The man tall plays well
So far, I have thought of the following function, but I'm not sure it's the
best solution. It assumes that the sentence has been augmented with part of
speech tags
(Also plz note that there may be multiple adjectives)


And what do you expect to happen with multiple adjectives?

"the tall fat bald man"

=> "the man tall fat bald"
=> "the man fat tall bald"
=> "the man bald fat tall"
=> "the man fat bald tall"
=> something else?

The approach I would take is this:


Walk along the sentence, inspecting each word.
Collect all consecutive adjectives into a list of adjectives
Other words get copied straight into the buffer.
When you reach a noun, copy the noun into the buffer, plus the list
  of adjectives (in whichever order you like!), then clear the list
  of adjectives ready for the next noun.

Does that help?

Something like this:

def swap(sentence):
buffer = []
adjectives = []
for word in sentence.split():
if word.endswith('/ADJ'):
adjectives.append(word)
elif word.endswith('/N'):
buffer.extend(adjectives)
buffer.append(word)
adjectives = []
else:
buffer.append(word)
return ' '.join(buffer)


P.S. Since this looks like homework, I have left a deliberate mistake in 
my code for you to fix. But it should be easy to fix if you study the 
code and think about what it is doing.



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


[Tutor] swapping list elements based on some criterion

2011-10-07 Thread عمـ نوفل ـاد
Hello Tutors,
It's been quite some time since I last posted something here, and now I'm
back with a question:

I want to re-structure English so that the adjectives appear after the
nouns, instead of before.
If I have a sentence like:

The tall man plays well
I need to change it to
The man tall plays well
So far, I have thought of the following function, but I'm not sure it's the
best solution. It assumes that the sentence has been augmented with part of
speech tags
(Also plz note that there may be multiple adjectives)

##
test = "the/DET tall/ADJ man/N plays/VBZ well/ADV"

def swapAdjNoun(sentence):
sentence = sentence.split()
new = []

x = enumerate(sentence)
for idx, lex in x:
if lex.endswith("/N") and sentence[idx-1].endswith("/ADJ"):
idx = idx - 1
elif lex.endswith("/ADJ") and sentence[idx+1].endswith("/N"):
idx = idx + 1
new.append((idx, lex))
new.sort()
new = [ elm[1] for elm in new]
return  new



I'd appreciate if if you  gave me some advice on how to do this the right
way.
-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal


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


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread lina
Still have a reading "multiple" files issue:

Traceback (most recent call last):
  File "counter-vertically-WORKING.py", line 26, in 
results[ch][col]+=1
IndexError: list index out of range

only one file ss_1.xpm was processed and wrote file, for the rest ss_2.xpm,
ss_3.xpm and following keep intact,

any suggestions?

thanks, and sorry for so many questions here,
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread lina
Another minor derived questions:

summary=[]
for a,b in zip(results['E'],results['B']):
summary.append(a+b)## now the summary is '[0,1, 3, 5,
6,0,0,0]'
del summary[0]   ## here I wanna remove the first zero, which came
from the initial double quote "EB...E",
summary.pop()## here I wanna remove the last three zeros
summary.pop()
summary.pop()
print(summary)   ### output is [1,3,5,6]
summary='\n'.join(str(summary).split(','))  ### wish the result in
one column,
with open(base+OUTFILEEXT,"w") as f:
f.write(str(summary))

the final result in the output.txt file is:
[1
3
5
6]

Q1: how can I remove the [1 "[" to only keep 1?

Q2 how can I improve above expressions?

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


Re: [Tutor] Runtime error while Test data creation

2011-10-07 Thread bob gailer

On 10/7/2011 5:32 PM, Guess?!? wrote:

A couple of suggestion below (inline with the code):



import pyodbc, random, datetime, uuid

#INT
conn = pyodbc.connect('DRIVER={SQL

Server};SERVER=SERVERNAME\INT_FOUNDATIONS;DATABASE=membership_service;UID=int_usr;PWD=blah')


c = conn.cursor()

# "enterprise_person_id" TEXT NOT NULL,

# read file
f = open('C:\\Users\\garora\\Desktop\\INTPPLHALFMIL.csv','r')

# Hardcoded valid seed values

# INT
programId = 50801
packageId = 46101
pkg_optionId = 40301


seed_data = "TEST_DATA"

numberPrePend = "-"

create_dt = datetime.datetime.now();
modified_dt = datetime.datetime.now();

i = 0



f.readline() # to dispense with first line




for each in f:
  (id, entrpid,) = each.split(',')
  print id
  id = id.replace('\n', '')
  print id
  id = uuid.UUID(id)
  #enterprise_person_id
  print entrpid
  entrpid = entrpid.replace('\n', '')
  print entrpid
  entrpid = uuid.UUID(entrpid)
  #random values for few columns
  statusRandom = random.randint(1,4)
  sourceRandom = random.randint(1,7)
  inputChannelRandom = random.randint(1,5)
  marketRandom= random.randint(1,10)
  trialRandom = random.randint(0,1)
  monthRandom = random.randint(3,12) # prevent month of feb
  dateRandom = random.randint(1,30) # prevent 31st on invalid month
  yearRandom = random.randint(2007,2010)
  member_start_dt = datetime.datetime(yearRandom, monthRandom,
dateRandom,0,0,0)
  current_term_start_dt = datetime.datetime(yearRandom,
monthRandom, dateRandom,0,0,0)

  print "id: " + str(i)



print id, i # same effect, less code. apply to following lines also



print " programId: " + str(programId)
print " packageId: " + str(packageId)
print " pkgoptionId: " + str(pkg_optionId)
print " status id: " + str(statusRandom)
print " source id: " + str(sourceRandom)
print " input channel id: " + str(inputChannelRandom)
print " market id: " + str(marketRandom)
print " person id: " + str(id)
print "membership id: " + numberPrePend + str(i)
print " member_start_dt " + str(member_start_dt)
print " current_term_start_dt " + str(current_term_start_dt)
print " started_as_trial " + str(trialRandom)
print " enterprise_person_id " + str(entrpid)

record = (i, programId, packageId, pkg_optionId, statusRandom,
sourceRandom, inputChannelRandom,
marketRandom, str(id), numberPrePend + str(i), member_start_dt,
current_term_start_dt, trialRandom
,seed_data, seed_data, create_dt, modified_dt, str(entrpid))
print record
c.execute("""insert into Members(id, membership_program_id,
membership_package_id,
membership_pkg_option_id, membership_status_id,
membership_source_id, src_input_channel_id,
src_market_id, person_id_ref, membership_id, member_start_dt,
current_term_start_dt, started_as_trial,
created_by,  modified_by, create_dt, modified_dt,
enterprise_person_id)
values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)""", record)
conn.commit()
i = i + 1

c.close()



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



-- 
Bob Gailer

919-636-4239  
Chapel Hill NC





--
Bob Gailer
919-636-4239
Chapel Hill NC

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


Re: [Tutor] Runtime error while Test data creation

2011-10-07 Thread bob gailer

On 10/7/2011 5:32 PM, Guess?!? wrote:

Hey Bob,

Thanks for the suggestions. There is no traceback/stacktrace errors 
that I can see (may be there is a file that is generated in python or 
windows directory due to run time failure but I am not able to locate 
it. Please let me know if you know).


The run time error pops up in a windows dialog box with a text I wrote 
below in my first email and closing the dialog box closes the IDLE GUI 
python.


Of course. I should have seen that from your original post.

My guess is that the problem is raised by the insert. Try commenting 
that out and see if the program runs to completion.


On Fri, Oct 7, 2011 at 1:35 PM, bob gailer > wrote:


On 10/7/2011 2:19 PM, Guess?!? wrote:

Hello all,

I am trying to create some test data for a search module that I
am building. Since I dont want to hit performance related issues
late in the game, I decided to create half a million records in
the DB.

My approach is create a csv file with valid UUIDs which I already
have. Read each record from csv and generate random data and do
DB insert as long the csv file has records in them. This crashes
near 10 record insertions.

The problem is that my program eventually fails with this error.
Any ideas what I can improve to make it go all the way?

Runtime Error!
Program: C:\Python27\pythonw.exe
This app has requested the Runtime to terminate it in an unusual
way. Please contact application support team

Here is the code that I wrote to do this.

===
import pyodbc, random, datetime, uuid

#INT
conn = pyodbc.connect('DRIVER={SQL

Server};SERVER=SERVERNAME\INT_FOUNDATIONS;DATABASE=membership_service;UID=int_usr;PWD=blah')


c = conn.cursor()

# "enterprise_person_id" TEXT NOT NULL,

# read file
f = open('C:\\Users\\garora\\Desktop\\INTPPLHALFMIL.csv','r')

# Hardcoded valid seed values

# INT
programId = 50801
packageId = 46101
pkg_optionId = 40301


seed_data = "TEST_DATA"

numberPrePend = "-"

create_dt = datetime.datetime.now();
modified_dt = datetime.datetime.now();

i = 0

for each in f:
if (i == 0):
i = i + 1
continue;
(id, entrpid,) = each.split(',')
print id
id = id.replace('\n', '')
print id
id = uuid.UUID(id)
#enterprise_person_id
print entrpid
entrpid = entrpid.replace('\n', '')
print entrpid
entrpid = uuid.UUID(entrpid)
#random values for few columns
statusRandom = random.randint(1,4)
sourceRandom = random.randint(1,7)
inputChannelRandom = random.randint(1,5)
marketRandom= random.randint(1,10)
trialRandom = random.randint(0,1)
monthRandom = random.randint(3,12) # prevent month of feb
dateRandom = random.randint(1,30) # prevent 31st on invalid month
yearRandom = random.randint(2007,2010)
member_start_dt = datetime.datetime(yearRandom, monthRandom,
dateRandom,0,0,0)
current_term_start_dt = datetime.datetime(yearRandom, monthRandom,
dateRandom,0,0,0)

print "id: " + str(i)
print " programId: " + str(programId)
print " packageId: " + str(packageId)
print " pkgoptionId: " + str(pkg_optionId)
print " status id: " + str(statusRandom)
print " source id: " + str(sourceRandom)
print " input channel id: " + str(inputChannelRandom)
print " market id: " + str(marketRandom)
print " person id: " + str(id)
print "membership id: " + numberPrePend + str(i)
print " member_start_dt " + str(member_start_dt)
print " current_term_start_dt " + str(current_term_start_dt)
print " started_as_trial " + str(trialRandom)
print " enterprise_person_id " + str(entrpid)

record = (i, programId, packageId, pkg_optionId, statusRandom,
sourceRandom, inputChannelRandom,
marketRandom, str(id), numberPrePend + str(i), member_start_dt,
current_term_start_dt, trialRandom
,seed_data, seed_data, create_dt, modified_dt, str(entrpid))
print record
c.execute("""insert into Members(id, membership_program_id,
membership_package_id,
membership_pkg_option_id, membership_status_id,
membership_source_id, src_input_channel_id,
src_market_id, person_id_ref, membership_id, member_start_dt,
current_term_start_dt, started_as_trial,
created_by,  modified_by, create_dt, modified_dt,
enterprise_person_id)
values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)""", record)
conn.commit()
i = i + 1

c.close()




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



-- 
Bob Gailer

919-636-4239  
Chapel Hill NC





--
Bob Gailer
919-636-4239
Chapel Hill NC

__

Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread lina
> C is a great language for writing Operating Systems and other "near the
> metal" code. But its not the best language for busiess apps, artificial
> intelligence and a host of other things. Bash is a good user shell, but its
> not even the best Unix shell for scripting (Thats probably ksh).
> D? Who actually uses D? I think your informant was not well informed.
>
Next time when I saw my friend, I would ask, "have you met someone who has
actually started in use D yet?"
it's a joke, thanks for informing me that.

>
> AS fopr Python becoming obsolete? Maybe someday, but not soon.
> And even if it does the principles it teaches are alive and well in many
> other languages - eg, Ruby, Lua both openly borrow from Python.
> Just as Python borrows from Lisp and Haskell and Smalltalk and Perl.
>
>
> > I was further told that fortran is obsolete, but still lots of
> > fortran guys using it.
>
> Absolutely and Lisp and COBOL ghave both been predicted to die for decades
> but are still marching on. It is very difficult to kill a popular language
> because the thing that made it popular keeps it
> alive.
>

> Ignore rumours and focus on programming. Don't worry about languages, once
> you know one you'll usually pick up another very quickly. They come and go
> in fashion but the concepts remain constant. Most programmers work in
> multiple languages, often even in a single
> project.

Thanks again,

>
>
> --
> 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] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread lina


> The 'with' syntax above implicitly closes the file for you
> at the end of the block. If you use the
>
> f = open(...)
>
> style you are expected to explicitly close the file when you are finished
> with it. This is especially important when writing to a file because that
> will guarantee that the OS writes the data to the file on disk. Without a
> close its possible for your data to  get held in a buffer that doesn't get
> written to disk. Rare, but possible.
>
> Thanks. BTW, are there some tricks in using gedit input code.

such as I wanna backspace, but usually won't count 4 spaces, and sometimes I
wanna more back space,
like move:

>>> a
to
>>> a

the "prompt" sign here to give a reference position of a, I am interested in
gedit, not idle here.

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


Re: [Tutor] Runtime error while Test data creation

2011-10-07 Thread Prasad, Ramit
From: tutor-bounces+ramit.prasad=jpmorgan@python.org 
[mailto:tutor-bounces+ramit.prasad=jpmorgan@python.org] On Behalf Of 
Guess?!?
Sent: Friday, October 07, 2011 4:32 PM
To: bob gailer
Cc: tutor@python.org
Subject: Re: [Tutor] Runtime error while Test data creation

Hey Bob,

Thanks for the suggestions. There is no traceback/stacktrace errors that I can 
see (may be there is a file that is generated in python or windows directory 
due to run time failure but I am not able to locate it. Please let me know if 
you know). 

The run time error pops up in a windows dialog box with a text I wrote below in 
my first email and closing the dialog box closes the IDLE GUI python. 

Thanks,
G
==


I would change those print statements to write to file (each record should be 
one line) instead. Be sure to append the file and not overwrite and close the 
file before each insert statement. Then you can see how far it gets and try to 
insert the last row again to see if it is a user error. Otherwise I would think 
it is possibly a connection/memory issue. Try running the code with 1 row first 
and then increase the number of rows being written. 


Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423



This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Runtime error while Test data creation

2011-10-07 Thread Guess?!?
Hey Bob,

Thanks for the suggestions. There is no traceback/stacktrace errors that I
can see (may be there is a file that is generated in python or windows
directory due to run time failure but I am not able to locate it. Please let
me know if you know).

The run time error pops up in a windows dialog box with a text I wrote below
in my first email and closing the dialog box closes the IDLE GUI python.

Thanks,
G

On Fri, Oct 7, 2011 at 1:35 PM, bob gailer  wrote:

>  On 10/7/2011 2:19 PM, Guess?!? wrote:
>
> Hello all,
>
>  I am trying to create some test data for a search module that I am
> building. Since I dont want to hit performance related issues late in the
> game, I decided to create half a million records in the DB.
>
>  My approach is create a csv file with valid UUIDs which I already
> have. Read each record from csv and generate random data and do DB insert as
> long the csv file has records in them. This crashes near 10 record
> insertions.
>
>  The problem is that my program eventually fails with this error. Any
> ideas what I can improve to make it go all the way?
>
>  Runtime Error!
> Program: C:\Python27\pythonw.exe
> This app has requested the Runtime to terminate it in an unusual
> way. Please contact application support team
>
>
> Please just post ordinary text. No colors, no fancy formatting.
>
> Please always post the entire traceback so we tell where the code failed.
>
>
>  Here is the code that I wrote to do this.
>
>  ===
>  import pyodbc, random, datetime, uuid
>
>  #INT
> conn = pyodbc.connect('DRIVER={SQL
> Server};SERVER=SERVERNAME\INT_FOUNDATIONS;DATABASE=membership_service;UID=int_usr;PWD=blah')
>
>
>  c = conn.cursor()
>
>  # "enterprise_person_id" TEXT NOT NULL,
>
>  # read file
> f = open('C:\\Users\\garora\\Desktop\\INTPPLHALFMIL.csv','r')
>
>  # Hardcoded valid seed values
>
>  # INT
> programId = 50801
> packageId = 46101
> pkg_optionId = 40301
>
>
>  seed_data = "TEST_DATA"
>
>  numberPrePend = "-"
>
>  create_dt = datetime.datetime.now();
> modified_dt = datetime.datetime.now();
>
>  i = 0
>
>  for each in f:
>  if (i == 0):
>  i = i + 1
>  continue;
>   (id, entrpid,) = each.split(',')
>  print id
>  id = id.replace('\n', '')
>  print id
>  id = uuid.UUID(id)
>#enterprise_person_id
>  print entrpid
>  entrpid = entrpid.replace('\n', '')
>  print entrpid
>  entrpid = uuid.UUID(entrpid)
>#random values for few columns
>  statusRandom = random.randint(1,4)
>  sourceRandom = random.randint(1,7)
>  inputChannelRandom = random.randint(1,5)
>  marketRandom= random.randint(1,10)
>  trialRandom = random.randint(0,1)
>  monthRandom = random.randint(3,12) # prevent month of feb
>  dateRandom = random.randint(1,30) # prevent 31st on invalid month
>  yearRandom = random.randint(2007,2010)
>  member_start_dt = datetime.datetime(yearRandom, monthRandom,
> dateRandom,0,0,0)
>  current_term_start_dt = datetime.datetime(yearRandom, monthRandom,
> dateRandom,0,0,0)
>
>  print "id: " + str(i)
>  print " programId: " + str(programId)
>  print " packageId: " + str(packageId)
>  print " pkgoptionId: " + str(pkg_optionId)
>  print " status id: " + str(statusRandom)
>  print " source id: " + str(sourceRandom)
>  print " input channel id: " + str(inputChannelRandom)
>  print " market id: " + str(marketRandom)
>  print " person id: " + str(id)
>  print "membership id: " + numberPrePend + str(i)
>  print " member_start_dt " + str(member_start_dt)
>  print " current_term_start_dt " + str(current_term_start_dt)
>  print " started_as_trial " + str(trialRandom)
>  print " enterprise_person_id " + str(entrpid)
>
>  record = (i, programId, packageId, pkg_optionId, statusRandom,
> sourceRandom, inputChannelRandom,
>  marketRandom, str(id), numberPrePend + str(i), member_start_dt,
> current_term_start_dt, trialRandom
>  ,seed_data, seed_data, create_dt, modified_dt, str(entrpid))
>   print record
>   c.execute("""insert into Members(id, membership_program_id,
> membership_package_id,
>  membership_pkg_option_id, membership_status_id, membership_source_id,
> src_input_channel_id,
>  src_market_id, person_id_ref, membership_id, member_start_dt,
> current_term_start_dt, started_as_trial,
>  created_by,  modified_by, create_dt, modified_dt, enterprise_person_id)
>  values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)""", record)
>  conn.commit()
>  i = i + 1
>
>  c.close()
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription 
> options:http://mail.python.org/mailman/listinfo/tutor
>
>
>
> --
> Bob Gailer919-636-4239
> Chapel Hill NC
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Runtime error while Test data creation

2011-10-07 Thread bob gailer

On 10/7/2011 2:19 PM, Guess?!? wrote:

Hello all,

I am trying to create some test data for a search module that I am 
building. Since I dont want to hit performance related issues late in 
the game, I decided to create half a million records in the DB.


My approach is create a csv file with valid UUIDs which I already 
have. Read each record from csv and generate random data and do DB 
insert as long the csv file has records in them. This crashes near 
10 record insertions.


The problem is that my program eventually fails with this error. Any 
ideas what I can improve to make it go all the way?


Runtime Error!
Program: C:\Python27\pythonw.exe
This app has requested the Runtime to terminate it in an unusual 
way. Please contact application support team


Please just post ordinary text. No colors, no fancy formatting.

Please always post the entire traceback so we tell where the code failed.


Here is the code that I wrote to do this.

===
import pyodbc, random, datetime, uuid

#INT
conn = pyodbc.connect('DRIVER={SQL 
Server};SERVER=SERVERNAME\INT_FOUNDATIONS;DATABASE=membership_service;UID=int_usr;PWD=blah')



c = conn.cursor()

# "enterprise_person_id" TEXT NOT NULL,

# read file
f = open('C:\\Users\\garora\\Desktop\\INTPPLHALFMIL.csv','r')

# Hardcoded valid seed values

# INT
programId = 50801
packageId = 46101
pkg_optionId = 40301


seed_data = "TEST_DATA"

numberPrePend = "-"

create_dt = datetime.datetime.now();
modified_dt = datetime.datetime.now();

i = 0

for each in f:
if (i == 0):
i = i + 1
continue;
(id, entrpid,) = each.split(',')
print id
id = id.replace('\n', '')
print id
id = uuid.UUID(id)
#enterprise_person_id
print entrpid
entrpid = entrpid.replace('\n', '')
print entrpid
entrpid = uuid.UUID(entrpid)
#random values for few columns
statusRandom = random.randint(1,4)
sourceRandom = random.randint(1,7)
inputChannelRandom = random.randint(1,5)
marketRandom= random.randint(1,10)
trialRandom = random.randint(0,1)
monthRandom = random.randint(3,12) # prevent month of feb
dateRandom = random.randint(1,30) # prevent 31st on invalid month
yearRandom = random.randint(2007,2010)
member_start_dt = datetime.datetime(yearRandom, monthRandom, 
dateRandom,0,0,0)
current_term_start_dt = datetime.datetime(yearRandom, monthRandom, 
dateRandom,0,0,0)


print "id: " + str(i)
print " programId: " + str(programId)
print " packageId: " + str(packageId)
print " pkgoptionId: " + str(pkg_optionId)
print " status id: " + str(statusRandom)
print " source id: " + str(sourceRandom)
print " input channel id: " + str(inputChannelRandom)
print " market id: " + str(marketRandom)
print " person id: " + str(id)
print "membership id: " + numberPrePend + str(i)
print " member_start_dt " + str(member_start_dt)
print " current_term_start_dt " + str(current_term_start_dt)
print " started_as_trial " + str(trialRandom)
print " enterprise_person_id " + str(entrpid)

record = (i, programId, packageId, pkg_optionId, statusRandom, 
sourceRandom, inputChannelRandom,
marketRandom, str(id), numberPrePend + str(i), member_start_dt, 
current_term_start_dt, trialRandom

,seed_data, seed_data, create_dt, modified_dt, str(entrpid))
print record
c.execute("""insert into Members(id, membership_program_id, 
membership_package_id,
membership_pkg_option_id, membership_status_id, membership_source_id, 
src_input_channel_id,
src_market_id, person_id_ref, membership_id, member_start_dt, 
current_term_start_dt, started_as_trial,

created_by,  modified_by, create_dt, modified_dt, enterprise_person_id)
values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)""", record)
conn.commit()
i = i + 1

c.close()



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



--
Bob Gailer
919-636-4239
Chapel Hill NC

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


Re: [Tutor] Help!

2011-10-07 Thread bob gailer

On 10/7/2011 12:32 PM, rmntcver...@aol.com wrote:
I need serious help with this Rock, Paper, Scissors program. The 
program runs smoothly but I can't get the score to print. Please help 
me with this one aspect! Here is my code right now:


Welcome to Python Help.

In future please use a meaningful subject, such as 'Rock Paper Scissors"

Always reply-all so a copy goes to the list.

We had a similar discussion recently on the Python-Tutor list. I 
submitted an alternate version of the game, which I reproduce here for 
your perusal. It uses some features of Python which may be new to you. 
Please study it, as it may offer some learning.


import random
print """Welcome to Rock,Paper, Scissors! This is a game of chance.
The computer randomly picks one of three throws.
Rock beats Scissors, but is beaten by Paper.
Scissors beat Paper, but are beaten by Rock.
Paper beats Rock, but is beaten by Scissors.
You enter:
r for Rock
s for Scissors
p for Paper
q to Quit'"""
wins = loses = 0
while True: # "endless" loop - exited by break statement
player = raw_input("Please pick your throw: (r, s, p, or q ):")
if player == "q":
break # exits the loop
elif player not in "rps": # check for valid entry
print "Invalid entry"
else:
computer= random.choice("rsp") # no need for a list - a string 
is a sequence

print "Computer throw:", computer
if player == computer: # testing various conditiions cam be 
greatly simplified

print "Tie! Throw again."
elif player + computer in ["rs", "sp", "pr"]:
print "You win! " + player + " beats " + computer
wins += 1 # simpler than wins = wins + 1
else:
print "You lose! " + computer + " beats " + player
loses +=1
print """Game Summary
Wins: %s
Loses:" %s""" % (wins,loses) # using % formatting and triple quoted string
print"Thanks for playing!"


-- Bob Gailer
919-636-4239
Chapel Hill NC
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] vcf_files and strings

2011-10-07 Thread Hs Hs


if col[x] == 'missense':
    print col[withRefSeqID]


hth






From: Anna Olofsson 
To: tutor@python.org
Sent: Friday, October 7, 2011 12:12 PM
Subject: [Tutor] vcf_files and strings


 
Hi,


I'm a beginner at Python and would really appreciate some help in how to 
extract information from a vcf file. 

The attached file consists of a lot of information on mutations, this one 
though is just 2 rows and 10 columns (the real one has a lot more rows). 

I want to extract the mRNA ID only if the mutation is missense. These two rows 
(mutations) that I have attached happens to be missense but how do I say that 
I'm not interested in the mutations that's not missense (they might be e.g. 
synonymous). Also, how do I say that if a mutation starts with a # symbol I 
don't want to include it (sometimes the chr starts with a hash).

vcf file: 2 rows, 10 columns. 
   
col 0     col 1    col 2                  col 3 
 col 4  col5    col6   
col7 col8 col9
chromosome  position       .                  Reference   
ALT  position          .      some statistics and the ID:s not 
important    not important

The important column is 7 where the ID is, i.e. 
refseq.functionalClass=missense. It's a missense mutation, so then I want to 
extract refseq.name=NM_003137492, or I want to extract only the ID, which in 
this case is NM_003137492. 

Then I want to do exactly the same thing for all the other mutations, but only 
for the missense mutations not the other ones. How do I accomplish that? Where 
do I start? 

Best,
Anna


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


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread Alan Gauld

On 07/10/11 18:56, lina wrote:

On Oct 8, 2011, at 0:39, "Prasad, Ramit"  wrote:
 I would really change this to explicitly close the file.


with open(base+OUTFILEEXT,"w") as f:
f.write(str(summary))


Btw, I do notice lots of suggestions of closing file.
Does your above sentence close the file here? Implicitly ?
Sorry, I don't see "close"


The 'with' syntax above implicitly closes the file for you
at the end of the block. If you use the

f = open(...)

style you are expected to explicitly close the file when you are 
finished with it. This is especially important when writing to a file 
because that will guarantee that the OS writes the data to the file on 
disk. Without a close its possible for your data to  get held in a 
buffer that doesn't get written to disk. Rare, but possible.


--
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] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread Alan Gauld

On 07/10/11 16:40, lina wrote:


but today I was also discouraged, I was told that you should not have
learned python, you should focus on C or bash, or D, cause python is
going to be obsolete,


C is a great language for writing Operating Systems and other "near the 
metal" code. But its not the best language for busiess apps, artificial 
intelligence and a host of other things. Bash is a good user shell, but 
its not even the best Unix shell for scripting (Thats probably ksh).

D? Who actually uses D? I think your informant was not well informed.

AS fopr Python becoming obsolete? Maybe someday, but not soon.
And even if it does the principles it teaches are alive and well in many 
other languages - eg, Ruby, Lua both openly borrow from Python.

Just as Python borrows from Lisp and Haskell and Smalltalk and Perl.

> I was further told that fortran is obsolete, but still lots of
> fortran guys using it.

Absolutely and Lisp and COBOL ghave both been predicted to die for 
decades but are still marching on. It is very difficult to kill a 
popular language because the thing that made it popular keeps it

alive.

Ignore rumours and focus on programming. Don't worry about languages, 
once you know one you'll usually pick up another very quickly. They come 
and go in fashion but the concepts remain constant. Most programmers 
work in multiple languages, often even in a single

project.

--
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] Help!

2011-10-07 Thread Alex Hall
On 10/7/11, rmntcver...@aol.com  wrote:
> I need serious help with this Rock, Paper, Scissors program. The program
> runs smoothly but I can't get the score to print. Please help me with this
> one aspect! Here is my code right now:
>
>
>
> import random
>
>
> def computerrockPaperScissors():
> selection = random.randint(0, 2)
> if selection == 0:
> y = 'r'
> if selection == 1:
> y = 's'
> if selection == 2:
> y = 'p'
> return y
>
>
> def humanrockPaperScissors():
> x = input("Make your selection: r (rock), p (paper), s (scissors), q
> (quit)")
> return x
>
>
> humanWins= 0
> computerWins= 0
> ties= 0
>
>
> def compareSelection(x, y):
> if x == 'r' and y == 'r':
> print("You picked rock, I picked rock. We tied!")
> ties+= 1
> elif x == 'r' and y == 's':
> print("You picked rock, I picked scissors. You won!")
> humanWins+= 1
> elif x == 'r' and y == 'p':
> print("You picked rock, I picked paper. I won!")
> computerWins+= 1
> elif x == 's' and y == 's':
> print("You picked scissors, I picked scissors. We tied!")
> ties+= 1
> elif x == 's' and y == 'r':
> print("You picked scissors, I picked rock. I won!")
> computerWins+= 1
> elif x == 's' and y == 'p':
> print("You picked scissors, I picked paper. You won!")
> humanWins+= 1
> elif x == 'p' and y == 'p':
> print("You picked paper, I picked paper. We tied!")
> ties+= 1
> elif x == 'p' and y == 's':
> print("You picked paper, I picked scissors. I won!")
> computerWins+= 1
> elif x == 'p' and y == 'r':
> print("You picked paper, I picked rock. You won!")
> humanWins+= 1
> elif x == 'q':
> print("Game over.")
> print(humanWins, computerWins, ties)

This only prints when the user enters 'q', not when the below for loop ends.
>
>
> def rockPaperScissors():
> print("Let's play Rock Paper Scissors. Rock beats scissors, scissors
> beats paper and paper beats rock.")
> for i in range(999):
> computerSelection = computerrockPaperScissors()
> humanSelection = humanrockPaperScissors()
> result = compareSelection(humanSelection, computerSelection)
Now, after the loop, print the win statistics since they will only
print once the loop exits. If you do that, and the user hits q, the
stats will print twice. I would change your for loop to:
while 1:
 ... #your code
 if humanSelection=='q': break
printStats() #print win/loss numbers, you must make that function if you want

This way, the player can go as many times as they want, even over
1000, and the stats will always print.
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


-- 
Have a great day,
Alex (msg sent from GMail website)
mehg...@gmail.com; http://www.facebook.com/mehgcap
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] vcf_files and strings

2011-10-07 Thread Anna Olofsson

Hi,

I'm a beginner at Python and would really appreciate some help in how to 
extract information from a vcf file. 

The attached file consists of a lot of information on mutations, this one 
though is just 2 rows and 10 columns (the real one has a lot more rows). 

I want to extract the mRNA ID only if the mutation is missense. These two rows 
(mutations) that I have attached happens to be missense but how do I say that 
I'm not interested in the mutations that's not missense (they might be e.g. 
synonymous). Also, how do I say that if a mutation starts with a # symbol I 
don't want to include it (sometimes the chr starts with a hash).

vcf file: 2 rows, 10 columns. 
   
col 0 col 1col 2  col 3 
 col 4  col5col6   col7 
col8 col9
chromosome  position   .  Reference   ALT   
   position  .  some statistics and the ID:s not 
importantnot important

The important column is 7 where the ID is, i.e. 
refseq.functionalClass=missense. It's a missense mutation, so then I want to 
extract refseq.name=NM_003137492, or I want to extract only the ID, which in 
this case is NM_003137492. 

Then I want to do exactly the same thing for all the other mutations, but only 
for the missense mutations not the other ones. How do I accomplish that? Where 
do I start? 

Best,
Anna


  4	69345	.	C	T	32	.	1kg.AD=0;1kg.AF=0.8865;1kg.AN=345;AC=1;AF=0.50;AN=2;BaseQRankSum=-6.432;DP=327;DS;Dels=0.00;FS=6.435;HRun=0;HaplotypeScore=5.0380;MQ=54.34;MQ0=13;MQRankSum=-5.3457;QD=2.65;ReadPosRankSum=-0.321;SB=-321.04;dbsnp.ID=rs43032118;dbsnp.dbSNPBuildID=112;hgid.AF=0.6754;refseq.changesAA=true;refseq.codingCoordStr=c.345C>T;refseq.codonCoord=234;refseq.functionalClass=missense;refseq.inCodingRegion=true;refseq.mrnaCoord=321;refseq.name=NM_003137492;refseq.name2=PQ3D2;refseq.positionType=CFD;refseq.proteinCoordStr=p.C132T;refseq.referenceAA=BRT;refseq.referenceCodon=DGF;refseq.spliceDist=321;refseq.transcriptStrand=+;refseq.variantAA=Ala;refseq.variantCodon=GCA	GT:AD:DP:GQ:PL	1/1:134,32:765:99:56,1,4576
4	87342	.	A	G	57.7	.	1kg.AD=0;1kg.AF=1.0;1kg.AN=345;AC=2;AF=0.00;AN=4;DP=2;Dels=0.00;FS=0.000;HRun=2;HaplotypeScore=0.;MQ=29.00;MQ0=0;QD=54.97;SB=-52.65;dbsnp.ID=rs6732096;dbsnp.dbSNPBuildID=136;hgid.AF=0.9567;refseq.changesAA=true;refseq.codingCoordStr=c.3207A>G;refseq.codonCoord=349;refseq.functionalClass=missense;refseq.inCodingRegion=true;refseq.mrnaCoord=4509;refseq.name=NM_132768;refseq.name2=PQR321;refseq.positionType=CDS;refseq.proteinCoordStr=p.O125R;refseq.referenceAA=Trp;refseq.referenceCodon=ATG;refseq.spliceDist=-45;refseq.transcriptStrand=+;refseq.variantAA=Arg;refseq.variantCodon=CTT	GT:AD:DP:GQ:PL	0/1:0,5:5:2.02:67,9,1
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Help!

2011-10-07 Thread rmntcverses
I need serious help with this Rock, Paper, Scissors program. The program runs 
smoothly but I can't get the score to print. Please help me with this one 
aspect! Here is my code right now:



import random


def computerrockPaperScissors():
selection = random.randint(0, 2)
if selection == 0:
y = 'r'
if selection == 1:
y = 's'
if selection == 2:
y = 'p'
return y


def humanrockPaperScissors():
x = input("Make your selection: r (rock), p (paper), s (scissors), q 
(quit)")
return x


humanWins= 0
computerWins= 0
ties= 0


def compareSelection(x, y):
if x == 'r' and y == 'r':
print("You picked rock, I picked rock. We tied!")
ties+= 1
elif x == 'r' and y == 's':
print("You picked rock, I picked scissors. You won!")
humanWins+= 1
elif x == 'r' and y == 'p':
print("You picked rock, I picked paper. I won!")
computerWins+= 1
elif x == 's' and y == 's':
print("You picked scissors, I picked scissors. We tied!")
ties+= 1
elif x == 's' and y == 'r':
print("You picked scissors, I picked rock. I won!")
computerWins+= 1
elif x == 's' and y == 'p':
print("You picked scissors, I picked paper. You won!")
humanWins+= 1
elif x == 'p' and y == 'p':
print("You picked paper, I picked paper. We tied!")
ties+= 1
elif x == 'p' and y == 's':
print("You picked paper, I picked scissors. I won!")
computerWins+= 1
elif x == 'p' and y == 'r':
print("You picked paper, I picked rock. You won!")
humanWins+= 1
elif x == 'q':
print("Game over.")
print(humanWins, computerWins, ties)




def rockPaperScissors():
print("Let's play Rock Paper Scissors. Rock beats scissors, scissors beats 
paper and paper beats rock.")
for i in range(999):
computerSelection = computerrockPaperScissors()
humanSelection = humanrockPaperScissors()
result = compareSelection(humanSelection, computerSelection)





 


















  




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


[Tutor] Runtime error while Test data creation

2011-10-07 Thread Guess?!?
Hello all,

I am trying to create some test data for a search module that I am building.
Since I dont want to hit performance related issues late in the game, I
decided to create half a million records in the DB.

My approach is create a csv file with valid UUIDs which I already have. Read
each record from csv and generate random data and do DB insert as long the
csv file has records in them. This crashes near 10 record insertions.

The problem is that my program eventually fails with this error. Any ideas
what I can improve to make it go all the way?

Runtime Error!
Program: C:\Python27\pythonw.exe
This app has requested the Runtime to terminate it in an unusual way. Please
contact application support team

Here is the code that I wrote to do this.

===
import pyodbc, random, datetime, uuid

#INT
conn = pyodbc.connect('DRIVER={SQL
Server};SERVER=SERVERNAME\INT_FOUNDATIONS;DATABASE=membership_service;UID=int_usr;PWD=blah')


c = conn.cursor()

# "enterprise_person_id" TEXT NOT NULL,

# read file
f = open('C:\\Users\\garora\\Desktop\\INTPPLHALFMIL.csv','r')

# Hardcoded valid seed values

# INT
programId = 50801
packageId = 46101
pkg_optionId = 40301


seed_data = "TEST_DATA"

numberPrePend = "-"

create_dt = datetime.datetime.now();
modified_dt = datetime.datetime.now();

i = 0

for each in f:
if (i == 0):
i = i + 1
continue;
 (id, entrpid,) = each.split(',')
print id
id = id.replace('\n', '')
print id
id = uuid.UUID(id)
 #enterprise_person_id
print entrpid
entrpid = entrpid.replace('\n', '')
print entrpid
entrpid = uuid.UUID(entrpid)
 #random values for few columns
statusRandom = random.randint(1,4)
sourceRandom = random.randint(1,7)
inputChannelRandom = random.randint(1,5)
marketRandom= random.randint(1,10)
trialRandom = random.randint(0,1)
monthRandom = random.randint(3,12) # prevent month of feb
dateRandom = random.randint(1,30) # prevent 31st on invalid month
yearRandom = random.randint(2007,2010)
member_start_dt = datetime.datetime(yearRandom, monthRandom,
dateRandom,0,0,0)
current_term_start_dt = datetime.datetime(yearRandom, monthRandom,
dateRandom,0,0,0)

print "id: " + str(i)
print " programId: " + str(programId)
print " packageId: " + str(packageId)
print " pkgoptionId: " + str(pkg_optionId)
print " status id: " + str(statusRandom)
print " source id: " + str(sourceRandom)
print " input channel id: " + str(inputChannelRandom)
print " market id: " + str(marketRandom)
print " person id: " + str(id)
print "membership id: " + numberPrePend + str(i)
print " member_start_dt " + str(member_start_dt)
print " current_term_start_dt " + str(current_term_start_dt)
print " started_as_trial " + str(trialRandom)
print " enterprise_person_id " + str(entrpid)

record = (i, programId, packageId, pkg_optionId, statusRandom, sourceRandom,
inputChannelRandom,
marketRandom, str(id), numberPrePend + str(i), member_start_dt,
current_term_start_dt, trialRandom
,seed_data, seed_data, create_dt, modified_dt, str(entrpid))
 print record
 c.execute("""insert into Members(id, membership_program_id,
membership_package_id,
membership_pkg_option_id, membership_status_id, membership_source_id,
src_input_channel_id,
src_market_id, person_id_ref, membership_id, member_start_dt,
current_term_start_dt, started_as_trial,
created_by,  modified_by, create_dt, modified_dt, enterprise_person_id)
values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)""", record)
conn.commit()
i = i + 1

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


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread lina
On Oct 8, 2011, at 0:39, "Prasad, Ramit"  wrote:

>> open(base+OUTFILEEXT,"w").write(str(summary))
> Unless Python3 is different with respect to files, I would really change this 
> to explicitly close the file. In general, I think explicitly closing 
> resources (database connections, files, etc) are a Good Thing.
> 
> with open(base+OUTFILEEXT,"w") as f:
>f.write(str(summary))
> 
Btw, I do notice lots of suggestions of closing file. 
Does your above sentence close the file here? Implicitly ?
Sorry, I don't see "close" 
And how do I know it's safely closed already?

I will check further later. Now reading email on phone. 

Thanks with best regards,

> 
> 
> Ramit
> 
> 
> Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
> 712 Main Street | Houston, TX 77002
> work phone: 713 - 216 - 5423
> 
> 
> 
> This email is confidential and subject to important disclaimers and
> conditions including on offers for the purchase or sale of
> securities, accuracy and completeness of information, viruses,
> confidentiality, legal privilege, and legal entity disclaimers,
> available at http://www.jpmorgan.com/pages/disclosures/email.  
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread lina
On Oct 8, 2011, at 0:33, "Prasad, Ramit"  wrote:

>> what does the 
>> 
>>   for col, ch in enumerate(line):
>> 
> 
> I highly recommend looking at web documentation when you can. It is not that 
> I have any problems answering any questions, but I know I get frustrated when 
> I am forced to learn things in 5 minute increments while waiting on people to 
> respond. Reading the documentation will give a better idea of Python because 
> there is *so* much more information there and the Python docs are actually 
> quite well written.
> 
> Please read the following link and then ask us about any questions you have. 
> http://docs.python.org/dev/library/functions.html#enumerate 
> 

Actually before I asked the question. I did some basic check before.
here is another way/channel of looking for answers. 
Later I also checked online. 
And learned what the enumerate mean, 
But at that time, when I asked on list, I did really don't understand. 
Sometimes the question I asked here is also the question I asked to myself. 
But thanks for your suggestion. 
When I saw soneone asked some very simple question on another list which I know 
that staff long. I also felt that way and wanna suggest them to google or use 
manual. 
> 
> 
>> but today I was also discouraged, I was told that you should not have 
>> learned python, you should focus on C or bash, or D, cause python is going 
>> to be obsolete, and I defensed that I saw lots people/apps using python, and 
>> it's always good to learn a language well, it's helpful for you to 
>> understand another language. I was further told that fortran is obsolete, 
>> but still lots of fortran guys using it.
>> I don't know much, since I started and wanted to learn, actually I wish to 
>> hear some encouraging words, not some words discouraging, but on another 
>> hands, it's always good to have an open mind, so I want to hear your opinion 
>> and >perspective, I am not knowledgable about this.
> 
> Language choice is an often debated subject. People are always proclaiming 
> that a certain language is the "best" and this list probably considers Python 
> the "best". The truth is there is no "best" language! The "best" language (in 
> my not-so-humble opinion) is the language that allows you to effectively, 
> quickly, and easily solve a specific problem. Use the best tool (i.e. 
> language) for the problem; just because you know how to use a hammer does not 
> mean that you should use it for cooking! :)
^_^  good point. 
> 
> As for encouraging words, well...that is not really something I am good at. 
> When you get stuck or bogged down in frustration, remember that learning 
> occurs most often by making mistakes first and then learning to avoid those 
> mistakes.
Thanks. 
> 
> Ramit
> 
> 
> Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
> 712 Main Street | Houston, TX 77002
> work phone: 713 - 216 - 5423
> 
> 
> This email is confidential and subject to important disclaimers and
> conditions including on offers for the purchase or sale of
> securities, accuracy and completeness of information, viruses,
> confidentiality, legal privilege, and legal entity disclaimers,
> available at http://www.jpmorgan.com/pages/disclosures/email.  
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread Prasad, Ramit
>what does the 
>
>for col, ch in enumerate(line):
>

I highly recommend looking at web documentation when you can. It is not that I 
have any problems answering any questions, but I know I get frustrated when I 
am forced to learn things in 5 minute increments while waiting on people to 
respond. Reading the documentation will give a better idea of Python because 
there is *so* much more information there and the Python docs are actually 
quite well written.

Please read the following link and then ask us about any questions you have. 
http://docs.python.org/dev/library/functions.html#enumerate 



>but today I was also discouraged, I was told that you should not have learned 
>python, you should focus on C or bash, or D, cause python is going to be 
>obsolete, and I defensed that I saw lots people/apps using python, and it's 
>always good to learn a language well, it's helpful for you to understand 
>another language. I was further told that fortran is obsolete, but still lots 
>of fortran guys using it.
>I don't know much, since I started and wanted to learn, actually I wish to 
>hear some encouraging words, not some words discouraging, but on another 
>hands, it's always good to have an open mind, so I want to hear your opinion 
>and >perspective, I am not knowledgable about this.

Language choice is an often debated subject. People are always proclaiming that 
a certain language is the "best" and this list probably considers Python the 
"best". The truth is there is no "best" language! The "best" language (in my 
not-so-humble opinion) is the language that allows you to effectively, quickly, 
and easily solve a specific problem. Use the best tool (i.e. language) for the 
problem; just because you know how to use a hammer does not mean that you 
should use it for cooking! :)

As for encouraging words, well...that is not really something I am good at. 
When you get stuck or bogged down in frustration, remember that learning occurs 
most often by making mistakes first and then learning to avoid those mistakes.

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread Prasad, Ramit
> open(base+OUTFILEEXT,"w").write(str(summary))
Unless Python3 is different with respect to files, I would really change this 
to explicitly close the file. In general, I think explicitly closing resources 
(database connections, files, etc) are a Good Thing.

with open(base+OUTFILEEXT,"w") as f:
f.write(str(summary))



Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423



This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread lina
#!/usr/bin/python3

import os.path
import glob

TOKENS="BE"
LINESTOSKIP=0
INFILEEXT=".xpm"
OUTFILEEXT=".txt"


if __name__=="__main__":

for fileName in glob.glob('*.xpm'):
base, ext =os.path.splitext(fileName)
text=open(fileName).readlines()
text=text[LINESTOSKIP:]
numcolumns=len(text[0])
results={}
for ch in TOKENS:
results[ch] = [0]*numcolumns
for line in text:
line = line.strip()
for col, ch in enumerate(line):
if ch in TOKENS:
results[ch][col]+=1
summary=[]
for a,b in zip(results['E'],results['B']):
summary.append(a+b)
print(summary)
open(base+OUTFILEEXT,"w").write(str(summary))

I test it, it finally works. (I was not so sensitive about indentation also)

If you have time or interest, hope to help polish the code,
and warmly welcome to make above one a bit sophisticated. better not
easy-readable for me at first sight.

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


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread lina
what does the

for col, ch in enumerate(line):

enumerate(line) mean, I used idle3 test enumerate, it always showed me:

>>> a
'abcde'
>>> enumerate(a)


I don't have a deep understanding.

Thanks all of you, for your kindness of giving advice and your patience in
explaination.

also another question,

sometimes in my life, I made lots of blind choice,
today I was kinda of happy telling a friend that I feel I finally started on
the road of learning or potentially using python in future.
As I mentioned before, I really did learn it on and off, study one week and
then give up, never give it a deep understanding.
but today I was also discouraged, I was told that you should not have
learned python, you should focus on C or bash, or D, cause python is going
to be obsolete, and I defensed that I saw lots people/apps using python, and
it's always good to learn a language well, it's helpful for you to
understand another language. I was further told that fortran is obsolete,
but still lots of fortran guys using it.
I don't know much, since I started and wanted to learn, actually I wish to
hear some encouraging words, not some words discouraging, but on another
hands, it's always good to have an open mind, so I want to hear your opinion
and perspective, I am not knowledgable about this.

Thanks again for all your time and guide,

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


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread lina
On Fri, Oct 7, 2011 at 11:03 PM, Alan Gauld wrote:

> On 07/10/11 13:21, lina wrote:
>
> One simple explanation:  it continued on to the next file, which has
>>neither "E" nor "B" in it.
>>
>> In this directory, I only kept one file. try.xpm
>>
>>  $ more try.xpm
>> aaEbb
>> aEEbb
>> EaEbb
>> EaEbE
>>
>> $ ls
>> counter-vertically-v2.py  try.xpm
>> counter-vertically.py   try.txt
>>
>
> That's 4 files.
> And your code will try to process all of them, including the Python
> scripts.
>


#!/usr/bin/python3

import os.path
import glob

TOKENS="BE"
LINESTOSKIP=0
INFILEEXT=".xpm"
OUTFILEEXT=".txt"


if __name__=="__main__":

for fileName in glob.glob('*.xpm'):
base, ext =os.path.splitext(fileName)
text=open(fileName).readlines()
text=text[LINESTOSKIP:]
numcolumns=len(text[0])
results={}
for ch in TOKENS:
results[ch] = [0]*numcolumns
for line in text:
line = line.strip()
for col, ch in enumerate(line):
if ch in TOKENS:
results[ch][col]+=1
print(results)
summary=[]
for a,b in zip(results['E'],results['B']):
summary.append(a+b)
print(summary)
open(base+OUTFILEEXT,"w").write(str(summary))

$ more try.xpm
aaEbb
aEEbb
EaEbb
EaEbE


$ python3 counter-vertically-v4.py
{'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 1, 0]}
[1, 0, 1, 0, 1, 0]


Huge unexpected problems, it's only output 1 or 0,
the summary results is not correct.



>
> I think that's a fundamental problem, you should use glob.glob() to ensure
> you only process the files you are interested in.
>
>
>
> --
> 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
>



-- 
Best Regards,

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


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread Alan Gauld

On 07/10/11 13:21, lina wrote:


One simple explanation:  it continued on to the next file, which has
neither "E" nor "B" in it.

In this directory, I only kept one file. try.xpm

  $ more try.xpm
aaEbb
aEEbb
EaEbb
EaEbE

$ ls
counter-vertically-v2.py  try.xpm
counter-vertically.py   try.txt


That's 4 files.
And your code will try to process all of them, including the Python scripts.

I think that's a fundamental problem, you should use glob.glob() to 
ensure you only process the files you are interested in.



--
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] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread Alan Gauld

On 07/10/11 11:25, Dave Angel wrote:


for a,b in zip(results['E'],results['B']):
summary.append(a+b)


I don't know why this gives a key error on 'E' (which basically means
that there is no key 'E') since the code above should guarantee that
it exists. Odd. I'm also not sure why the error occurs after it prints
summary. Are you sure the output is in the sequence you showed in your
message?


One simple explanation: it continued on to the next file, which has
neither "E" nor "B" in it.


But even then this bit of code should ensure that a list of zeros exists:

def processfile(infilename):
results={}
base, ext =os.path.splitext(infilename)
if ext == INFILEEXT:
text = fetchonefiledata(infilename)
numcolumns=len(text[0])
for ch in TOKENS:
results[ch] = [0]*numcolumns

Unless of course the second file doesn't have the right file extension!
In that case it would jump to process the summary with no results data.

for k,v in results.items():
print(results)
summary=[]
for a,b in zip(results['E'],results['B']):
summary.append(a+b)

Yep, that would do it!

Lina, it looks like you need to either move the initialisation of 
results above the extension check, or else move the summary check inside 
the if block.


--
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] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread lina


>
> Now fixed the excessive output.
>
> Thanks,
>
> but in another case, seems there is a problem, for the line actually is:
>
> "EEES~~EE~EE~EE~
>
> E~~EEE~E
> EE~EE~",
>
> not naked EEEor whatever. it's already in ""
>

They treated " "  as char,

but I don't understand why the output is so many zeros ? and only zero for
my case
because each line ends up with , ?

print(text) showed:

SSST",\n', '"E

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


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread lina
On Fri, Oct 7, 2011 at 4:08 PM, lina  wrote:

>
>
> On Fri, Oct 7, 2011 at 3:39 PM, lina  wrote:
>
>>
>>
>> On Fri, Oct 7, 2011 at 9:50 AM, Steven D'Aprano wrote:
>>
>>> lina wrote:
>>>
>>>  May I ask a further question:

  a
>>>
>> {'B': [4, 5, 6], 'E': {1, 2, 3}}

>>>
>>> Why is a['B'] a list and a['E'] a set?
>>>
>>>
>>>
>>>
>>>  How can I get the value of
 set(a['E'])+set(a['B'])

 I mean, get a new dict 'B+E':[5,7,9]

>>>
>>>
>>> You are confusing different things into one question, as if I had asked:
>>>
>>> "How do I make a hard boiled egg? I mean, get a potato salad."
>>>
>>> You must ask a clear question to get a clear answer.
>>>
>>>
>>>
>>> To answer your first question, what do you mean by adding two sets? I can
>>> take the *union* of two sets (anything in either one OR the other):
>>>
>>> >>> a['E'] | set(a['B'])  # one is already a set, no need to convert
>>> {1, 2, 3, 4, 5, 6}
>>>
>>>
>>> or I can take the *intersection* of the two sets (anything in both one
>>> AND the other):
>>>
>>> >>> a['E'] & set(a['B'])
>>> set()
>>>
>>> There are no items in common between the two, so nothing in the
>>> intersection.
>>>
>>>
>>> To get the result you are asking for:
>>>
>>> [5, 7, 9]
>>>
>>> makes no sense. How do you expect to get a *list* by combining two
>>> *sets*? They are different things. Lists have order, sets do not:
>>>
>>> >>> [1, 2, 3] == [3, 2, 1]
>>> False
>>> >>> {1, 2, 3} == {3, 2, 1}
>>> True
>>>
>>>
>>> A list is a sequence of values in order, a set is like a jumble of values
>>> tossed in a bag.
>>>
>>> My *guess* is that you don't care about sets at all, you want two lists:
>>>
>>
>> Thanks, I did not realize the great differences between the list and sets.
>> I was not so sensitive about the concepts before.
>>
>>>
>>>
>>> [1, 2, 3]
>>> [4, 5, 6]
>>>
>>>
>>> and you want to add them item by item to get another list:
>>>
>>> [5, 7, 9]
>>>
>>>
>>> Have I guessed correctly?
>>>
>>>
>>> If so, here's the hard way to do it:
>>>
>>>
>>> first_list = [1, 2, 3]
>>> second_list = [4, 5, 6]
>>> result = []
>>> for i in range(3):
>>>a = first_list[i]
>>>b = second_list[i]
>>>result.append(a + b)
>>>
>>> print(result)
>>>
>>>
>>> Walking along two lists in lock-step like that is so common that Python
>>> has a dedicated function specially for it: zip.
>>>
>>> result = []
>>> for a,b in zip(first_list, second_list):
>>>result.append(a+b)
>>>
>>>
>>> which can be simplified further to a list comprehension:
>>>
>>> result = [a+b for a,b in zip(first_list, second_list)]
>>>
>>>
>> Thanks, just why the output it's something double, more than I want.
>>
>> #!/bin/python3
>>
>> import os.path
>>
>> TOKENS="BE"
>>
>> LINESTOSKIP=0
>> INFILEEXT=".xpm"
>> OUTFILEEXT=".txt"
>>
>> def dofiles(topdirectory):
>> for filename in os.listdir(topdirectory):
>> processfile(filename)
>>
>> def processfile(infilename):
>> results={}
>>
>> base, ext =os.path.splitext(infilename)
>> if ext == INFILEEXT:
>> text = fetchonefiledata(infilename)
>> numcolumns=len(text[0])
>> for ch in TOKENS:
>> results[ch] = [0]*numcolumns
>> for line in text:
>> line = line.strip()
>> for col, ch in enumerate(line):
>> if ch in TOKENS:
>> results[ch][col]+=1
>> for k,v in results.items():
>>
> My mistake, here should remove the "for k,v in results.items()"

> print(results)
>> summary=[]
>> for a,b in zip(results['E'],results['B']):
>> summary.append(a+b)
>> writeonefiledata(base+OUTFILEEXT,summary)
>>
>>
>> def fetchonefiledata(inname):
>> infile = open(inname)
>> text = infile.readlines()
>> return text[LINESTOSKIP:]
>>
>> def writeonefiledata(outname,summary):
>>
>> outfile = open(outname,"w")
>> for elem in summary:
>>
> another mistake here, I shouldn't have used "for elem in summary"

> outfile.write(str(summary))
>>
>>
>>
>> if __name__=="__main__":
>> dofiles(".")
>>
>>
>>  $ python3 counter-vertically-v2.py
>> {'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 1, 0]}
>> {'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 1, 0]}
>>
>> $ more try.txt
>> [1, 0, 1, 0, 1, 0][1, 0, 1, 0, 1, 0][1, 0, 1, 0, 1, 0][1, 0, 1, 0, 1,
>> 0][1, 0, 1
>> , 0, 1, 0][1, 0, 1, 0, 1, 0]
>>
>> $ more try.xpm
>> aaEbb
>> aEEbb
>> EaEbb
>> EaEbE
>>
>> Thanks,
>>
>
> I thought it might be some loop reason made it double output the results,
> so I made an adjustation in indent, now it showed:
>
> $ python3 counter-vertically-v2.py
> {'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 1, 0]}
> {'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 1, 0]}
> [1, 0, 1, 0, 1, 0]
> Traceback (most recent call last):
>   File "counter-vertically-v2.py", line 48, in 
> dofiles(".")
>   File "counter-vertically-v2.py", line 13, in dofiles
> processfile(filename)
>   File "counter-vertically-v2.py", line 31, in processfil

Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread lina
On Fri, Oct 7, 2011 at 9:08 PM, Andreas Perstinger <
andreas.perstin...@gmx.net> wrote:

> On 2011-10-07 14:21, lina wrote:
>
>>  I don't know why this gives a key error on 'E' (which basically means
 that
  there is no key 'E') since the code above should guarantee that it
 exists.
  Odd. I'm also not sure why the error occurs after it prints summary.
 Are you
  sure the output is in the sequence you showed in your message?

  One simple explanation:  it continued on to the next file, which has

>>>  neither "E" nor "B" in it.
>>>
>>>  In this directory, I only kept one file. try.xpm
>>
>
> That's wrong.
>
>
>   $ more try.xpm
>> aaEbb
>> aEEbb
>> EaEbb
>> EaEbE
>>
>> $ ls
>> counter-vertically-v2.py  try.xpm
>> counter-vertically.py   try.txt
>>
>
> As "ls" proves, you have *4* files in the directory.
>
> You start your script with "dofiles(".")". This function gets a list of
> *all* files in the directory in an *arbitrary* order and processes each of
> it.
>
> In your function "processfile" you first create an empty dictionary
> ("results = {}") and then you put values into the dictionary *only* for
> xpm-files ("if ext == INFILEEXT:"). *But* you print the summary for *every*
> file because the indentation at the end of the function is outside the
> if-branch where you check for the file extension. So for every file which
> isn't a xpm-file, "results" is an empty dictionary (see first line of the
> function) and therefore "zip(results['E'], results['B'])" will throw an
> exception.
>
> How to solve it? I personally would check for the file extension in the
> function "dofiles" and would only continue with xpm-files (in other words
> move the if-statement from "processfile" to "dofiles".)
>
 def processfile(infilename):
results={}
base, ext =os.path.splitext(infilename)
if ext == INFILEEXT:
text = fetchonefiledata(infilename)
numcolumns=len(text[0])
for ch in TOKENS:
results[ch] = [0]*numcolumns
for line in text:
line = line.strip()
for col, ch in enumerate(line):
if ch in TOKENS:
results[ch][col]+=1
for k,v in results.items():
print(results)
summary=[]
for a,b in zip(results['E'],results['B']):
summary.append(a+b)
writeonefiledata(base+OUTFILEEXT,summary)
else:
os.sys.exit()

This part has already given the indentation. if moved it a bit further,

it would show more like:
$ python3 counter-vertically-v2.py
{'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 0, 0, 0, 0]}
{'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 0, 0, 0, 0]}
{'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 0, 0, 0, 0]}
{'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 0, 0, 0, 0]}
{'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 0, 0]}
{'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 0, 0]}
{'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 0, 0]}
{'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 0, 0]}
{'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 1, 0]}
{'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 1, 0]}

Thanks again,

>
> Bye, Andreas
>
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread Andreas Perstinger

On 2011-10-07 14:21, lina wrote:

 I don't know why this gives a key error on 'E' (which basically means that
 there is no key 'E') since the code above should guarantee that it exists.
 Odd. I'm also not sure why the error occurs after it prints summary. Are you
 sure the output is in the sequence you showed in your message?

  One simple explanation:  it continued on to the next file, which has

 neither "E" nor "B" in it.


In this directory, I only kept one file. try.xpm


That's wrong.


  $ more try.xpm
aaEbb
aEEbb
EaEbb
EaEbE

$ ls
counter-vertically-v2.py  try.xpm
counter-vertically.py   try.txt


As "ls" proves, you have *4* files in the directory.

You start your script with "dofiles(".")". This function gets a list of 
*all* files in the directory in an *arbitrary* order and processes each 
of it.


In your function "processfile" you first create an empty dictionary 
("results = {}") and then you put values into the dictionary *only* for 
xpm-files ("if ext == INFILEEXT:"). *But* you print the summary for 
*every* file because the indentation at the end of the function is 
outside the if-branch where you check for the file extension. So for 
every file which isn't a xpm-file, "results" is an empty dictionary (see 
first line of the function) and therefore "zip(results['E'], 
results['B'])" will throw an exception.


How to solve it? I personally would check for the file extension in the 
function "dofiles" and would only continue with xpm-files (in other 
words move the if-statement from "processfile" to "dofiles".)


Bye, Andreas

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


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread lina
When I put it into a real case,

it showed me all as 0 0 0 s

The python code and the real one file can be accessed from below link:

https://docs.google.com/leaf?id=0B93SVRfpVVg3MjQ0YzEzOWUtYWU0MC00YzAwLWJiYTctY2E5YTEzY2U0NGI3&hl=en_GB

https://docs.google.com/leaf?id=0B93SVRfpVVg3MWMxZDg0MmItOTNiMi00NGZkLWEzMGEtZDU5NzI1YjRlZjU3&hl=en_GB

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


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread lina
On Fri, Oct 7, 2011 at 6:25 PM, Dave Angel  wrote:

> On 10/07/2011 06:06 AM, Alan Gauld wrote:
>
>> On 07/10/11 09:08, lina wrote:
>> 
>>
>>  summary=[]
>>> for a,b in zip(results['E'],results['B'])**:
>>> summary.append(a+b)
>>>
>>
>> I don't know why this gives a key error on 'E' (which basically means that
>> there is no key 'E') since the code above should guarantee that it exists.
>> Odd. I'm also not sure why the error occurs after it prints summary. Are you
>> sure the output is in the sequence you showed in your message?
>>
>>  One simple explanation:  it continued on to the next file, which has
> neither "E" nor "B" in it.
>
In this directory, I only kept one file. try.xpm

 $ more try.xpm
aaEbb
aEEbb
EaEbb
EaEbE

$ ls
counter-vertically-v2.py  try.xpm
counter-vertically.py   try.txt


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



-- 
Best Regards,

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


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread lina
On Fri, Oct 7, 2011 at 6:25 PM, Dave Angel  wrote:

> On 10/07/2011 06:06 AM, Alan Gauld wrote:
>
>> On 07/10/11 09:08, lina wrote:
>> 
>>
>>  summary=[]
>>> for a,b in zip(results['E'],results['B'])**:
>>> summary.append(a+b)
>>>
>>
>> I don't know why this gives a key error on 'E' (which basically means that
>> there is no key 'E') since the code above should guarantee that it exists.
>> Odd. I'm also not sure why the error occurs after it prints summary. Are you
>> sure the output is in the sequence you showed in your message?
>>
>>  One simple explanation:  it continued on to the next file, which has
> neither "E" nor "B" in it.
>
>
>
>
The final results got "6" summary out, which I only expect one.
and how can I output the summary, in the wrong situation, let it \n ?


$ more try.txt
[1, 0, 1, 0, 1, 0][1, 0, 1, 0, 1, 0][1, 0, 1, 0, 1, 0][1, 0, 1, 0, 1, 0][1,
0, 1
, 0, 1, 0][1, 0, 1, 0, 1, 0]



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



-- 
Best Regards,

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


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread Dave Angel

On 10/07/2011 06:06 AM, Alan Gauld wrote:

On 07/10/11 09:08, lina wrote:


 summary=[]
 for a,b in zip(results['E'],results['B']):
 summary.append(a+b)


I don't know why this gives a key error on 'E' (which basically means 
that there is no key 'E') since the code above should guarantee that 
it exists. Odd. I'm also not sure why the error occurs after it prints 
summary. Are you sure the output is in the sequence you showed in your 
message?


One simple explanation:  it continued on to the next file, which has 
neither "E" nor "B" in it.




--

DaveA

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


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread Dave Angel

On 10/07/2011 04:08 AM, lina wrote:


I thought it might be some loop reason made it double output the results, so
I made an adjustation in indent, now it showed:
$ python3 counter-vertically-v2.py
{'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 1, 0]}
{'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 1, 0]}
[1, 0, 1, 0, 1, 0]
Traceback (most recent call last):
   File "counter-vertically-v2.py", line 48, in
 dofiles(".")
   File "counter-vertically-v2.py", line 13, in dofiles
 processfile(filename)
   File "counter-vertically-v2.py", line 31, in processfile
 for a,b in zip(results['E'],results['B']):
KeyError: 'E'

still two results, but the summary is correct, with a KeyError which I don't
know how to fix the key error here.

#!/bin/python3

import os.path


TOKENS="BE"
LINESTOSKIP=0
INFILEEXT=".xpm"
OUTFILEEXT=".txt"

def dofiles(topdirectory):
 for filename in os.listdir(topdirectory):
 processfile(filename)

def processfile(infilename):
 results={}
 base, ext =os.path.splitext(infilename)
 if ext == INFILEEXT:
 text = fetchonefiledata(infilename)
 numcolumns=len(text[0])
 for ch in TOKENS:
 results[ch] = [0]*numcolumns
 for line in text:
 line = line.strip()
 for col, ch in enumerate(line):
 if ch in TOKENS:
 results[ch][col]+=1
 for k,v in results.items():
 print(results)
That'll print the whole map for each item in it.  Since you apparently 
have two items, "E" and "B", you get the whole thing printed out twice.


I have no idea what you really wanted to print, but it probably was k and v


 summary=[]
 for a,b in zip(results['E'],results['B']):
 summary.append(a+b)
 print(summary)
 writeonefiledata(base+OUTFILEEXT,summary)

def fetchonefiledata(inname):
 infile = open(inname)
 text = infile.readlines()
 return text[LINESTOSKIP:]

def writeonefiledata(outname,summary):
 outfile = open(outname,"w")
 for elem in summary:
 outfile.write(str(summary))


if __name__=="__main__":
 dofiles(".")

Thanks all for your time,


As for the reason you got the exception, it probably was because the 
NEXT file had no E's in it.


One of the reasons to break this stuff into separate functions is so you 
can test them separately.  You probably should be calling processfile() 
directly in your top-level code, till it all comes out correctly.  Or at 
least add a print of the filename it's working on.



Anyway, it's  probably a mistake to ever reference "E" and "B" 
explicitly, but instead loop through the TOKENS.  That way it'll still 
work when you add more or different tokens.  Further, if it's considered 
valid for an input file not to have samples of all the tokens, then you 
have to loop through the ones you actually have.  That might mean 
looping through the keys of results.  Or, for the particular use case in 
that line, there's undoubtedly a method of results that will give you 
all the values in a list.  That list would make an even better argument 
to zip().  Once again, I remind you of the dir() function, to see 
available methods.


--

DaveA

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


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread Alan Gauld

On 07/10/11 09:08, lina wrote:


TOKENS="BE"
LINESTOSKIP=0
INFILEEXT=".xpm"
OUTFILEEXT=".txt"

def dofiles(topdirectory):
 for filename in os.listdir(topdirectory):
 processfile(filename)

def processfile(infilename):
 results={}
 base, ext =os.path.splitext(infilename)
 if ext == INFILEEXT:



 text = fetchonefiledata(infilename)
 numcolumns=len(text[0])
 for ch in TOKENS:
 results[ch] = [0]*numcolumns
 for line in text:
 line = line.strip()
 for col, ch in enumerate(line):
 if ch in TOKENS:
 results[ch][col]+=1


It would be easier to read(and debug) if you put
that chunk into a function. Using the naming style below
it could be called processOneFileData() for example...

Make it return the results dictionary.


 for k,v in results.items():
 print(results)


This prints the same thing (results) for as many items
are in results. I'm pretty sure you don't want that.
Just printing results once should be sufficient.


 summary=[]
 for a,b in zip(results['E'],results['B']):
 summary.append(a+b)


I don't know why this gives a key error on 'E' (which basically means 
that there is no key 'E') since the code above should guarantee that it 
exists. Odd. I'm also not sure why the error occurs after it prints 
summary. Are you sure the output is in the sequence you showed in your 
message?



 print(summary)
 writeonefiledata(base+OUTFILEEXT,summary)

def fetchonefiledata(inname):
 infile = open(inname)
 text = infile.readlines()
 return text[LINESTOSKIP:]

def writeonefiledata(outname,summary):
 outfile = open(outname,"w")
 for elem in summary:
 outfile.write(str(summary))


if __name__=="__main__":
 dofiles(".")



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


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread lina
On Fri, Oct 7, 2011 at 3:39 PM, lina  wrote:

>
>
> On Fri, Oct 7, 2011 at 9:50 AM, Steven D'Aprano wrote:
>
>> lina wrote:
>>
>>  May I ask a further question:
>>>
>>>  a
>>
> {'B': [4, 5, 6], 'E': {1, 2, 3}}
>>>
>>
>> Why is a['B'] a list and a['E'] a set?
>>
>>
>>
>>
>>  How can I get the value of
>>> set(a['E'])+set(a['B'])
>>>
>>> I mean, get a new dict 'B+E':[5,7,9]
>>>
>>
>>
>> You are confusing different things into one question, as if I had asked:
>>
>> "How do I make a hard boiled egg? I mean, get a potato salad."
>>
>> You must ask a clear question to get a clear answer.
>>
>>
>>
>> To answer your first question, what do you mean by adding two sets? I can
>> take the *union* of two sets (anything in either one OR the other):
>>
>> >>> a['E'] | set(a['B'])  # one is already a set, no need to convert
>> {1, 2, 3, 4, 5, 6}
>>
>>
>> or I can take the *intersection* of the two sets (anything in both one AND
>> the other):
>>
>> >>> a['E'] & set(a['B'])
>> set()
>>
>> There are no items in common between the two, so nothing in the
>> intersection.
>>
>>
>> To get the result you are asking for:
>>
>> [5, 7, 9]
>>
>> makes no sense. How do you expect to get a *list* by combining two *sets*?
>> They are different things. Lists have order, sets do not:
>>
>> >>> [1, 2, 3] == [3, 2, 1]
>> False
>> >>> {1, 2, 3} == {3, 2, 1}
>> True
>>
>>
>> A list is a sequence of values in order, a set is like a jumble of values
>> tossed in a bag.
>>
>> My *guess* is that you don't care about sets at all, you want two lists:
>>
>
> Thanks, I did not realize the great differences between the list and sets.
> I was not so sensitive about the concepts before.
>
>>
>>
>> [1, 2, 3]
>> [4, 5, 6]
>>
>>
>> and you want to add them item by item to get another list:
>>
>> [5, 7, 9]
>>
>>
>> Have I guessed correctly?
>>
>>
>> If so, here's the hard way to do it:
>>
>>
>> first_list = [1, 2, 3]
>> second_list = [4, 5, 6]
>> result = []
>> for i in range(3):
>>a = first_list[i]
>>b = second_list[i]
>>result.append(a + b)
>>
>> print(result)
>>
>>
>> Walking along two lists in lock-step like that is so common that Python
>> has a dedicated function specially for it: zip.
>>
>> result = []
>> for a,b in zip(first_list, second_list):
>>result.append(a+b)
>>
>>
>> which can be simplified further to a list comprehension:
>>
>> result = [a+b for a,b in zip(first_list, second_list)]
>>
>>
> Thanks, just why the output it's something double, more than I want.
>
> #!/bin/python3
>
> import os.path
>
> TOKENS="BE"
>
> LINESTOSKIP=0
> INFILEEXT=".xpm"
> OUTFILEEXT=".txt"
>
> def dofiles(topdirectory):
> for filename in os.listdir(topdirectory):
> processfile(filename)
>
> def processfile(infilename):
> results={}
>
> base, ext =os.path.splitext(infilename)
> if ext == INFILEEXT:
> text = fetchonefiledata(infilename)
> numcolumns=len(text[0])
> for ch in TOKENS:
> results[ch] = [0]*numcolumns
> for line in text:
> line = line.strip()
> for col, ch in enumerate(line):
> if ch in TOKENS:
> results[ch][col]+=1
> for k,v in results.items():
> print(results)
> summary=[]
> for a,b in zip(results['E'],results['B']):
> summary.append(a+b)
> writeonefiledata(base+OUTFILEEXT,summary)
>
>
> def fetchonefiledata(inname):
> infile = open(inname)
> text = infile.readlines()
> return text[LINESTOSKIP:]
>
> def writeonefiledata(outname,summary):
>
> outfile = open(outname,"w")
> for elem in summary:
> outfile.write(str(summary))
>
>
>
> if __name__=="__main__":
> dofiles(".")
>
>
>  $ python3 counter-vertically-v2.py
> {'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 1, 0]}
> {'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 1, 0]}
>
> $ more try.txt
> [1, 0, 1, 0, 1, 0][1, 0, 1, 0, 1, 0][1, 0, 1, 0, 1, 0][1, 0, 1, 0, 1, 0][1,
> 0, 1
> , 0, 1, 0][1, 0, 1, 0, 1, 0]
>
> $ more try.xpm
> aaEbb
> aEEbb
> EaEbb
> EaEbE
>
> Thanks,
>

I thought it might be some loop reason made it double output the results, so
I made an adjustation in indent, now it showed:
$ python3 counter-vertically-v2.py
{'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 1, 0]}
{'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 1, 0]}
[1, 0, 1, 0, 1, 0]
Traceback (most recent call last):
  File "counter-vertically-v2.py", line 48, in 
dofiles(".")
  File "counter-vertically-v2.py", line 13, in dofiles
processfile(filename)
  File "counter-vertically-v2.py", line 31, in processfile
for a,b in zip(results['E'],results['B']):
KeyError: 'E'

still two results, but the summary is correct, with a KeyError which I don't
know how to fix the key error here.

#!/bin/python3

import os.path


TOKENS="BE"
LINESTOSKIP=0
INFILEEXT=".xpm"
OUTFILEEXT=".txt"

def dofiles(topdirectory):
for filename in os.listdir(topdirectory):
processfile(filename)

def processfile(infilename):

Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread lina
On Fri, Oct 7, 2011 at 9:50 AM, Steven D'Aprano  wrote:

> lina wrote:
>
>  May I ask a further question:
>>
>>  a
>
 {'B': [4, 5, 6], 'E': {1, 2, 3}}
>>
>
> Why is a['B'] a list and a['E'] a set?
>
>
>
>
>  How can I get the value of
>> set(a['E'])+set(a['B'])
>>
>> I mean, get a new dict 'B+E':[5,7,9]
>>
>
>
> You are confusing different things into one question, as if I had asked:
>
> "How do I make a hard boiled egg? I mean, get a potato salad."
>
> You must ask a clear question to get a clear answer.
>
>
>
> To answer your first question, what do you mean by adding two sets? I can
> take the *union* of two sets (anything in either one OR the other):
>
> >>> a['E'] | set(a['B'])  # one is already a set, no need to convert
> {1, 2, 3, 4, 5, 6}
>
>
> or I can take the *intersection* of the two sets (anything in both one AND
> the other):
>
> >>> a['E'] & set(a['B'])
> set()
>
> There are no items in common between the two, so nothing in the
> intersection.
>
>
> To get the result you are asking for:
>
> [5, 7, 9]
>
> makes no sense. How do you expect to get a *list* by combining two *sets*?
> They are different things. Lists have order, sets do not:
>
> >>> [1, 2, 3] == [3, 2, 1]
> False
> >>> {1, 2, 3} == {3, 2, 1}
> True
>
>
> A list is a sequence of values in order, a set is like a jumble of values
> tossed in a bag.
>
> My *guess* is that you don't care about sets at all, you want two lists:
>

Thanks, I did not realize the great differences between the list and sets. I
was not so sensitive about the concepts before.

>
>
> [1, 2, 3]
> [4, 5, 6]
>
>
> and you want to add them item by item to get another list:
>
> [5, 7, 9]
>
>
> Have I guessed correctly?
>
>
> If so, here's the hard way to do it:
>
>
> first_list = [1, 2, 3]
> second_list = [4, 5, 6]
> result = []
> for i in range(3):
>a = first_list[i]
>b = second_list[i]
>result.append(a + b)
>
> print(result)
>
>
> Walking along two lists in lock-step like that is so common that Python has
> a dedicated function specially for it: zip.
>
> result = []
> for a,b in zip(first_list, second_list):
>result.append(a+b)
>
>
> which can be simplified further to a list comprehension:
>
> result = [a+b for a,b in zip(first_list, second_list)]
>
>
Thanks, just why the output it's something double, more than I want.

#!/bin/python3

import os.path

TOKENS="BE"
LINESTOSKIP=0
INFILEEXT=".xpm"
OUTFILEEXT=".txt"

def dofiles(topdirectory):
for filename in os.listdir(topdirectory):
processfile(filename)

def processfile(infilename):
results={}
base, ext =os.path.splitext(infilename)
if ext == INFILEEXT:
text = fetchonefiledata(infilename)
numcolumns=len(text[0])
for ch in TOKENS:
results[ch] = [0]*numcolumns
for line in text:
line = line.strip()
for col, ch in enumerate(line):
if ch in TOKENS:
results[ch][col]+=1
for k,v in results.items():
print(results)
summary=[]
for a,b in zip(results['E'],results['B']):
summary.append(a+b)
writeonefiledata(base+OUTFILEEXT,summary)

def fetchonefiledata(inname):
infile = open(inname)
text = infile.readlines()
return text[LINESTOSKIP:]

def writeonefiledata(outname,summary):
outfile = open(outname,"w")
for elem in summary:
outfile.write(str(summary))


if __name__=="__main__":
dofiles(".")


 $ python3 counter-vertically-v2.py
{'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 1, 0]}
{'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 1, 0]}

$ more try.txt
[1, 0, 1, 0, 1, 0][1, 0, 1, 0, 1, 0][1, 0, 1, 0, 1, 0][1, 0, 1, 0, 1, 0][1,
0, 1
, 0, 1, 0][1, 0, 1, 0, 1, 0]

$ more try.xpm
aaEbb
aEEbb
EaEbb
EaEbE

Thanks,

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



-- 
Best Regards,

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


Re: [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

2011-10-07 Thread lina
On Fri, Oct 7, 2011 at 9:38 AM, Dave Angel  wrote:

> On 10/06/2011 12:21 PM, lina wrote:
>
>> 
>>
>>
>>> As for splitting into functions, consider:
>>>
>>> #these two are capitalized because they're intended to be constant
>>> TOKENS = "BE"
>>> LINESTOSKIP = 43
>>> INFILEEXT = ".xpm"
>>> OUTFILEEXT = ".txt"
>>>
>>> def dofiles(topdirectory):
>>>for filename in os.listdr(topdirectory):
>>>processfile(filename)
>>>
>>> def processfile(infilename):
>>>base, ext =os.path.splitext(fileName)
>>>if ext == INFILEEXT:
>>>text = fetchonefiledata(infilename)
>>>numcolumns = len(text[0])
>>>results = {}
>>>for ch in TOKENS:
>>>
>>>results[ch] = [0] * numcolumns
>>>for line in text:
>>>line = line.strip()
>>>
>>>for col, ch in enumerate(line):
>>>if ch in tokens:
>>>results[ch][col] += 1
>>>
>>>  I still have trouble understanding the results[ch][col] part.
>>
>> Thanks ahead,
>>
>>
>>  First ask yourself what results consists of.  It's a dictionary, which is
> a aggregate of pairs of key/value items.  In each case the key is a
> character, and the the value is a list of numbers.
>
> So results[ch] is a particular value, in other words a list.
>
> And results[ch] [col] is a particular item of the list, in other words, and
> integer
>
> If we use += 1 on that integer, we increment it by 1
>
> Is that clear?   if not, write a separate program to build such as
> structure, and try printing out each level of item
> print results["E"]
> should display a list
>  print results["E"][0]
> should display an integer
>  print results["E"][1]
>  print results["E"][2]
>

Yes. I understand this part now. But how can I print a list consists of the
value of key B + E.

For {'B': [4, 5, 6], 'E': [1, 2, 3]}

I wanna get the summary of B and E in each column, namely [5, 7, 9]


>
>
> Does this help?
>
> --
>
> DaveA
>
>


-- 
Best Regards,

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


[Tutor] urllib2 sslerror

2011-10-07 Thread Johan Geldenhuys
Hi everybody,

Almost weekend, hang in there.

I need some advice and hope somebody can help me.

I have the follow piece of code that I use to get some data from a https
webpage.

---
< some other stuff up here>
try:
return_object = urllib2.urlopen(theurl)
html_content = return_object.read()
  
now = time.strftime("%Y%m%d%H%M%S")
img_file = "%s_%s.jpg"% (location, now)
f = open(img_file, 'wb')
f.write(html_content)
f.close()
file = img_file
except Exception, e:
print e


---

Sometimes I get the following error and I don't know why. It occurs very
randomly and is hard or nearly impossible to reproduce (maybe there is
somebody who knows how to create it).

---
Traceback (most recent call last):
  File "/var/ai-muse/jail/pkgdb/camimage/getimage.py", line 74, in run
return_object = urllib2.urlopen(self.theurl)
  File "/usr/lib/python2.2/urllib2.py", line 138, in urlopen
  File "/usr/lib/python2.2/urllib2.py", line 322, in open
  File "/usr/lib/python2.2/urllib2.py", line 301, in _call_chain
  File "/usr/lib/python2.2/urllib2.py", line 792, in https_open
  File "/usr/lib/python2.2/urllib2.py", line 779, in do_open
  File "/usr/lib/python2.2/urllib2.py", line 342, in error
  File "/usr/lib/python2.2/urllib2.py", line 301, in _call_chain
  File "/usr/lib/python2.2/urllib2.py", line 611, in http_error_401
  File "/usr/lib/python2.2/urllib2.py", line 590, in http_error_auth_reqed
  File "/usr/lib/python2.2/urllib2.py", line 600, in retry_http_basic_auth
  File "/usr/lib/python2.2/urllib2.py", line 322, in open
  File "/usr/lib/python2.2/urllib2.py", line 301, in _call_chain
  File "/usr/lib/python2.2/urllib2.py", line 792, in https_open
  File "/usr/lib/python2.2/urllib2.py", line 774, in do_open
  File "/usr/lib/python2.2/httplib.py", line 752, in getreply
  File "/usr/lib/python2.2/httplib.py", line 593, in getresponse
  File "/usr/lib/python2.2/httplib.py", line 99, in __init__
  File "/usr/lib/python2.2/httplib.py", line 628, in makefile
sslerror: (5, 'EOF occurred in violation of protocol')



Any idea why it happens randomly and how I can check the cause?
I read on a Google search page that one guy had the same issue and traced it
back to some characters in the data he was receiving. Could that cause it?

Thanks in advance
  
Johan 


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