Re: [Tutor] force imports from site-packages?

2011-10-06 Thread Alex Hall
Thanks, it is now working properly. It turned out to be something in
the __all__ list that didn'tmake much sense, but now that I know what
to use it is working.

On 10/6/11, Steven D'Aprano  wrote:
> Alex Hall wrote:
>> Hi all,
>> I have managed to get a couple of packages in site-packages which
>> share names with some folders in the same directory as a program, or
>> at least a subdir somewhere below has the same name. Is there a way to
>> force my script to look in lib/site-packages before the script's
>> folder? I can't rename these folders since Git will get confused if I
>> do.
>> In other words, I have a package called LRSignature installed, but in
>> a subdir of my script's directory is another LRSignature folder which
>> is not an actual Python package. Python sees this non-package folder
>
> It shouldn't. Python doesn't look into nested folders unless you
> explicitly add them to the search path. Here's an example:
>
>
> [steve@sylar ~]$ mkdir math
> [steve@sylar ~]$ echo "print('spam')" > math/module.py
> [steve@sylar ~]$ python
> Python 2.5 (r25:51908, Nov  6 2007, 16:54:01)
> [GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>  >>> import math  # Can we still see the standard math module?
>  >>> math.sin(1)
> 0.8414709848078965
>  >>> import module  # How about the other module?
> Traceback (most recent call last):
>File "", line 1, in 
> ImportError: No module named module
>
>
> So as you can see, the math subdirectory and its content is ignored by
> Python because it isn't explicitly part of the search path. So I don't
> understand why you are seeing this problem unless you are cd'ing into
> the LRSignature folder first.
>
>
>> first and so, quite rightly, throws an exception that the class of the
>> package I want, LRSignature.Sign, does not exist. It exists in the
>> site-packages copy, but Python won't overlook the nearer copy. How do
>> I make it do this? Thanks.
>
>
> The *right* solution is to fix the name clash, which may or may not
> involve changing the name of something. But I can't tell what, since I
> don't understand why you are having this problem.
>
> But if you can't fix the problem, you can cover it up by manipulating
> the import search path. It is exposed as sys.path, and it is just a list
> of places to look in the specific order given.
>
>
>
>
> --
> Steven
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>


-- 
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


Re: [Tutor] force imports from site-packages?

2011-10-06 Thread Steven D'Aprano

Alex Hall wrote:

Hi all,
I have managed to get a couple of packages in site-packages which
share names with some folders in the same directory as a program, or
at least a subdir somewhere below has the same name. Is there a way to
force my script to look in lib/site-packages before the script's
folder? I can't rename these folders since Git will get confused if I
do.
In other words, I have a package called LRSignature installed, but in
a subdir of my script's directory is another LRSignature folder which
is not an actual Python package. Python sees this non-package folder


It shouldn't. Python doesn't look into nested folders unless you 
explicitly add them to the search path. Here's an example:



[steve@sylar ~]$ mkdir math
[steve@sylar ~]$ echo "print('spam')" > math/module.py
[steve@sylar ~]$ python
Python 2.5 (r25:51908, Nov  6 2007, 16:54:01)
[GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import math  # Can we still see the standard math module?
>>> math.sin(1)
0.8414709848078965
>>> import module  # How about the other module?
Traceback (most recent call last):
  File "", line 1, in 
ImportError: No module named module


So as you can see, the math subdirectory and its content is ignored by 
Python because it isn't explicitly part of the search path. So I don't 
understand why you are seeing this problem unless you are cd'ing into 
the LRSignature folder first.




first and so, quite rightly, throws an exception that the class of the
package I want, LRSignature.Sign, does not exist. It exists in the
site-packages copy, but Python won't overlook the nearer copy. How do
I make it do this? Thanks.



The *right* solution is to fix the name clash, which may or may not 
involve changing the name of something. But I can't tell what, since I 
don't understand why you are having this problem.


But if you can't fix the problem, you can cover it up by manipulating 
the import search path. It is exposed as sys.path, and it is just a list 
of places to look in the specific order given.





--
Steven

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


[Tutor] force imports from site-packages?

2011-10-06 Thread Alex Hall
Hi all,
I have managed to get a couple of packages in site-packages which
share names with some folders in the same directory as a program, or
at least a subdir somewhere below has the same name. Is there a way to
force my script to look in lib/site-packages before the script's
folder? I can't rename these folders since Git will get confused if I
do.
In other words, I have a package called LRSignature installed, but in
a subdir of my script's directory is another LRSignature folder which
is not an actual Python package. Python sees this non-package folder
first and so, quite rightly, throws an exception that the class of the
package I want, LRSignature.Sign, does not exist. It exists in the
site-packages copy, but Python won't overlook the nearer copy. How do
I make it do this? Thanks.

-- 
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


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

2011-10-06 Thread Steven D'Aprano

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:


[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)]



--
Steven
___
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-06 Thread Dave Angel

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]


Does this help?

--

DaveA

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


Re: [Tutor] basic problem

2011-10-06 Thread Alan Gauld

On 06/10/11 23:11, ADRIAN KELLY wrote:


can someone spell out to me in (simply if possible) what this programme
is doing.


Mac has done that pretty well.

However, let me add a couple of comments:


# this programme will adding something to everything the user
#types by using a for loop


This comment does not describe what the program actually does.
In  fact the program simply echoes what the user types in.
Well. Ok I suppose it adds a space at the front, but it goes
about it in a very long winded way!


#set values
new_message=" "
VOWELS="AEIOU"
message=raw_input ("Enter a message: ")
for letter in message:
if letter.lower() not in VOWELS:
new_message = new_message+letter
print new_message


In Python = is not a test or statement as it is in math.
For that we use ==. A single = sign is an assignment.

So

x = 42  means that x takes on the value 42.
x = 42 + 1  means x takes on the value 42+1, or 43

(Some other languages use another symbol for this which in my opinion is 
better, but sadly Python doesn't )


So

 new_message = new_message+letter

means new_message becomes the old value of new_message plus the value of 
letter(which changes on each cycle through the for loop)


This is such a common idiom that, as Mac said, Python provides a 
shorthand way of expressing it:


new_message += letter

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] basic problem

2011-10-06 Thread Mac Ryan
On Thu, 6 Oct 2011 22:11:37 +
ADRIAN KELLY  wrote:


> can someone spell out to me in (simply if possible) what this
> programme is doing.  i understand the concept of the "for" loop and
> what its doing with the message the user enters.  i just cant
> understand the VOWELS and how it keeps adding 1 letter to the
> message. thanks for looking A
>  
>   
> # this programme will adding something to everything the user
> #types by using a for loop

This program might have been intended to to so, but it actually does
not. Have you tried running it?

> #set values
> new_message=" "
> VOWELS="AEIOU"

This last line defines what vowels are. It defines it as a string, but
keep in mind that strings in python can be both handled like individual
object like in "print(text)" or like an ordered collection of
letters, like in "for letter in message" (more on this in a moment).

> message=raw_input ("Enter a message: ")
> for letter in message:

This is an example of the second way of handling strings (the proper
name for what is going on here is "iteration"): the last line will
instruct python to take one letter at a time, from left to right, from
the message, and do with it whatever is indented here below.

> if letter.lower() not in VOWELS:

This is still an example of handling strings as ordered collections of
letters: basically it says to check if any letter in "VOWELS" is the
same as the letter being processed, in lower case.

Actually this is also an example of pointless operation: VOWELS has
been defined as "AEIOU" which are all capital letters. Since the
comparison is done between a letter from the user input in lower case
("letter.lower()") the condition will always be True, so this line
might well not be there.

> new_message = new_message+letter

The idea behind this line is that you keep adding the letter to the end
of the message. The line works, but it's not an example of good python
code. An improvement might be to use:

new_message += letter

which is more common and more readable. But in python one seldom does
this kind of operation because strings are "immutable objects". In
other words even if the logic behind is to "add a character" what is
really going on behind the scenes is that the old version of
"new_message" is destroyed, and a new one will take its place. While
this is not a problem in this simple example, it is an expensive
process, so often it is preferable to store the various "additions" in
a list and merge them together only once, at the very end.
The syntax is:

''.join(['a', 'b', 'c'])

so in your program you would probably initialise an empty list first,
and then append the letters at each loop:

pool = []
for letter in message:
pool.append(letter)
print ''.join(pool)

HTH!

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


[Tutor] basic problem

2011-10-06 Thread ADRIAN KELLY



 Hi all,
can someone spell out to me in (simply if possible) what this programme is 
doing.  i understand the concept of the "for" loop and 
what its doing with the message the user enters.  i just cant understand the 
VOWELS and how it keeps adding 1 letter to the message.
 
thanks for looking
A
 
  
# this programme will adding something to everything the user
#types by using a for loop
#set values
new_message=" "
VOWELS="AEIOU"
message=raw_input ("Enter a message: ")
for letter in message:
if letter.lower() not in VOWELS:
new_message = new_message+letter
print new_message
  ___
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-06 Thread lina


>
>
> 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,


>writeonefiledata(base+**OUTFILEEXT, results)
>
> def fetchonefiledata(inname):
>infile = open(inname)
>text = infile.readlines()
>return text[LINESTOSKIP:]
>
> def writeonefiledata(outname):
>outfile = open(outname, "w")
>...process the results as appropriate...
>(since you didn't tell us how multiple tokens were to be displayed)
>
> if __name__ == "__main__":
>dofiles(".") #or get the top directory from the sys.argv variable,
> which is set from command line.
>
>
>
>  --
>
> DaveA
>
>


-- 
Best Regards,

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


Re: [Tutor] map one file and print it out following the sequence

2011-10-06 Thread Prasad, Ramit
>attached the clumsy script just to prove what I said is true. and at present I 
>want to "push" myself on the way of >using python. so I can actually learn 
>something.  

I think you were close to the solution. It seemed like all you needed to do was 
write the output to file correctly (which you are learning in one of your other 
threads)

>For this one, the python programming I will come back later, just put it aside 
>for a short while.
Just do not forget to come back to it. Coming back often gets forgotten ;)

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-06 Thread Andreas Perstinger

On 2011-10-06 16:11, lina wrote:

I still don't know how to (standard) convert the list values to a string.

def writeonefiledata(outname,results):
 outfile = open(outname,"w")
 for key, value in results.items():
 print(value)
 outfile.write(str(results[key]))
Is it a wrong way?


There isn't really a wrong way because it's completely up to you how the 
list (or its elements) should be represented. As you will have noticed, 
"str(list)" just gives you the standard representation in Python (all 
list elements separated by a comma inside square brackets):

>>> str([1, 2, 3, 4])
'[1, 2, 3, 4]'

But I think you want something else because earlier you wrote:

On 2011-10-04 18:38, lina wrote:
> For file:
>
> aaEbb
> aEEbb
> EaEbb
> EaEbE
>
> the expected output is
>
> 2 1 0 1

(Although I suppose, as Alan already noticed, that you've forgotten to 
mention the third column and the output should be: 2 1 4 0 1)


So you have to build your own string. You could use the join method. It 
takes a list and concatenates the list elements to a string separated by 
the given string object:

>>> "".join(["1", "2", "3"])
'123'
>>> "-".join(["1", "2", "3"])
'1-2-3'

The problem now is that "join" will only join the list elements if they 
are strings but in your case the list elements are integers. You have to 
convert them before you can concatenate them. In Python that's usually 
the job of a list comprehension but I'm not sure if you already know 
what that is. So for a start you can also use a for-loop:

>>> l = []
>>> for number in [1, 2, 3]:
... l.append(str(number))
...
>>> l
["1", "2", "3"]

You must also be aware that when you write your new strings into a file 
that "write" doesn't append a newline ("\n") to each string (in contrast 
to "print"). So you have to do it manually if you don't want to write 
just a long list of numbers.


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-06 Thread lina
On Thu, Oct 6, 2011 at 4:33 AM, Prasad, Ramit wrote:

> >>yes, you're iterating over the keys of a dictionary.  Since it only has
> the key "E", that's what you get.  Try printing dir(results) to see what
> methods might return something other than the key.  Make the language work
> for you.
>
> >Sorry I am not smart.  value?
>
> Dictionaries {} are containers for key/value based pairs like { key :
> value, another_key : value(can be same or repeated) }
>
> For example:
> {'B': [0, 0, 0, 0, 0, 0], 'E': [2, 1, 4, 0, 1, 0]}
> The keys here are 'B' and 'E'. The values here are [0, 0, 0, 0, 0, 0] (for
> key 'B') and [2, 1, 4, 0, 1, 0] (for key 'E')
>
> You can get the value of a dictionary by doing: value = dictionary[key]
> You can set the value of a dictionary by doing: dictionary[key] = value
>
> You will get an exception if you try to get a value for a key that is not
> in the dictionary.
> >>> dictionary = {'B': [0, 0, 0, 0, 0, 0], 'E': [2, 1, 4, 0, 1, 0]}
> >>> dictionary['F']
> KeyError: 'F'
>
> You can iterate over dictionaries in a couple ways
> 1-
> for key in dictionary: # also the same as
>  # for key in dictionary.keys()
>value = dictionary[ key ]
> 2-
> for key, value in dictionary.iteritems():
>< do something here with >
>
Now finally figure out what's the string, list and dictionary.
May I ask a further question:

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

How can I get the value of
set(a['E'])+set(a['B'])

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

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
>



-- 
Best Regards,

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


Re: [Tutor] String switch

2011-10-06 Thread bob gailer

On 10/5/2011 5:51 PM, Christopher King wrote:
There is a program that will open another program, write code at the 
top of the program. The code at the top will cause the program to 
print all strings afterwards in swap case. 


What you've told us so far may make total sense to you, but it is not 
enough information for us to act on.


We need (at least) an example of a program that you want to modify.

From that we /might /be able to tell what you mean by "print" and "all 
strings".


I can hear you now "what's the matter with these guys - what part of 
print and strings do they not understand?".


Humor us. It is not a lack of understanding. "print and strings" have(to 
me)  multiple meanings.


Currently we are guessing, as Alan has done, and we may be way off the 
mark. Guessing takes extra time and energy.


Having said that I will offer an alternative guess: assuming you are 
using Python 3 and you are using the built-in print function to "print 
strings",  then you could redefine print  by ading (at the top) (untested):


def print(something, **args): __builtins__.print(something.swapcase(), 
**args)


You can't do this with Python 2, as print is a statement and can't be 
redefined. You could alter the program - find all occurrences of "print" 
used as a statement, and replace them with a function call. So


print "asdf" would become swapprint("asdf")

and at the top you'd add (untested):

def swapprint(something): print something.swapcase()

--
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] map one file and print it out following the sequence

2011-10-06 Thread lina
On Thu, Oct 6, 2011 at 10:48 PM, Prasad, Ramit wrote:

> >I attached the two files.
>
> Although I will help you, I will not do the work for you. If you are still
> having errors, please include your script, python version, and the problem
> (if you are getting an error include the full error).
>

Thanks for your help.

Actually this one was solved by a clumsy bash script long time ago,

attached the clumsy script just to prove what I said is true. and at present
I want to "push" myself on the way of using python. so I can actually learn
something.

For this one, the python programming I will come back later, just put it
aside for a short while.

#for i in 3 4 5 6 7 8 9 10
#2
#do
#  sed '
 #/H01/ d
 #/H02/ d
 #/H03/ d
 #/H04/ d'  < proAB_processed_$i.pdb > proAB_processed.pdb
#awk '{
#if($4 == "CUR")
#a[i++] = $0
#}
#END{
#print a[6]
#print a[7]
#print a[8]
#print a[9]
#print a[5]
#print a[13]
#print a[33]
#print a[34]
#print a[12]
#print a[2]
#print a[11]
#print a[1]
#print a[10]
#print a[14]
#print a[15]
#print a[16]
#print a[17]
#print a[18]
#print a[19]
#print a[20]
#print a[21]
#print a[22]
#print a[23]
#print a[24]
#print a[4]
#print a[28]
#print a[0]
#print a[27]
#print a[3]
#print a[26]
#print a[31]
#print a[32]
#print a[25]
#print a[29]
#print a[30]
#}' proAB_processed.pdb > try.pdb



>
> 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
>



-- 
Best Regards,

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


Re: [Tutor] map one file and print it out following the sequence

2011-10-06 Thread Prasad, Ramit
>I attached the two files.

Although I will help you, I will not do the work for you. If you are still 
having errors, please include your script, python version, and the problem (if 
you are getting an error include the full error).

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-06 Thread lina
On Thu, Oct 6, 2011 at 1:39 PM, Andreas Perstinger <
andreas.perstin...@gmx.net> wrote:

> On 2011-10-06 05:46, lina wrote:
>
>> On Thu, Oct 6, 2011 at 4:33 AM, Prasad, 
>> Ramit
>> >wrote:
>>
>>>  Dictionaries {} are containers for key/value based pairs like { key :
>>>
>>>  value, another_key : value(can be same or repeated) }
>>>
>>>  For example:
>>>  {'B': [0, 0, 0, 0, 0, 0], 'E': [2, 1, 4, 0, 1, 0]}
>>>  The keys here are 'B' and 'E'. The values here are [0, 0, 0, 0, 0, 0]
>>> (for
>>>
>>  ^^**^^^
>
>>  key 'B') and [2, 1, 4, 0, 1, 0] (for key 'E')
>>>
>>
>>  def writeonefiledata(outname,**results):
>> outfile = open(outname,"w")
>> for key in results:
>> return outfile.write(results[key])
>>
>> $ python3 counter-vertically-v2.py
>> Traceback (most recent call last):
>>   File "counter-vertically-v2.py", line 43, in
>> dofiles(".")
>>   File "counter-vertically-v2.py", line 12, in dofiles
>> processfile(filename)
>>   File "counter-vertically-v2.py", line 29, in processfile
>> writeonefiledata(base+**OUTFILEEXT,results)
>>   File "counter-vertically-v2.py", line 39, in writeonefiledata
>> return outfile.write(results[key])
>> TypeError: must be str, not list
>>
>^
> The error message tells you, that "results[key]" is a list but "write" just
> excepts a string. (see Ramit's explanation above).
> You have to convert the list values to a string.
>
I still don't know how to (standard) convert the list values to a string.

def writeonefiledata(outname,results):
outfile = open(outname,"w")
for key, value in results.items():
print(value)
outfile.write(str(results[key]))
Is it a wrong way?

Thanks all for the help.


> BTW: You shouldn't return the write operation because that will exit your
> function after the first iteration.



>
>
>  def writeonefiledata(outname,**results):
>> outfile = open(outname,"w")
>> for key, value in results.iteritems():
>> return outfile.write(key)
>>
>> $ python3 counter-vertically-v2.py
>> Traceback (most recent call last):
>>   File "counter-vertically-v2.py", line 43, in
>> dofiles(".")
>>   File "counter-vertically-v2.py", line 12, in dofiles
>> processfile(filename)
>>   File "counter-vertically-v2.py", line 29, in processfile
>> writeonefiledata(base+**OUTFILEEXT,results)
>>   File "counter-vertically-v2.py", line 38, in writeonefiledata
>> for key, value in results.iteritems():
>> AttributeError: 'dict' object has no attribute 'iteritems'
>>
>
> In Python 3 there is no "dict.iteritems()" any more:
> http://docs.python.org/py3k/**whatsnew/3.0.html#views-and-**
> iterators-instead-of-lists
> Use "dict.items()" instead
>
> Bye, Andreas
>
>
> __**_
> 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-06 Thread lina
On Thu, Oct 6, 2011 at 5:18 PM, Alan Gauld wrote:

> On 06/10/11 04:54, lina wrote:
>
> If you use IDLE, the standard IDE that comes with Python, you should
>>find that hitting tab (or pausing briefly) in a file editor will
>>bring up a pick list of options.
>>
>> Just tried the  idle-python2.6,
>> Q1: Is it bound with certain python version, such as for python3, do I
>> need get idle-python3 ?
>>
>
> Yes, you need IDLE for V3.
> IDLE is a python program itself so in theory you could run earlier versions
> but the version for V3 uses V3 syntax so you need to match the versions. If
> you are using Linux your package manager should have an IDLE v3 package
> available.
>
>
>
>  Q2: how to use the last command I input, such as I typed something wrong
>> and I wanna correct in the next step.
>>
>
> Use Alt-p(previous) to go back through history
> Use Alt-n(next) to go forward.
> You can also cursor up to the command you want(or use the mouse) and hit
> return to bring it into the prompt.

Thanks, this is helpful. ^_^

>
>
>  seems no much difference with using terminal directly. I might be wrong
>> here.
>>
>
> You get syntax colouring and auto indentation as well as the tool tips.
> There is a slight "bug" in the interactive window in that the alignment is
> off for some statements(*) but if typing code into a file it all works fine.
>
>
> (*)
> >>> if True:
>x = 42
> else:
>y = 42
>
>
> Notice the else is out of line with if because of the >>>
>
>
> Other features of IDLE are a graphical debugger that allows you to step
> through your code line by line, A syntax checker, a class browser(for when
> you get into objects). There is also a "code context" option but I have no
> idea what it does!


Thanks for your time.

>
>
> --
> 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-06 Thread Alan Gauld

On 06/10/11 04:54, lina wrote:


If you use IDLE, the standard IDE that comes with Python, you should
find that hitting tab (or pausing briefly) in a file editor will
bring up a pick list of options.

Just tried the  idle-python2.6,
Q1: Is it bound with certain python version, such as for python3, do I
need get idle-python3 ?


Yes, you need IDLE for V3.
IDLE is a python program itself so in theory you could run earlier 
versions but the version for V3 uses V3 syntax so you need to match the 
versions. If you are using Linux your package manager should have an 
IDLE v3 package available.




Q2: how to use the last command I input, such as I typed something wrong
and I wanna correct in the next step.


Use Alt-p(previous) to go back through history
Use Alt-n(next) to go forward.
You can also cursor up to the command you want(or use the mouse) and hit 
return to bring it into the prompt.



seems no much difference with using terminal directly. I might be wrong
here.


You get syntax colouring and auto indentation as well as the tool tips.
There is a slight "bug" in the interactive window in that the alignment 
is off for some statements(*) but if typing code into a file it all 
works fine.



(*)
>>> if True:
x = 42
else:
y = 42


Notice the else is out of line with if because of the >>>


Other features of IDLE are a graphical debugger that allows you to step 
through your code line by line, A syntax checker, a class browser(for 
when you get into objects). There is also a "code context" option but I 
have no idea what it does!


--
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 me in this programme

2011-10-06 Thread Alan Gauld

On 06/10/11 08:15, Praveen Singh wrote:


CountWords("google")
[('e',1),('g',2),('l',1),('o',2)]

by the level of this question you can easily understand i am new in
python.the problem i am facing is how to make set of word and count(e,1)
and add this to list??


Some clues:

a for loop will iterate over a string:

for letter in "google":
print letter


A dictionary holds a value against a key.
A key can be a letter. The corresponding value can be a number.


Does that help?
--
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] String switch

2011-10-06 Thread Alan Gauld

On 05/10/11 22:51, Christopher King wrote:

There is a program that will open another program, write code at the top
of the program. The code at the top will cause the program to print all
strings afterwards in swap case.


If I understand this correctly you have  mainprog.py and subprog.py.
And you want mainprog.py to modify subprog.py such that when you run 
subprog.py it swaps case of the output?


An easier way to achieve that would be to write a program that simply 
translates its input into a swapcased output. Then you can redirect the 
output of subprog.py into that program:


python subprog.py | python swapcase.py

Now, the fact that these are two python scripts rather than executables 
complicates things slightly so you probably need to experiment with the 
shell syntax to make it work, but that would be my approach.


--
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] declaring a blank interger

2011-10-06 Thread Hugo Arts
On Thu, Oct 6, 2011 at 7:09 AM, Mike Nickey  wrote:
> Hey all,
> I'm sorry for such a silly question but I want to declare a blank integer
> for x.  What I have is a base for a program that I'm working on for fun.
> Yes, for fun.

1) no question is silly 2) Programming is fun, many people have it as
a hobby! That's not weird at all.

> The long term goal is to create a way to log items that are currently being
> done via pencil & paper and make this easier for me and my team.
> What I am running into though is how can I declare a variable to use that is
> blank at start but then gets populated later on.
> Here is what I have so far.
> Thanks in advance

The question you need to ask yourself is "why?" If the variable has no
value, why would you declare it at all? Why not just wait until you
have a value for it? It is common for languages such as C to declare
all your variables beforehand, but in python there is really no need
to do that.

For one, variables in python don't have a type. Values do, but
variables do not. So there is no need to tell python beforehand what
types your variables will have. You can easily get rid of both of the
variable declarations at the top of that program, and assign them when
you have a value for them:

def get_username():
return raw_input("Enter your name: ")

def get_usernumber():
return raw_input("Enter a number: ")

username = get_username()
x = get_usernumber()

Note that raw_input, as a function, returns a value of type str
(that's a string). But for a number, it would make more sense to have
a value with type int. So we should adapt our get_usernumber function
to convert our str value into an int value:

def get_usernumber():
return int(raw_input("Enter a number: "))

very simple, right? Now, the next problem you will run into, is what
happens when the user doesn't actually enter a number? I suggest you
try it. the int() function won't be very happy about it and raise an
exception. You'll have to catch that exception, print out some error
message, and ask for another number, until the user gets it right. But
I'll leave that part up to you.

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


[Tutor] Help me in this programme

2011-10-06 Thread Praveen Singh
this is one of the problem based on list which i found at pyschool.

CountWords("google")
[('e',1),('g',2),('l',1),('o',2)]

by the level of this question you can easily understand i am new in
python.the problem i am facing is how to make set of word and count(e,1) and
add this to list??
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor