Re: [Tutor] importing my module imports only one function

2014-01-30 Thread Dave Angel
 Ian D  Wrote in message:
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
> 

1) you forgot to escape the backslashes in your module path.
 Either use forward slashes,  double them, or use a raw string.
 

2) Perhaps you have more than one such file in your path.
 Temporarily add a print to it, or examine modtest.__file__.  Less
 likely,  you might have a spurious. pyc file lying around.
 

And if you post in text mode, I might be able to quote some of
 your code and be more specific. 

-- 
DaveA

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


Re: [Tutor] getUncPath(mappedDrive)

2014-01-30 Thread eryksun
On Thu, Jan 30, 2014 at 8:13 AM, danz  wrote:
> I apologize to all, but my above code won't work with paths that have
> embedded spaces.  It also turns out that the "net use" command inserts a
> carriage-return/line-feed between the Path and Network fields when the last
> character position of the Path exceeds 80 characters.
>
> My above approach seemed  simpler, but that's just because I posted it
> before adequate testing.  Unfortunately, in order to make it actually work
> for all cases, the implementation becomes more complex and begins to look
> like a kludge.  So my recommendation is to use Tim's ctypes approach.

You could use WMI's Win32_LogicalDisk class [1]. One way is to parse
CSV output from wmic.exe [2]:

wmic LogicalDisk WHERE "DriveType=4" ^
GET DeviceID, ProviderName /format:csv

You can parse the output using the csv module. Or use Tim's wmi module [3]:

import wmi

DRIVE_REMOTE = 4

def available_network_drives():
net_drives = dict()
c = wmi.WMI()
for drive in c.Win32_LogicalDisk(DriveType=DRIVE_REMOTE):
net_drives[drive.DeviceID] = drive.ProviderName
return net_drives

[1] http://msdn.microsoft.com/en-us/library/aa394173
[2] http://ss64.com/nt/wmic.html
[3] http://timgolden.me.uk/python/wmi/index.html
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] importing my module imports only one function

2014-01-30 Thread Alan Gauld

On 30/01/14 15:43, Ian D wrote:

if I create a module called "modtest.py" like this:

import turtle

def square():



def tri():

if __name__ == "__main__":

 tri()

And then call it like this:

import  modtest

modtest.square()
modtest.tri()

why would I just get ability to call the 'square()' function and not the
'tri()' function.


Are you by any chance testing this from inside an IDE and added the 
tri() fuinction after the square one?


If so I'd suggest shutting down the IDE and restarting.
You may be seeing the old module imported prior to your
adding tri()

There is a reload()??? function for reloading modules that have
already been loaded to avoid this kind of thing but I don't
trust it entirely so if in doubt shut the IDE down and
restart...

Or just try running it in the command line interpreter
instead...

Assuming my guess at the cause is correct of course.

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

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


[Tutor] importing my module imports only one function

2014-01-30 Thread Ian D
if I create a module called "modtest.py" like this:
 
import turtle
 
def square():
for i in range(4):
turtle.fd(100)
turtle.lt(90)
 
def tri():
for i in range(3):
turtle.fd(100)
turtle.lt(120)
 
 
if __name__ == "__main__":

tri()
 
And then call it like this:
 
import sys
sys.path.append("D:\python\modules")
import  modtest
 
modtest.square()
modtest.tri()
 
why would I just get ability to call the 'square()' function and not the 
'tri()' function.
 
I get a square and a trace back:
 
line 7, in 
modtest.tri()
AttributeError: 'module' object has no attribute 'tri'
 
 
  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] getUncPath(mappedDrive)

2014-01-30 Thread danz
I apologize to all, but my above code won't work with paths that have
embedded spaces.  It also turns out that the "net use" command inserts a
carriage-return/line-feed between the Path and Network fields when the last
character position of the Path exceeds 80 characters.

My above approach seemed  simpler, but that's just because I posted it
before adequate testing.  Unfortunately, in order to make it actually work
for all cases, the implementation becomes more complex and begins to look
like a kludge.  So my recommendation is to use Tim's ctypes approach.



--
View this message in context: 
http://python.6.x6.nabble.com/Tutor-getUncPath-mappedDrive-tp4652304p5045785.html
Sent from the Python - tutor mailing list archive at Nabble.com.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] imaplib and mutt flags

2014-01-30 Thread Bill Campbell
On Thu, Jan 30, 2014, Alan Gauld wrote:
> On 30/01/14 02:45, Bill Campbell wrote:
>> I'm writing a python script which uses imaplib
>
> We don't get many messages about imaplib so I'm
> not sure how many folks here know about it.

I've used it off and on for years, but still don't know much
about it :-).

>> After some trial and error, I've figured out how to remove the
>> \Seen flag from messages I want to look at manually,
>
> Then it's probably a good idea to post at least a code
> extract showing us how you are going about that.
> We work better when we have code to look at...
>
>> have a bit of a problem in that when I open the folder with mutt
>> using direct access to the Maildir folder in the file system,
>> mutt flags these parsed but unseen messages with the 'O' instead
>> of 'N' which is uses for new messages.
>
> We have some mutt users so they may be able to p[itch in here.
>
>> The mail server is using courier-imap with Maildir stores on
>> CentOS Linux.  Mutt access is direct on the file system, not via
>> IMAP.
>
> OK, Final missing piece is which Python version?

On this machine, Python 2.4.6.

It seems my main problem is more related to the way that
courier-imap and mutt handle the Maildir stores.  Mutt
distinguishes unread messages as 'N' where the messages are in
the $folder/new directory and 'O' in $folder/cur, and moves them
from cur to new when one manually changes the flag to 'N'.

The IMAP protocol hides the mail store so one cannot manage the
message files as this depends on the physical storage format,
Maildir, UW IMAP, etc.

The bottom line is that this isn't an imaplib/python problem.

Bill
-- 
INTERNET:   b...@celestial.com  Bill Campbell; Celestial Software LLC
URL: http://www.celestial.com/  PO Box 820; 6641 E. Mercer Way
Voice:  (206) 236-1676  Mercer Island, WA 98040-0820
Fax:(206) 232-9186  Skype: jwccsllc (206) 855-5792

The problems we face today are there because the people who work for a
living are now outnumbered by those who vote for a living. -- Anonymous
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Mastermind Help

2014-01-30 Thread Alan Gauld

On 29/01/14 22:19, Amy Davidson wrote:

I’ve been given an assignment in which I need to recreate

> the game Mastermind.

OK I assume you know how mastermind works?
ie the rules of the game.


I’ve been attempting it for approx a week with little to no progress.

> I was hoping someone would be able to point me in the right direction.

OK, Start by writing down an example of what the finished
game should look like. Maybe a welcome message followed by
a prompt for input. An example input and the games response
and so on until it completes.

That will act as a specification for your program.


here is what I’ve got:

import random


I assume you plan on using random to generate the target pattern?
For now I'd start with a fixed pattern, generating a random one is best 
left to the end otherwise testing behaviour will be slightly more difficult.



def masterMind():
 userGuess = raw_input("Guess my 5 digit password:”)

 while True:
 if len(userGuess) != 5:
userGuess = input("Guess my 5 digit password:”)



When you create a "while True" loop its vital that you provide some way 
to exit. I usually code the exit clause immediately after creating

the loop.

Your loop has no exit so it just loops forever.
If the len() is not 5 you get a prompt and another attempt
but if it does equal 5 the loop just spins silently.

Your next steps should be:
1) Clarify in your mind what the program should do (see above)
2) create a means of exiting the loop
3) create a target to guess
4) start comparing the user input to the target. ( I suggest
you create a second function to do that, you can then test
it in isolation)

Let us know how you get on.

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

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


Re: [Tutor] getUncPath(mappedDrive)

2014-01-30 Thread Tim Golden
On 30/01/2014 11:39, Tim Golden wrote:
> On 29/01/2014 21:58, danz wrote:
>> Tim.
>>
>> I came across your code while searching for a similar need.  Your post was
>> the best I could find on the subject.  Perhaps more importantly, you showed
>> me that going down the ctypes rabbit hole can be less intimidating than I
>> assumed.  Thanks!
> 
> [The OP appears to be replying via nabble to a tutor thread from about
> 18 months ago. I was the person doing most of the talking hence the
> reply to me]

Just by way of an alternative, the code outlined here:

http://timgolden.me.uk/python/win32_how_do_i/show_mapped_drives.html

will produce the same effect as your parsing of the "net use" output.

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


Re: [Tutor] getUncPath(mappedDrive)

2014-01-30 Thread Tim Golden
On 29/01/2014 21:58, danz wrote:
> Tim.
> 
> I came across your code while searching for a similar need.  Your post was
> the best I could find on the subject.  Perhaps more importantly, you showed
> me that going down the ctypes rabbit hole can be less intimidating than I
> assumed.  Thanks!

[The OP appears to be replying via nabble to a tutor thread from about
18 months ago. I was the person doing most of the talking hence the
reply to me]

> 
> My use case was a little different than the original poster's.  I need to
> retrieve the network paths for all of the mapped drives that are currently
> connected.  I chose a different implementation, that seems to work well.  I
> would appreciate any comments you have on this approach.

[... snip subprocess "NET USE" + splitlines ...]

It's a perfectly reasonable approach. The usual caveats would apply:
that you're at the mercy of layout changes in "NET USE" and of i18n
changes to the "OK" text. But both of those are low risk and if it works
for you and you're in control of your environment, then it's fine.

There's something somewhat satisfying in employing the underlying API
for what should be a future-proof solution, but parsing stdout is a
well-established approach as well.

> BTW, Thank you for the information on your website.  Your information and
> pointers to GetDriveType() and GetVolumeInformation() helped me a a lot.

You're welcome. Glad it was useful.


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


Re: [Tutor] Project directory structure

2014-01-30 Thread Steven D'Aprano
On Wed, Jan 29, 2014 at 10:45:53PM +0530, Reuben wrote:
> Hi,
> 
> Do we need to follow any particular directory structure for creating any
> New projects or could we just randomly create a folder containing the
> script of interest?

Yes and no.

If all you're doing is writing a single file script, you don't even need 
a folder at all. Just create it, well, just about anywhere you like.

If you're creating something a little more formal, say you plan to make 
it public, there is a convention for laying out project directories:

myproject
+-- CHANGES.txt
+-- LICENCE.txt
+-- MANIFEST.in  
+-- README.txt
+-- setup.py
+-- src
+-- myproject.py


although the src directory is not compulsory.

If you're creating a package, rather than a single module, then you do 
need to use a special directory structure:

mypackage
+-- __init__.py
+-- __main__.py
+-- cheese.py
+-- eggs.py
+-- spam.py


The above is a package called "mypackage", containing the following 
modules:

mypackage
mypackage.cheese
mypackage.eggs
mypackage.spam

plus two special modules:

__init__.py is needed for Python to recognise this as a package, rather 
than a folder full of files; when you run `import mypackage`, it is 
the code inside __init__.py that runs.

__main__.py is used when you try to run mypackage as an executable file. 
When you run `python -m mypackage` from the shell, it runs the code in 
__main__.py.


But apart from that, pretty much anything goes.



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


Re: [Tutor] Split Method

2014-01-30 Thread Peter Otten
Rafael Knuth wrote:

> Hey there,
> 
> I am having some issues with splitting strings.
> I already know how to split strings that are separated through empty
> spaces:
> 
> def SplitMyStrings():
> Colors = "red blue green white black".split()
> return (Colors)
> 
> print(SplitMyStrings())
> 

> ['red', 'blue', 'green', 'white', 'black']
> 
> ... however I couldn't figure out how to split each character into a list
> item. This is what I want to get as a result:
> 

> ['r', 'e', 'd', 'b', 'l', 'u', 'e', 'g', 'r', 'e', 'e', 'n', 'w', 'h',
> 'i', 't', 'e', 'b', 'l', 'a', 'c', 'k']

>>> colors = "red blue green white black"
>>> list(colors)
['r', 'e', 'd', ' ', 'b', 'l', 'u', 'e', ' ', 'g', 'r', 'e', 'e', 'n', ' ', 
'w', 'h', 'i', 't', 'e', ' ', 'b', 'l', 'a', 'c', 'k']

If you don't want the spaces remove them before

>>> list(colors.replace(" ", ""))
['r', 'e', 'd', 'b', 'l', 'u', 'e', 'g', 'r', 'e', 'e', 'n', 'w', 'h', 'i', 
't', 'e', 'b', 'l', 'a', 'c', 'k']

or while

[c for c in colors if not c.isspace()]
['r', 'e', 'd', 'b', 'l', 'u', 'e', 'g', 'r', 'e', 'e', 'n', 'w', 'h', 'i', 
't', 'e', 'b', 'l', 'a', 'c', 'k']

converting to a list. Note that c.isspace() is true for all whitespace 
chars; use [... if c != " "] if you want to omit " " only.

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


Re: [Tutor] Split Method

2014-01-30 Thread Steven D'Aprano
On Thu, Jan 30, 2014 at 12:11:56PM +0100, Rafael Knuth wrote:
> Hey there,
> 
> I am having some issues with splitting strings.
> I already know how to split strings that are separated through empty spaces:
> 
> def SplitMyStrings():
> Colors = "red blue green white black".split()
> return (Colors)
> 
> print(SplitMyStrings())
> 
> >>>
> ['red', 'blue', 'green', 'white', 'black']
> 
> ... however I couldn't figure out how to split each character into a list 
> item.

list(some_string)

For example:

py> list("hello world")
['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']


Note that the space is considered an ordinary character. If you want to 
ignore spaces:

py> list("hello world".replace(" ", ""))
['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd']


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


[Tutor] Split Method

2014-01-30 Thread Rafael Knuth
Hey there,

I am having some issues with splitting strings.
I already know how to split strings that are separated through empty spaces:

def SplitMyStrings():
Colors = "red blue green white black".split()
return (Colors)

print(SplitMyStrings())

>>>
['red', 'blue', 'green', 'white', 'black']

... however I couldn't figure out how to split each character into a list item.
This is what I want to get as a result:

>>>
['r', 'e', 'd', 'b', 'l', 'u', 'e', 'g', 'r', 'e', 'e', 'n', 'w', 'h',
'i', 't', 'e', 'b', 'l', 'a', 'c', 'k']

I am using Python 3.3.0
Thanks in advance!

All the best,

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


[Tutor] Python Mastermind Help

2014-01-30 Thread Amy Davidson
Hello,

I’ve been given an assignment in which I need to recreate the game Mastermind. 
I’ve been attempting it for approx a week with little to no progress. I was 
hoping someone would be able to point me in the right direction.

here is what I’ve got:

import random

def masterMind():
userGuess = raw_input("Guess my 5 digit password:”)

while True:
if len(userGuess) != 5:
   userGuess = input("Guess my 5 digit password:”)

Much appreciated.

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


Re: [Tutor] getUncPath(mappedDrive)

2014-01-30 Thread danz
Tim.

I came across your code while searching for a similar need.  Your post was
the best I could find on the subject.  Perhaps more importantly, you showed
me that going down the ctypes rabbit hole can be less intimidating than I
assumed.  Thanks!

My use case was a little different than the original poster's.  I need to
retrieve the network paths for all of the mapped drives that are currently
connected.  I chose a different implementation, that seems to work well.  I
would appreciate any comments you have on this approach.

BTW, Thank you for the information on your website.  Your information and
pointers to GetDriveType() and GetVolumeInformation() helped me a a lot.

Dan

---
import subprocess

def available_network_drives():
net_drives = dict()
for line in subprocess.check_output(['net', 'use']).splitlines():
if line.startswith('OK'):
fields = line.split()
net_drives[fields[1]] = fields[2]   # [1] == key, [2] ==
net_path
return net_drives

print available_network_drives()



--
View this message in context: 
http://python.6.x6.nabble.com/Tutor-getUncPath-mappedDrive-tp4652304p5045727.html
Sent from the Python - tutor mailing list archive at Nabble.com.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Python shell wont open IDLE or an exisiting .py files

2014-01-30 Thread shangonichols
I am on Windows 8, Python 3.3.4 and 3.3.3 and all previous versions exhibit the 
same problem on my Windows 8 PC. This problem occurred out of nowhere 
overnight. It was working fine for months until today.

>  I tried to open a file and nothing happened. If I tried to open a .py file
> (any .py file) from an existing instance of IDLE, it briefly flashed up a
> new window and then closed both the new window and the existing window
> (normally it opens the requested in a new window leaving the existing window
> untouched).
>
> If I launch the Python GUI it opens a Python Shell fine. But as soon as I
> try to open a file (including a "new" file), it closes the Shell.
>
> I rebooted the machine. Same problem.
>
> I repaired the Python installation and rebooted. Same problem.
>
> I uninstalled Python. Rebooted. Deleted the Python33 directory entirely.
> Rebooted. Installed Python. Rebooted. Same problem.
>
> Everything else on the system appears to be working just fine.
>
> Any ideas what the problem might be or how else I might go about fixing
> things?








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


Re: [Tutor] imaplib and mutt flags

2014-01-30 Thread Alan Gauld

On 30/01/14 02:45, Bill Campbell wrote:

I'm writing a python script which uses imaplib


We don't get many messages about imaplib so I'm
not sure how many folks here know about it.


After some trial and error, I've figured out how to remove the
\Seen flag from messages I want to look at manually,


Then it's probably a good idea to post at least a code
extract showing us how you are going about that.
We work better when we have code to look at...


have a bit of a problem in that when I open the folder with mutt
using direct access to the Maildir folder in the file system,
mutt flags these parsed but unseen messages with the 'O' instead
of 'N' which is uses for new messages.


We have some mutt users so they may be able to p[itch in here.


The mail server is using courier-imap with Maildir stores on
CentOS Linux.  Mutt access is direct on the file system, not via
IMAP.


OK, Final missing piece is which Python version?

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

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