Re: [Tutor] Python Homework

2016-05-02 Thread Danny Yoo
On Sun, May 1, 2016 at 1:47 AM, Ben Finney  wrote:
> Katie Tuite  writes:
>
> You'll need to write only plain text email (no attached documents, no
> “rich text”) for the information to survive correctly. This is always
> good practice for any technical discussion forum.

Hi Katie,

Also, try to present as much background as possible.  In particular:
were there other homework problems that you were able to solve
successfully?  And for the problem you're showing us: was it all
greek, or did certain parts make sense?  Were there particular
problem-solving strategies that you tried that didn't work out?

The more you can say and verbalize, that might help to pinpoint the
confusion.  With that, we'll try to tailor our answers for you rather
than the problem specifically.


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


Re: [Tutor] simple regex question

2016-05-02 Thread Danny Yoo
On Sun, May 1, 2016 at 9:49 AM, bruce  wrote:

> I've created a test regex. However, after spending time/google.. can't
> quite figure out how to then get the "complete" line containing the
> returned regex/pattern.
>
> Pretty sure this is simple, and i'm just missing something.


A few people have mentioned "beautiful soup"; I agree: you should look
into using that instead of regular expressions alone.

The docs for beautiful soup are pretty good, and should help you on your way:

https://www.crummy.com/software/BeautifulSoup/bs4/doc/

This is not to say that regular expressions are useless.  Far from it!
 You can tell beautiful soup to search with regexes:

https://www.crummy.com/software/BeautifulSoup/bs4/doc/#a-regular-expression
https://www.crummy.com/software/BeautifulSoup/bs4/doc/#find-all

For your case, you can probably say something like:

soup.find_all(id=pattern)

where the pattern is precisely the regex in your original program.
You can then get the results back as structured portions of the HTML
tree.  The point is that if you use a parser that understands HTML,
you can do table-row-oriented things without having to worry about the
actual string lines.


That's often a much better situation than trying to deal with a flat
string and trying to use regular expressions to parse tree structure.
You do not want to write code that contributes to the summoning of the
Nameless One.  
(http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454,
http://blog.codinghorror.com/parsing-html-the-cthulhu-way/)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Dictionary Question

2016-05-02 Thread cs

On 03May2016 00:56, Jason N.  wrote:

Thank you all for your responses. 
A quick follow up, what is the best way to make dictionary requests case 
in-sensitive? For example, "Apple and "apple" should bring back the same 
dictionary response. Thank you.


There are a few ways depending what your more fine grained objectives are. But 
they all tend to revolve around "normalising" the keys, which is a common 
practice for many things where multiple values are considered the same: in your 
case upper and lower case.


So the easy thing is to always convert to lower case (or upper case, but lower 
case is less SHOUTY). Eg:


 def save(d, key, value):
   d[key.lower()] = value

so the normalising function here is d.lower.

Usually you'd be making yourself a mapping class of some kind: an object which 
behaves like a dictionay:


 https://docs.python.org/3/glossary.html#term-mapping

And internally it would usually have a dictionary for storage. Completely 
untested example code:


 class CaseInsensitiveMapping:

   def __init__(self):
 self._d = {}

   def __getitem__(self, key):
 return self._d[key.lower()]
   
   def __setitem__(self, key, value):

 self._d[key.lower()] = value

and so forth for the other special ("dunder" in Pythonspeak) methods used to 
implement a mapping:


 https://docs.python.org/3/reference/datamodel.html#emulating-container-types

From the outside:

 cimap = CaseInsensitiveMapping()
 cimap['X']=1
 print(cimap['x'])

should print 1.

Now having sketched a trivial example like this, you might need to be more 
elaborate depending on youruse case. For example, some mappings like this one 
preserve the case used to insert the original key. So while ['X'] and ['x'] 
would both find the value 1, they .keys() method with recite 'X' because that 
was the specific string used to put the 1 into the mapping. That would make the 
internal implementation more complicated.


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


Re: [Tutor] Dictionary Question

2016-05-02 Thread isaac tetteh
If only I understand what you mean. You can just make all the values in the 
dictionary lower, upper or capitalized. Then if you want take an input or 
whatever you want to do with it just use .lower() or .upper() or .capitalized() 
to convert it to what is in the dictionary. Maybe someone has a better way to 
do it :) 


Sent from my iPhone

> On May 2, 2016, at 7:59 PM, Jason N. via Tutor  wrote:
> 
> Thank you all for your responses. 
> A quick follow up, what is the best way to make dictionary requests case 
> in-sensitive? For example, "Apple and "apple" should bring back the same 
> dictionary response. Thank you. 
> 
>On Monday, May 2, 2016 6:57 PM, Bob Gailer  wrote:
> 
> 
> 
> 
>> On May 2, 2016 5:27 PM, "Jason N. via Tutor"  wrote:
>> 
>> Hello,
>> Wanted to ask if its possible to have a dictionary that can be looked up by 
>> either values?
>> For example, mydic = {"A: "Apple", "B": "Banana"}
>> When user inputs "A" I want "Apple" to come. But if the user enter "Apple" I 
>> want "A" to respond.
> I think this would depend on how big the data set is and how often you want 
> to look things up.
> Two other Solutions: 
> Create a class which internally manages two dictionaries.
> If things are really big create a database using for example sqlite.
> 
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Dictionary Question

2016-05-02 Thread Jason N. via Tutor
Thank you all for your responses. 
A quick follow up, what is the best way to make dictionary requests case 
in-sensitive? For example, "Apple and "apple" should bring back the same 
dictionary response. Thank you. 

On Monday, May 2, 2016 6:57 PM, Bob Gailer  wrote:
 
 

 
On May 2, 2016 5:27 PM, "Jason N. via Tutor"  wrote:
>
> Hello,
> Wanted to ask if its possible to have a dictionary that can be looked up by 
> either values?
> For example, 
> mydic = {"A: "Apple", "B": "Banana"}When user inputs "A" I want "Apple" to 
> come. But if the user enter "Apple" I want "A" to respond.
I think this would depend on how big the data set is and how often you want to 
look things up.
Two other Solutions: 
Create a class which internally manages two dictionaries.
If things are really big create a database using for example sqlite.

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


Re: [Tutor] Dictionary Question

2016-05-02 Thread Bob Gailer
On May 2, 2016 5:27 PM, "Jason N. via Tutor"  wrote:
>
> Hello,
> Wanted to ask if its possible to have a dictionary that can be looked up
by either values?
> For example,
> mydic = {"A: "Apple", "B": "Banana"}When user inputs "A" I want "Apple"
to come. But if the user enter "Apple" I want "A" to respond.
I think this would depend on how big the data set is and how often you want
to look things up.
Two other Solutions:
Create a class which internally manages two dictionaries.
If things are really big create a database using for example sqlite.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Dictionary Question

2016-05-02 Thread Alan Gauld via Tutor
On 02/05/16 22:55, isaac tetteh wrote:
> 
> For some reason i cant find reply all . But try this 
> for key, value in mydic.items():
>   If A==value:
>Print key

or as a function:

def findKey(dct, val):
   for k,v in dct.items():
  if v == val:
 return k

mydic = {"A: "Apple", "B": "Banana"}

print( findKey(mydic,'Apple') )   # -> 'A'

The problem is that while keys are unique, values
might not be, so what do you do if multiple keys
share the same value?

You could use a comprehension:

def findKeys(dct,val):
keys = [k for k,v in dct.items() if v == val]
return keys

But if you are only interested in one of them then
it's down to you to figure out which!

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] Detect the folder of a file

2016-05-02 Thread Kanika Murarka
Thank you everyone !

My situation was to check the indentation of every python file via a script.
I think looking for bin/activate will work.

On 28 April 2016 at 23:08, Alan Gauld via Tutor  wrote:

> On 28/04/16 11:11, Steven D'Aprano wrote:
>
> > You know, some day I must learn why people use virtual environments.
>
> Me too :-)
>
> My co-author included a section in one of her chapters of our
> recent book, and I duly played with them while reviewing that
> chapter. But at the end I just deleted it all and
> thought "H?"
>
> I know why they are useful in theory, but I've never found
> a practical use for them myself.
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Dictionary Question

2016-05-02 Thread isaac tetteh
Sorry for the if statement the correct statement should be "if 'apple' ==value:"

Sent from my iPhone

> On May 2, 2016, at 4:58 PM, isaac tetteh  wrote:
> 
> 
> For some reason i cant find reply all . But try this 
> for key, value in mydic.items():
>  If A==value:
>   Print key
> Nb: use iteritems() if using python2
> 
> Sent from my iPhone
> 
>> On May 2, 2016, at 4:29 PM, Jason N. via Tutor  wrote:
>> 
>> Hello,
>> Wanted to ask if its possible to have a dictionary that can be looked up by 
>> either values?
>> For example, 
>> mydic = {"A: "Apple", "B": "Banana"}When user inputs "A" I want "Apple" to 
>> come. But if the user enter "Apple" I want "A" to respond.
>> Please let me know the best way to handle this type cause instead of just 
>> created duplicate entries to cover all possibilities. Thank you.
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> https://mail.python.org/mailman/listinfo/tutor
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Dictionary Question

2016-05-02 Thread isaac tetteh

For some reason i cant find reply all . But try this 
for key, value in mydic.items():
  If A==value:
   Print key
Nb: use iteritems() if using python2

Sent from my iPhone

> On May 2, 2016, at 4:29 PM, Jason N. via Tutor  wrote:
> 
> Hello,
> Wanted to ask if its possible to have a dictionary that can be looked up by 
> either values?
> For example, 
> mydic = {"A: "Apple", "B": "Banana"}When user inputs "A" I want "Apple" to 
> come. But if the user enter "Apple" I want "A" to respond.
> Please let me know the best way to handle this type cause instead of just 
> created duplicate entries to cover all possibilities. Thank you.
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Dictionary Question

2016-05-02 Thread Jason N. via Tutor
Hello,
Wanted to ask if its possible to have a dictionary that can be looked up by 
either values?
For example, 
mydic = {"A: "Apple", "B": "Banana"}When user inputs "A" I want "Apple" to 
come. But if the user enter "Apple" I want "A" to respond.
Please let me know the best way to handle this type cause instead of just 
created duplicate entries to cover all possibilities. Thank you.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor