[Tutor] Importing files not located on the home directory or standardard library

2010-06-17 Thread Independent Learner
I am having trouble figuring out how to import modules not located on the home 
directory or standard library of modules. Meaning say I write a module and 
instead of saving it on the home directory I save in a folder in my desktop. I 
do not know how to retrieve it by way of the import statement. I know it has 
something to do with the PYTHONPATH or .pth,  however I am not sure how to 
configure these, right now I am reading Learning Pythong 3rd ed. by Mark Lutz 
and unfortnately that book has a lot more words then examples so I am usually 
left to figure alot of the concepts myself while rereading word for word to try 
to figure out how the heck it applies to IDLE and practical uses for code. I 
thoroughly understand modules and how to import them when they are in the home 
directory or standard library, thats pretty simple, however like I said I do 
not know how to import files outside of these directories. I am using windows 7 
and python 2.6.5

Lets say I want to load a file called quiz located in C:\Users\Julius 
Hernandez\Desktop\Python2.6.5\Online Course\Chp.4

How would I configure PYTHONPATH or .pth  or what ever to import this file from 
IDLE

I am also having slight trouble understanding module packages as well, though I 
suspect it has something to do with not knowing how to configure PYTHONPATH or 
.pth files

Thanks =) ~Julius H. 


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


[Tutor] help

2010-06-17 Thread KB SU
help
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] pickling/shelve classes

2010-06-17 Thread Eike Welk
Hello Payal!

On Thursday June 17 2010 04:48:19 Payal wrote:
> Hi all,
> Can someone please help in this below?
> 
> class F(object) :
>...: def __init__(self, amt) : self.amt = amt
>...: def dis(self) : print 'Amount : ', self.amt
>...: def add(self, na) :
>...: self.amt += na
>...: F.dis(self)
> 
> pickle.dumps(F)
> gives PicklingError: Can't pickle : it's not found
> as __main__.F

I think it is a bug in IPython: I can do this in regular Python, but in 
IPython I get the same error that you get:

Python 2.6.2 (r262:71600, Mar 29 2010, 15:30:01)
[GCC 4.4.1 [gcc-4_4-branch revision 150839]] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class A(object):pass
...
>>> import pickle
>>> pickle.dumps(A)
'c__main__\nA\np0\n.'
>>>


You should file a bug report in their bug tracker:
http://github.com/ipython/ipython/issues


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


Re: [Tutor] help

2010-06-17 Thread Modulok
Solution: width times height.


On 6/17/10, KB SU  wrote:
> help
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Trouble with sockets...

2010-06-17 Thread Modulok
List,

I'm new to sockets and having trouble. I tried to write a simple
client/server program (see code below). The client would send a string
to the server. The server would echo that back to the client.

PROBLEM:
I can send data to the server, and get data back, but only for the
first call to the 'sendall()' method. If I try to call it in a loop on
the client, to print a count-down, only the first call gets echoed.
Clearly, I'm missing something.

EXAMPLE: (They're on the same physical machine.)
Shell on the server:
modu...@modunix> ./socket_server.py
Listening for clients...
Got connection from 192.168.1.1

Shell on the client:
modu...@modunix> ./latency_client.py 192.168.1.1 2554
0

What I wanted:
[-]modu...@modunix> ./latency_client.py 192.168.1.1 2554
0
1
2


CODE:

# The client program:

import socket
import sys
def main():
'''Send data to a server and receive the data echoed back to us.'''

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect( ('192.168.1.1', 2554) )

for i in range(3):
s.sendall(str(i))
print s.recv(1024)
s.close()

# Execution starts here:
try:
main()
except KeyboardInterrupt:
sys.exit("Aborting.")


# The server program:

import socket
import sys
def main():
'''Listen for a client and echo data back to them.'''

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind( ('192.168.1.1', 2554) )
s.listen(5)

print "Listening for clients..."
while True:
channel, client = s.accept()
print "Got connection from %s" % client[0]
message = channel.recv(1024)
channel.send(message)
channel.close()

# Execution starts here:
try:
main()
except KeyboardInterrupt:
sys.exit("Aborting.")
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] conventions for establishing and saving default valuesfor variables

2010-06-17 Thread Hugo Arts
On Thu, Jun 17, 2010 at 1:19 AM, Steven D'Aprano  wrote:
> On Thu, 17 Jun 2010 03:25:03 am Alan Gauld wrote:
>> "R. Alan Monroe"  wrote
>>
>> >> directory, I suppose. Under windows, probably the registry.
>> >> There's the _winreg and configparser modules.
>> >
>> > Consider using the %USERPROFILE% environment variable rather than
>> > the
>> > registry.
>>
>> How would that work? That is just a single variable that points
>> to the users Settings folder. Effectively their home directory in
>> unix terms, so you could store the config file there. But
>> you couldn't store the kind of data you would store in the
>> registry?
>>
>> I'm confused.
>
> [snip explanation of environment variables]
>
> So unless I've missed something exceedingly subtle, writing to
> environment variables is not a solution to the problem of saving
> default values. It's not even part of a solution.
>

Just speculation here, but.. I suppose what he's suggesting is that
you use ConfigParser on both linux and windows, rather than the
registry on one platform and a config file on the other. on windows
you'd then place the config file in the %USERPROFILE% directory.

It's actually a rather good idea, not having to bother with the
registry saves you a lot of work.

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


Re: [Tutor] help

2010-06-17 Thread Steven D'Aprano
On Thu, 17 Jun 2010 05:28:37 pm KB SU wrote:
> help

No no, don't tell us what you need help about! I love guessing games!

Let me see now... I'm guessing that your problem is that don't know how 
to work out the length of a string. Here's one way:

string = "something"
count = 1
for char in string:
count *= 10

import math
print "The string has", math.log10(count), "characters."

 
There's probably a shorter way too.



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


Re: [Tutor] help

2010-06-17 Thread Mark Lawrence

On 17/06/2010 08:28, KB SU wrote:

help




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


Help, I need somebody,
Help, not just anybody,
Help, you know I need someone, help.

I'll leave you to find out the rests of the lyrics. :)

Mark Lawrence.

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


Re: [Tutor] How to model objects aimed to persistence?

2010-06-17 Thread Alan Gauld


"Knacktus"  wrote

###
my_favourite_movies = FavouriteMovies([Movie("Gladiator", "Russel 
Crowe")])

###

When I want to rebuild the data from a xml file or database, my 
objects somehow need to know which references to other objects they 
hold.


OK, If you limit yourself to XML or a database then yes you will
need an ID and use the IDs as references. Thats why Pickle/Shelve
are so convenient for persistence - they don;t need the references,
you can just pickle the list of objects, or whatever.

HTH,

--
Alan Gauld
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] Importing files not located on the home directory orstandardard library

2010-06-17 Thread Alan Gauld


"Independent Learner"  wrote

Lets say I want to load a file called quiz located in 
C:\Users\Julius

Hernandez\Desktop\Python2.6.5\Online Course\Chp.4

How would I configure PYTHONPATH or .pth or what ever
to import this file from IDLE


The IDLE bit is irrelevant its how the underlying Python
interpreter does things that matters.

You would need to set your PYTHONPATH environment variable
to include the directory containing your modules.

Do you know how to add values to a Windows Environment variable?
If not go to the Getting Started topic of my tutor for an example of
setting the PATH variable (You do the same but for PYTHONPATH,
the only difference is you might need to create it if it doesn't
already exist!)

Otherwise, Where are you having problems?

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

2010-06-17 Thread Lowell Tackett


>From the virtual desk of Lowell Tackett  


--- On Thu, 6/17/10, Mark Lawrence  wrote:

From: Mark Lawrence 
Subject: Re: [Tutor] help
To: tutor@python.org
Date: Thursday, June 17, 2010, 8:30 AM

On 17/06/2010 08:28, KB SU wrote:
> help
>
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

Help, I need somebody,
Help, not just anybody,
Help, you know I need someone, help.

I'll leave you to find out the rests of the lyrics. :)

Mark Lawrence.

You're "dating" yourself!  (I was in 'Nam when that song came out.)

___
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] pickling/shelve classes

2010-06-17 Thread Eike Welk
On Thursday June 17 2010 11:11:11 Eike Welk wrote:
> You should file a bug report in their bug tracker:
> http://github.com/ipython/ipython/issues

I believe tthis is the bug:
http://github.com/ipython/ipython/issues/#issue/29

It seems that objects defined on IPython's top-level can't be pickled. 


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


Re: [Tutor] help

2010-06-17 Thread Mark Lawrence

On 17/06/2010 19:22, Lowell Tackett wrote:



From the virtual desk of Lowell Tackett


--- On Thu, 6/17/10, Mark Lawrence  wrote:

From: Mark Lawrence
Subject: Re: [Tutor] help
To: tutor@python.org
Date: Thursday, June 17, 2010, 8:30 AM

On 17/06/2010 08:28, KB SU wrote:

help



Help, I need somebody,
Help, not just anybody,
Help, you know I need someone, help.

I'll leave you to find out the rests of the lyrics. :)

Mark Lawrence.

You're "dating" yourself!  (I was in 'Nam when that song came out.)

Jeesh, unlucky you :(  (I was in shorts at our village primary school)

Another lyric, "I hope I die before I get old", similar sort of era, eh? :)

Regards.

Mark Lawrence.


___
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


[Tutor] collectd application

2010-06-17 Thread Kaushal Shriyan
Hi,

For example I do wget -O file.png
"http://collectd.example.com/collectd/cgi-bin/collection.cgi?action=show_plugin;host=testdb;timespan=day;plugin=mysql";
 for a single host "testdb" and plugin "mysql"

I do /usr/bin/mime-construct --header 'Sender: mor_...@test.com'
--header 'From: mor_...@test.com' --to y...@mail.com --subject 'Email
test' --file-attach /var/tmp/file.png

That would be a manual way of doing it. I do have numerous servers and
multiple plugins. Basically i would like to do it without manual
intervention.

Please suggest further to send multiple png files from multiple
servers with multiple collectd plugins.

is that possible using python ?

Thanks,

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


[Tutor] string encoding

2010-06-17 Thread Rick Pasotto
I'm using BeautifulSoup to process a webpage. One of the fields has a
unicode character in it. (It's the 'registered trademark' symbol.) When
I try to write this string to another file I get this error:

UnicodeEncodeError: 'ascii' codec can't encode characters in position 31-32: 
ordinal not in range(128)

In the interpreter the  offending string portion shows as: 'Realtors\xc2\xae'.

How can I deal with this single string? The rest of the document works
fine.

-- 
"Freedom can't be kept for nothing. If you set a high value on liberty,
you must set a low value on everything else." -- Lucius Annaeus Seneca, 65
A.D.
Rick Pasottor...@niof.nethttp://www.niof.net
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] string encoding

2010-06-17 Thread Lie Ryan
On 06/18/10 06:41, Rick Pasotto wrote:
> I'm using BeautifulSoup to process a webpage. One of the fields has a
> unicode character in it. (It's the 'registered trademark' symbol.) When
> I try to write this string to another file I get this error:
> 
> UnicodeEncodeError: 'ascii' codec can't encode characters in position 31-32: 
> ordinal not in range(128)
> 
> In the interpreter the  offending string portion shows as: 'Realtors\xc2\xae'.
> 
> How can I deal with this single string? The rest of the document works
> fine.

You need to tell BeautifulSoup the encoding of the HTML document. You
can encode this information in either the:

- (preferred) Encoding is specified externally from HTTP Header
ContentType declaration, e.g.:
Content-Type: text/html; charset=utf-8

- HTML ContentType declaration: e.g.


- XML declaration -- for XHTML document used for parsing using XML
parser (hint: BeautifulSoup isn't XML/XHTML parser), e.g.:


However, BeautifulSoup will also uses some heuristics to *guess* the
encoding of a tag soup that doesn't have a proper encoding.

So, the most likely reason is this, from Beautiful Soup's FAQ:
http://www.crummy.com/software/BeautifulSoup/documentation.html#Why
can't Beautiful Soup print out the non-ASCII characters I gave it?
"""
Why can't Beautiful Soup print out the non-ASCII characters I gave it?

If you're getting errors that say: "'ascii' codec can't encode character
'x' in position y: ordinal not in range(128)", the problem is probably
with your Python installation rather than with Beautiful Soup. Try
printing out the non-ASCII characters without running them through
Beautiful Soup and you should have the same problem. For instance, try
running code like this:

latin1word = 'Sacr\xe9 bleu!'
unicodeword = unicode(latin1word, 'latin-1')
print unicodeword

If this works but Beautiful Soup doesn't, there's probably a bug in
Beautiful Soup. However, if this doesn't work, the problem's with your
Python setup. Python is playing it safe and not sending non-ASCII
characters to your terminal. There are two ways to override this behavior.

1. The easy way is to remap standard output to a converter that's not
afraid to send ISO-Latin-1 or UTF-8 characters to the terminal.

import codecs
import sys
streamWriter = codecs.lookup('utf-8')[-1]
sys.stdout = streamWriter(sys.stdout)

codecs.lookup returns a number of bound methods and other objects
related to a codec. The last one is a StreamWriter object capable of
wrapping an output stream.

2. The hard way is to create a sitecustomize.py file in your Python
installation which sets the default encoding to ISO-Latin-1 or to UTF-8.
Then all your Python programs will use that encoding for standard
output, without you having to do something for each program. In my
installation, I have a /usr/lib/python/sitecustomize.py which looks like
this:

import sys
sys.setdefaultencoding("utf-8")

For more information about Python's Unicode support, look at Unicode for
Programmers or End to End Unicode Web Applications in Python. Recipes
1.20 and 1.21 in the Python cookbook are also very helpful.

Remember, even if your terminal display is restricted to ASCII, you can
still use Beautiful Soup to parse, process, and write documents in UTF-8
and other encodings. You just can't print certain strings with print.
"""

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


Re: [Tutor] string encoding

2010-06-17 Thread Rick Pasotto
On Fri, Jun 18, 2010 at 12:24:25PM +1000, Lie Ryan wrote:
> On 06/18/10 06:41, Rick Pasotto wrote:
> > I'm using BeautifulSoup to process a webpage. One of the fields has a
> > unicode character in it. (It's the 'registered trademark' symbol.) When
> > I try to write this string to another file I get this error:
> > 
> > UnicodeEncodeError: 'ascii' codec can't encode characters in position 
> > 31-32: ordinal not in range(128)
> > 
> > In the interpreter the  offending string portion shows as: 
> > 'Realtors\xc2\xae'.
> > 
> > How can I deal with this single string? The rest of the document works
> > fine.
> 
> You need to tell BeautifulSoup the encoding of the HTML document. You
> can encode this information in either the:
> 
> - (preferred) Encoding is specified externally from HTTP Header
> ContentType declaration, e.g.:
> Content-Type: text/html; charset=utf-8
> 
> - HTML ContentType declaration: e.g.
> 

The document has:



When I look at the document in vim and when I 'print' in python I see
the two characters of an acented capital A and the circled 'r'.

> latin1word = 'Sacr\xe9 bleu!'
> unicodeword = unicode(latin1word, 'latin-1')
> print unicodeword

TypeError: decoding Unicode is not supported

> If this works but Beautiful Soup doesn't, there's probably a bug in
> Beautiful Soup. However, if this doesn't work, the problem's with your
> Python setup. Python is playing it safe and not sending non-ASCII
> characters to your terminal. There are two ways to override this behavior.
> 
> 1. The easy way is to remap standard output to a converter that's not
> afraid to send ISO-Latin-1 or UTF-8 characters to the terminal.
> 
> import codecs
> import sys
> streamWriter = codecs.lookup('utf-8')[-1]
> sys.stdout = streamWriter(sys.stdout)
> 
> codecs.lookup returns a number of bound methods and other objects
> related to a codec. The last one is a StreamWriter object capable of
> wrapping an output stream.

Those four lines executed but I still get

TypeError: decoding Unicode is not supported

> Remember, even if your terminal display is restricted to ASCII, you can
> still use Beautiful Soup to parse, process, and write documents in UTF-8
> and other encodings. You just can't print certain strings with print.

I can print the string fine. It's f.write(string_with_unicode) that fails with:

UnicodeEncodeError: 'ascii' codec can't encode characters in position 31-32: 
ordinal not in range(128)

Shouldn't I be able to f.write() *any* 8bit byte(s)?

repr() gives: u"Realtors\\xc2\\xae"

BTW, I'm running python 2.5.5 on debian linux.

-- 
"Making fun of born-again christians is like hunting dairy cows with a
 high powered rifle and scope." -- P.J. O'Rourke
Rick Pasottor...@niof.nethttp://www.niof.net
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] collectd application

2010-06-17 Thread Steven D'Aprano
On Fri, 18 Jun 2010 04:53:49 am Kaushal Shriyan wrote:
[...]
> is that possible using python ?

Yes. Python is a Turing complete language, it can do anything that any 
other language can do.

This site will probably help you:

http://www.catb.org/%7Eesr/faqs/smart-questions.html

Good luck, and we look forward to you coming back with a better 
question!



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


[Tutor] sqlite3 select extra characters

2010-06-17 Thread Lang Hurst

Is there a way to just return the values when using sqlite3?

If I:

   names = c.execute('select names from students')
   for name in names: print names

I get the list I want, but they look like

(u'Cleese, John')
(u'Jones, Terry')
(u'Gillaim, Terry')

is there a way to just return

Cleese, John
Jones, Terry
Gillaim, Terry

?

It seems a shame to have to run the answers through a stip process.

--
There are no stupid questions, just stupid people.

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