Re: guess file type in python

2007-11-19 Thread Matt Nordhoff
Japan Shah wrote:
>  
> 
>  
> 
>
> 

mimetypes module?
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sorting a list of list

2007-11-21 Thread Matt Nordhoff
Tim Chase wrote:
>> are there available library or pythonic algorithm for sorting a list
>> of list depending on the index of the list inside the list of my
>> choice?
> 
> The built-in sorted() function and the sort() method on various
> collections take an optional "key=function" keyword paramater
> with which you can pass a function (lambdas are convenient) to
> extract the bit on which you want to compare:
> 
 d_list = [
> ... ['a', 1, 9],
> ... ['b', 2, 8],
> ... ['c', 3, 7],
> ... ['d', 4, 6],
> ... ['e', 5, 5],
> ... ]
 print sorted.__doc__
> sorted(iterable, cmp=None, key=None, reverse=False) --> new
> sorted list
 sorted(d_list, key=lambda x: x[2]) # sort by the 3rd item
> [['e', 5, 5], ['d', 4, 6], ['c', 3, 7], ['b', 2, 8], ['a', 1, 9]]
 sorted(d_list, key=lambda x: x[1]) # sort by the 2nd item
> [['a', 1, 9], ['b', 2, 8], ['c', 3, 7], ['d', 4, 6], ['e', 5, 5]]
 sorted(d_list, key=lambda x: x[0]) # sort by the 1st item
> [['a', 1, 9], ['b', 2, 8], ['c', 3, 7], ['d', 4, 6], ['e', 5, 5]]

Not to be too complicated, but there are functions that return a
callable that is faster than a lambda.

>>> import operator

Then, instead of "lambda x: x[2]" use "operator.itemgetter(2)" and
instead of "lambda x: x.foo" use "operator.attrgetter('foo')".
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: new is missing in module hmac SHA-based encryption function in Python

2007-11-21 Thread Matt Nordhoff
Hailong Wang wrote:
> Matt,
> 
> Thanks for response, I changed my script to hw_hmac.py, but still does
> not work.
> 
> Traceback (most recent call last):
>   File "C:/Python25/hw_script/my_hmac.py", line 1, in 
> import hmac
>   File "C:\Python25\hw_script\hmac.py", line 4, in 
> myhmac = hmac.new("test")
> AttributeError: 'module' object has no attribute 'new'


(Note to anyone else: He responded off-list saying he got it to work. :-) )
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: new is missing in module hmac SHA-based encryption function in Python

2007-11-21 Thread Matt Nordhoff
Hailong Wang wrote:
> I have small hmac encryption programm by python, but always complain
> that hmac module does not have attribute new, do I need to install
> anything additinal in my laptop? I am using python 2.5.1
> 
> import hmac
> import md5
> import sha
> myhmac = hmac.new("test")
> 
> 
> Traceback (most recent call last):
>   File "C:\Python25\hw_script\hmac.py", line 1, in 
> import hmac
>   File "C:\Python25\hw_script\hmac.py", line 4, in 
> myhmac = hmac.new("test")
> AttributeError: 'module' object has no attribute 'new'

Your script is called "hmac.py", so it's importing itself. You need to
name it something else.
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: "Python" is not a good name, should rename to "Athon"

2007-11-30 Thread Matt Nordhoff
Tim Chase wrote:
> (You'd think this was the Lisp ML, not Python... )

Atsp? :-)
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: "Python" is not a good name, should rename to "Athon"

2007-11-30 Thread Matt Nordhoff
[EMAIL PROTECTED] wrote:
> Python is a good programming language,  but "Python" is not a good
> name.
> 
> First, python also means snake, Monty Python. If we search "python" in
> google, emule, many results are not programming resource. If we search
> PHP, all results are programming resource.
> 
> Second, python also means snake, snake is not a good thing in western
> culture. Many people dislike any things relevant to snake. We must
> have high regard for the custom.
> 
> Now, python3000 is coming. It's the best time to rename!
> 
> Athon is a good candidate, you could provide better names.
> 
> In Athon, the first letter "A" could pronounce as [ e ] .

Great, now the Google results will just be filled with AMD typoes. :-P

One downside of Athon is that "Python" makes for a good two-letter
abbreviation: py. It sounds nice (hey, pie), and since it ends in a
vowel, is good for project names: PyGTK or pytz or whatever. AtGTK and
attz don't have quite the ring.
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Timezones in python

2007-12-05 Thread Matt Nordhoff
[EMAIL PROTECTED] wrote:
> Is there any build in solution in python to handle timezones? My
> problem is I have to convert +4 time to +0. In the worst case i can
> just add +4 to the houer but I'm not very happy about that. Another
> problem is the summer/winter timechange which happen with one week
> difference. I am looking for something what can convert different
> timezone to localtime.

There's the pytz library [1] [2], which uses the tz database popular on
Unix systems, and python-dateutil [3] [4] also includes support for it,
among doing other date- and time-related things.

Those support time zone names (America/New_York or EST5EDT), and also
converting between them. If you only want to convert between +4 and +0,
without giving names for them, you don't need them, but I don't know how
to do it. Using UTC internally and only converting to other zones when
reading data in and displaying it makes things simpler . .

[1] 
[2] 
[3] 
[4] 
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: File to dict

2007-12-07 Thread Matt Nordhoff
Chris wrote:
> Ta Matt, wasn't paying attention to what I typed. :)
> And didn't know that about .get() and not having to declare the
> global.
> Thanks for my mandatory new thing for the day ;)

:-)
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: File to dict

2007-12-07 Thread Matt Nordhoff
Duncan Booth wrote:
> Just some minor points without changing the basis of what you have done 
> here:
> 
> Don't bother with 'readlines', file objects are directly iterable.
> Why are you calling both lstrip and rstrip? The strip method strips 
> whitespace from both ends for you.
> 
> It is usually a good idea with code like this to limit the split method to 
> a single split in case there is more than one colon on the line: i.e. 
> x.split(':',1)
> 
> When you have a sequence whose elements are sequences with two elements 
> (which is what you have here), you can construct a dict directly from the 
> sequence.
> 
> But why do you construct a dict from that input data simply to throw it 
> away? If you only want 1 domain from the file just pick it out of the list. 
> If you want to do multiple lookups build the dict once and keep it around.
> 
> So something like the following (untested code):
> 
> from __future__ import with_statement
> 
> def loaddomainowners(domain):
> with open('/etc/virtual/domainowners','r') as infile:
> pairs = [ line.split(':',1) for line in infile if ':' in line ]
> pairs = [ (domain.strip(), owner.strip())
> for (domain,owner) in pairs ]
> return dict(lines)
> 
> DOMAINOWNERS = loaddomainowners()
> 
> def lookupdmo(domain):
> return DOMAINOWNERS[domain]

Using two list comprehensions mean you construct two lists, which sucks
if it's a large file.

Also, you could pass the list comprehension (or better yet a generator
expression) directly to dict() without saving it to a variable:

with open('/etc/virtual/domainowners','r') as fh:
return dict(line.strip().split(':', 1) for line in fh)

(Argh, that doesn't .strip() the key and value, which means it won't
work, but it's so simple and elegant and I'm tired enough that I'm not
going to add that. :-P Just use another genexp. Makes for a line
complicated enough that it could be turned into a for loop, though.)
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: File to dict

2007-12-07 Thread Matt Nordhoff
Chris wrote:
> For the first one you are parsing the entire file everytime you want
> to lookup just one domain.  If it is something reused several times
> during your code execute you could think of rather storing it so it's
> just a simple lookup away, for eg.
> 
> _domain_dict = dict()
> def generate_dict(input_file):
> finput = open(input_file, 'rb')
> global _domain_dict
> for each_line in enumerate(finput):
> line = each_line.strip().split(':')
> if len(line)==2: _domain_dict[line[0]] = line[1]
> 
> finput.close()
> 
> def domain_lookup(domain_name):
> global _domain_dict
> try:
> return _domain_dict[domain_name]
> except KeyError:

What about this?

_domain_dict = dict()
def generate_dict(input_file):
global _domain_dict
# If it's already been run, do nothing. You might want to change
# this.
if _domain_dict:
return
fh = open(input_file, 'rb')
try:
for line in fh:
line = line.strip().split(':', 1)
if len(line) == 2:
_domain_dict[line[0]] = line[1]
finally:
fh.close()

def domain_lookup(domain_name):
return _domain_dict.get(domain_name)

I changed generate_dict to do nothing if it's already been run. (You
might want it to run again with a fresh dict, or throw an error or
something.)

I removed enumerate() because it's unnecessary (and wrong -- you were
trying to split a tuple of (index, line)).

I also changed the split to only split once, like Duncan Booth suggested.

The try-finally is to ensure that the file is closed if an exception is
thrown for some reason.

domain_lookup doesn't need to declare _domain_dict as global because
it's not assigning to it. .get() returns None if the key doesn't exist,
so now the function returns None. You might want to use a different
value or throw an exception (use _domain_dict[domain_name] and not catch
the KeyError if it doesn't exist, perhaps).

Other than that, I just reformatted it and renamed variables, because I
do that. :-P
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: finding dir of main .py file

2007-12-11 Thread Matt Nordhoff
ron.longo wrote:
> Nope, maybe I'm not explaining myself well.
> 
> When I do os.getenv('HOME') I get back None.
> 
> According to the docs, 'HOME' is the user's home directory on some
> platforms.  Which is not what I want.
> 
> What I want is the directory in which an application's main .py file
> resides.  That is, when I type: python MyApp.py, I want to know in which
> directory does MyApp.py reside?

Shane is right.

>>> print __file__

>>> print modulename.__file__

Just call os.path.dirname() on __file__ to get the directory.
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: E-Mail Parsing

2007-12-12 Thread Matt Nordhoff
Merrigan wrote:
> I am writing a script to administer my E-Mail Server. The One thing
> I'm currently struggling with is kind of Parsing the E-Mail adress
> that I supply to the script.
> 
> I need to get the username (The part BEFORE the @ sign) out of the
> address so that I can use it elsewhere. The problem I have with this
> is that both the domain, and the username are variable lenghts and
> that I have no idea how to split the two parts.
> 
> Is there any way that I can do this?
> 
> Thank ye very much.

>>> addr = "[EMAIL PROTECTED]"
>>> addr.split("@")
['user', 'example.com']

If it's formatted like a To header ("User <[EMAIL PROTECTED]>"), use
email.Utils.parseaddr() to get the address out of it.\

The email modules might help you a lot:


-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: determining bytes read from a file.

2007-12-13 Thread Matt Nordhoff
vineeth wrote:
> parser.add_option("-b", "--bytes", dest="bytes")

This is an aside, but if you pass 'type="int"' to add_option, optparse
will automatically convert it to an int, and (I think), give a more
useful error message on failure.
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: E-Mail Parsing

2007-12-13 Thread Matt Nordhoff
Merrigan wrote:
> Hi Matt,
> 
> Thank you very much for the help. It was exactly what I was looking
> for, and made my script much safer and easier to use.
> 
> Blessings!
> 
>  -- Merrigan

You're welcome. :-)
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: urlparse.urlparse bug - misparses long URL

2007-12-13 Thread Matt Nordhoff
John Nagle wrote:
> Here's a hostile URL that "urlparse.urlparse" seems to have mis-parsed.
> 
> http://[EMAIL 
> PROTECTED]&xUDysvTbzZZOaymjQ2oYIx2AvMdJ1WQfjP02wIBBQBb1EVZAqmmGunxrcyGx1AcfegWUUYtaZfRW434O5Qn6InSMUZXgF5e3KzJbCntBGOj7pv31zab&action=login-run&passkey=e84239c9da59dbeb61d4d45db2cc5840&info_hash=%c9q%be%fe%c6j%ca%fd0%18%fe%23J%bd%89%d3%06L%fdV&info_hash=%18%9d%fb%15v%c0A%1f%c8%dds%0f%17%99%ceQ%83%a0%3e%27&info_hash=%df%f0%1c%5e%d75%b2%7d%e6D%0d%3e%d8%fbZ%5c%de%2ae%93&https://www.midamericabank.com/my_acccounts/default.aspxL0PWSjXev6xlkMTqVKFbLUgrh8CBquCchip4PuQDWYLYpzDGOFkLZyY
> 
> What we get back in the "accesshost" field (i.e. the domain name) is
> 
> 
> '[EMAIL 
> PROTECTED]&xUDysvTbzZZOaymjQ2oYIx2AvMdJ1WQfjP02wIBBQBb1EVZAqmmGunxrcyGx1AcfegWUUYtaZfRW434O5Qn6InSMUZXgF5e3KzJbCntBGOj7pv31zab&action=login-run&passkey=e84239c9da59dbeb61d4d45db2cc5840&info_hash=%c9q%be%fe%c6j%ca%fd0%18%fe%23J%bd%89%d3%06L%fdV&info_hash=%18%9d%fb%15v%c0A%1f%c8%dds%0f%17%99%ceQ%83%a0%3e%27&info_hash=%df%f0%1c%5e%d75%b2%7d%e6D%0d%3e%d8%fbZ%5c%de%2ae%93&https:'
> 
> 
> which is wrong.  Something far out in that URL is breaking urlparse, and it's 
> not able to extract the domain name properly.
> 
> It's not a UNICODE issue; forced the data to "str" and it still mis-parses.
> 
> I'm trying to construct s shorter string that fails.  More to follow.
> 
> (Yes, another error associated with the wonderful world of parsing hostile 
> sites 
> in Python.  This is from a phishing attack, and that URL is in PhishTank.)
> 
>   John Nagle
>   SiteTruth

It's breaking on the first slash, which just happens to be very late in
the URL.

>>> urlparse('http://example.com?blahblah=http://example.net')
('http', 'example.com?blahblah=http:', '//example.net', '', '', '')
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: High speed web services

2007-12-14 Thread Matt Nordhoff
herbasher wrote:
> I'm wondering: I'm really not so much into heavy frameworks like
> Django, because I need to build a fast, simple python based
> webservice.
> 
> That is, a request comes in at a certain URL, and I want to utilize
> Python to respond to that request.
> 
> Ideally, I want the script to be "cached" so it doesn't have to be re-
> parsed everytime.
> 
> 
> Phrasing this into a question:
> How do I built highly available and lighting fast Python webservice?
> 
> I'm open to using any kind of server software, and all that under a
> *nix based system.

Well, you could simply use FastCGI/SCGI/mod_python/mod_wsgi without any
framework on top of it. It's not much harder than writing a CGI script.

If you're doing something with multiple actual web pages or something,
just use a framework. It'll be fast enough.
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: opposite of zip()?

2007-12-17 Thread Matt Nordhoff
Rich Harkins wrote:
> [EMAIL PROTECTED] wrote:
>> Given a bunch of arrays, if I want to create tuples, there is
>> zip(arrays). What if I want to do the opposite: break a tuple up and
>> append the values to given arrays:
>>map(append, arrays, tupl)
>> except there is no unbound append() (List.append() does not exist,
>> right?).
>>
> 
> list.append does exist (try the lower-case flavor).
> 
>> Without append(), I am forced to write a (slow) explicit loop:
>>   for (a, v) in zip(arrays, tupl):
>>   a.append(v)
>>
> 
> Except that isn't technically the opposite of zip.  The opposite would
> be a tuple of single-dimensional tuples:
> 
> def unzip(zipped):
> """
> Given a sequence of size-sized sequences, produce a tuple of tuples
> that represent each index within the zipped object.
> 
> Example:
> >>> zipped = zip((1, 2, 3), (4, 5, 6))
> >>> zipped
> [(1, 4), (2, 5), (3, 6)]
> >>> unzip(zipped)
> ((1, 2, 3), (4, 5, 6))
> """
> if len(zipped) < 1:
> raise ValueError, 'At least one item is required for unzip.'
> indices = range(len(zipped[0]))
> return tuple(tuple(pair[index] for pair in zipped)
>  for index in indices)
> 
> This is probably not the most efficient hunk of code for this but this
> would seem to be the correct behavior for the opposite of zip and it
> should scale well.
> 
> Modifying the above with list.extend would produce a variant closer to
> what I think you're asking for:
> 
> def unzip_extend(dests, zipped):
> """
> Appends the unzip versions of zipped into dests.  This avoids an
> unnecessary allocation.
> 
> Example:
> >>> zipped = zip((1, 2, 3), (4, 5, 6))
> >>> zipped
> [(1, 4), (2, 5), (3, 6)]
> >>> dests = [[], []]
> >>> unzip_extend(dests, zipped)
> >>> dests
> [[1, 2, 3], [4, 5, 6]]
> """
> if len(zipped) < 1:
> raise ValueError, 'At least one item is required for unzip.'
> for index in range(len(zipped[0])):
> dests[index].extend(pair[index] for pair in zipped)
> 
> This should perform pretty well, as extend with a comprehension is
> pretty fast.  Not that it's truly meaningful, here's timeit on my 2GHz
> laptop:
> 
> bash-3.1$ python -m timeit -s 'import unzip; zipped=zip(range(1024),
> range(1024))' 'unzip.unzip_extend([[], []], zipped)'
> 1000 loops, best of 3: 510 usec per loop
> 
> By comparison, here's the unzip() version above:
> 
> bash-3.1$ python -m timeit -s 'import unzip; zipped=zip(range(1024),
> range(1024))' 'unzip.unzip(zipped)'
> 1000 loops, best of 3: 504 usec per loop
> 
> Rich

As Paddy wrote, zip is its own unzip:

>>> zipped = zip((1, 2, 3), (4, 5, 6))
>>> zipped
[(1, 4), (2, 5), (3, 6)]
>>> unzipped = zip(*zipped)
>>> unzipped
[(1, 2, 3), (4, 5, 6)]

Neat and completely confusing, huh? :-)


-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question about email-handling modules

2007-12-20 Thread Matt Nordhoff
Robert Latest wrote:
> Hello,
> 
> I'm new to Python but have lots of programming experience in C, C++ and 
> Perl. Browsing through the docs, the email handling modules caught my eye 
> because I'd always wanted to write a script to handle my huge, ancient, and 
> partially corrupted email archives.
> 
> Of course I know that this kind of project shouldn't be tackled by a 
> beginner in a language, but I still thought I'd give it a spin. 
> 
> So I wrote the stuff at the bottom. It lists senders, subjects and 
> addressees of all messages in an mbox.
> 
> Things that I don't understand:
> 
> 1. Why can I get the 'subject' and 'from' header field unsig the [] 
> notation, but not 'to'? When I print Message.keys I get a list of all header 
> fields of the message, including 'to'. What's the difference between 
> message['to'] and message.get('to')?

On dicts, and presumably on Messages too, .get returns a default value
(None, or you can specify another with .get("key", "default") if the key
doesn't exist.

I can't say why ['to'] doesn't work when it's in the list of keys, though.

> 2. Why can't I call the get_payload() method on the message? What I get is 
> this cryptic error: "AttributeError: Message instance has no attribute 
> 'get_payload'". I'm trying to call a method here, not an attribute. It makes 
> no difference if I put parentheses after get_payload or not. I looked into 
> the email/Message module and found get_payload defined there. 

Methods are attributes. When you do "obj.method()", "obj.method" and
"()" are really two separate things: It gets the "method" attribute of
"obj", and then calls it.

> I don't want to waste your time by requesting that you pick apart my silly 
> example. But maybe you can give me a pointer in the right direction. This is 
> python 2.4 on a Debian box.
> 
> ---
> 
> #!/usr/bin/python
> import mailbox
> import email  # doesn't make a difference
> from email import Message # neither does this
> 
> mbox = file("mail.old/friends")
> 
> for message in mailbox.UnixMailbox(mbox):
> subject = message['subject']
> frm = message['from']
> #to = message['to'] # this throws a "Key Error"
> to = message.get('to'); # ...but this works
> print frm, "writes about", subject, "to", to
> #print message.get_payload() # this doesn't work
> 
> --
> 
> robert

(Oops, I wrote this like half an hour ago, but I never sent it.)
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Local variables in classes and class instantiation

2007-12-23 Thread Matt Nordhoff
A.J. Bonnema wrote:
> Hi all,
> 
> I just started using Python. I used to do some Java programming, so I am 
> not completely blank.
> 
> I have a small question about how classes get instantiated within other 
> classes. I have added the source of a test program to the bottom of this 
> mail, that contains 3 methods within a testclass that each instantiate 
> the same class and bind it to a local variable. My understanding was, 
> that the local variable gets garbage collected as soon as the method 
> goes out of scope. Thus I would expect the local variable 'cpu' in these 
> methods to be independant and create different instances of the class CPU.
> 
> Still, when I execute all three methods, I get two instances that are 
> equal and the third is different.
> Is there some circomstance that makes two object creations result in the 
> same object?
> 
> = output
> Output from the (test)program is:
> cpu class = 
> .cpu class = 
> .cpu class = 
> .
> --
> Ran 3 tests in 0.001s
> 
> OK
> === source
> The source of the test program is:
> 
> 

What if the second object is separate, but just gets allocated at the
same location as the first one?

Try printing some unique attribute of each object, like the one that
stores the "cpuXX" argument that gets passed in.
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python for web...

2007-12-25 Thread Matt Nordhoff
[EMAIL PROTECTED] wrote:
> Hi everyone,
> 
> I have to develop a web based enterprise application for my final year
> project. Since i am interested in open source, i searched the net.
> Almost 90% of them were PHP and MySQL. Cant we use python for that ? I
> tried several sites, but there is not enough tutorial for beginners
> [mod_python, PSP etc]. I couldnt find any detailed book, not even a
> single book :( All the python books are covering only CGI part)
> 
> Any suggestions? Any recommended book?
> 
> Execuse my English.
> 
> Thushanthan.

Well, there are multiple different Python web frameworks, including
Django, Pylons, TurboGears and Zope (also the barebones CherryPy).
They're all much more high-level than PHP, including things like
database APIs and templating languages. Most/all of them have books or
at least good documentation and guides.

If you're doing something really simple (one web page, minimal DB usage,
whatever), you can use bare WSGI, perhaps like libraries like Beaker for
sessions.

Google all of the terms I used. There should be information available. :-P
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: save gb-2312 web page in a .html file

2007-12-26 Thread Matt Nordhoff
Peter Pei wrote:
> I am trying to read a web page and save it in a .html file. The problem is 
> that the web page is GB-2312 encoded, and I want to save it to the file with 
> the same encoding or unicode. I have some code like this:
> url = 'http://blah/'
> headers = { 'User-Agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows 
> NT)' }
> 
> req = urllib2.Request(url, None, headers)
> page = urllib2.urlopen(req).read()
> 
> file = open('btchina.html','wb')
> file.write(page.encode('gb-2312'))
> file.close()
> 
> It is obviously not working, and I am hoping someone can help me.

.read() returns the bytes exactly how it downloads them. It doesn't
interpret them. If those bytes are GB-2312-encoded text, that's what
they are. There's no need to reencode them. Just .write(page) (of
course, this way you don't verify that it's correct).

(BTW, don't use 'file' as a variable name. It's an alias of the 'open()'
function.)
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: save gb-2312 web page in a .html file

2007-12-26 Thread Matt Nordhoff
Peter Pei wrote:
> You must be right, since I tried one page and it worked. But there is 
> something wrong with this particular page: 
> http://overseas.btchina.net/?categoryid=-1. When I open the saved file (with 
> IE7), it is all messed up.
> 
> url = 'http://overseas.btchina.net/?categoryid=-1'
> headers = { 'User-Agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows 
> NT)' }
> req = urllib2.Request(url, None, headers)
> page = urllib2.urlopen(req).read()
> 
> htmlfile = open('btchina.html','w')
> htmlfile.write(page)
> htmlfile.close() 

I dunno. The file does specify its charset, so unless IE ignores that
and tries to guess and fails, it should work fine.
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Big-endian binary data to/from Python ints?

2007-12-26 Thread Matt Nordhoff
William McBrine wrote:
> Here are a couple of functions that I feel stupid for having written. 
> They work, and they're pretty straightforward; it's just that I feel like 
> I must be missing an easier way to do this...
> 
> def net_to_int(numstring):
> """Convert a big-endian binary number, in the form of a string of
> arbitrary length, to a native int.
> """
> num = 0
> for i in numstring:
> num *= 256
> num += ord(i)
> return num
> 
> def int_to_net(num):
> """Convert a native int to a four-byte big-endian number, in the form
> of a string.
> """
> numstring = ''
> for i in xrange(4):
> numstring = chr(num % 256) + numstring
> num /= 256
> return numstring
> 
> The situation: I'm getting a four-byte packet from a socket that consists 
> of a big-endian 32-bit integer. (It specifies the length of the data that 
> follows.) I have to send the same thing in reply. send() and recv() work 
> with strings... I'm familiar with ntohl() and htonl(), but those expect/ 
> return integers.

The struct module should do it, but do you prefer your code or format
strings? :-P


-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Optional code segment delimiter?

2007-12-29 Thread Matt Nordhoff
xkenneth wrote:
> Is it possible to use optional delimiters other than tab and colons?
> 
> For example:
> 
>  if this==1 {
>   print this
>  }



Heheheh..


-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sqlobject question...

2007-12-29 Thread Matt Nordhoff
bruce wrote:
> hi...
> 
> this continues my investigation of python/sqlobject, as it relates to the
> need to have an id, which is auto-generated.
> 
> per various sites/docs on sqlobject, it appears that you can override the
> id, by doing something similar to the following:
> 
> def foo(SQLObject):
> def _init(self, id, connection=None):
> id = str(id)
> SQLObject._init(self, id, connection)
> 
> cat=StringCol()
> dog=StringCol()
> 
> 
> however, no matter what i do, i can't seem to insert anything into "id" so
> that i can use the "id"
> 
> any thoughts/comments/pointers, code samples would be helpful!!!
> 
> thanks

SQLObject may do something weird, but normally the init method is named
"__init__", not "_init". Try changing that.
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Fate of itertools.dropwhile() and itertools.takewhile()

2007-12-30 Thread Matt Nordhoff
Raymond Hettinger wrote:
> I'm considering deprecating these two functions and would like some
> feedback from the community or from people who have a background in
> functional programming.
> 
> * I'm concerned that use cases for the two functions are uncommon and
> can obscure code rather than clarify it.
> 
> * I originally added them to itertools because they were found in
> other functional languages and because it seemed like they would serve
> basic building blocks in combination with other itertools allow
> construction of a variety of powerful, high-speed iterators.  The
> latter may have been a false hope -- to date, I've not seen good
> recipes that depend on either function.
> 
> * If an always true or always false predicate is given, it can be hard
> to break-out of the function once it is running.
> 
> * Both functions seem simple and basic until you try to explain them
> to someone else.  Likewise, when reading code containing dropwhile(),
> I don't think it is self-evident that dropwhile() may have a lengthy
> start-up time.
> 
> * Since itertools are meant to be combined together, the whole module
> becomes easier to use if there are fewer tools to choose from.
> 
> These thoughts reflect my own experience with the itertools module.
> It may be that your experience with them has been different.  Please
> let me know what you think.
> 
> Raymond

FWIW, Google Code Search shows a few users:



Do any of them make good use of them?
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: problem with global var

2008-01-03 Thread Matt Nordhoff
Bruno Ferreira wrote:
> Hi,
> 
> I wrote a very simple python program to generate a sorted list of
> lines from a squid access log file.
> 
> Here is a simplified version:
> 
> ##
> 1  logfile = open ("squid_access.log", "r")
> 2  topsquid = [["0", "0", "0", "0", "0", "0", "0"]]
> 3
> 4  def add_sorted (list):

Don't call your variable "list". There's already the built-in type "list".

> 5 for i in range(50):

You should probably use xrange here.

> 6  if int(list[4]) > int(topsquid[i][4]):
> 7  topsquid.insert(i,list)
> 8  break
> 8  # Max len = 50
> 10 if len(topsquid) > 50:
> 11 topsquid = topsquid[0:50]

I'd just use "[:50]", the 0 is implied.

> 13 while True:
> 14 logline = logfile.readline()
> 15 linefields = logline.split()
> 16
> 17 if logline != "":
> 18 add_sorted (linefields)
> 19 else:
> 20 break

for logline in logfile:
if logline:
linefields = logline.split()
add_sorted(linefields)
else:
break

> 22 for i in range (len(topsquid)):
> 23 print topsquid[i][4]

for i in topsquid:
print i[4]

(You probably want to use a name other than "i" then.)

> 
>
> When I execute the program _without_ the lines 10 and 11:
> 
> 10 if len(topsquid) > 50:
> 11 topsquid = topsquid[0:50]
> 
> it runs perfectly.
> 
> But if I execute the program _with_ those lines, this exception is thrown:
> 
> [EMAIL PROTECTED]:~$ python topsquid.py
> Traceback (most recent call last):
>   File "topsquid.py", line 20, in 
> add_sorted (linefields)
>   File "topsquid.py", line 6, in add_sorted
> if int(list[4]) > int(topsquid[i][4]):
> UnboundLocalError: local variable 'topsquid' referenced before assignment
> 
> 
> Note that now the error shown is not related with the lines 10 and 11,
> but wiht a line prior to them.
> 
> Any hints?

Basically, you're trying to read the global variable "topsquid", and
then you're trying to define a local variable "topsquid". Python doesn't
like that. Declare it as global by adding "global topsquid" to the top
of the function.
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Delete lines containing a specific word

2008-01-06 Thread Matt Nordhoff
Francesco Pietra wrote:
> Please, how to adapt the following script (to delete blank lines) to delete
> lines containing a specific word, or words?
> 
> f=open("output.pdb", "r")
> for line in f:
>   line=line.rstrip()
>   if line:
>   print line
> f.close()
> 
> If python in Linux accepts lines beginning with # as comment lines, please 
> also
> a script to comment lines containing a specific word, or words, and back, to
> remove #.
> 
> Thanks
> francesco pietra

If you're on Linux, why not just use grep?

$ grep -v theword output.pdb

Otherwise, just replace "if line:" with "if 'theword' not in line:".

Thanks to rstrip and print, the script changes the file's line endings
to the platform's default one. "rstrip()" also removes other types of
whitespace, like spaces and tabs.

I'm not sure how that script handles different line endings in the file.
Might want to use 'rb' instead of 'r' to open the file in binary mode on
Windows (to also help with other issues), or 'rU' or 'rbU' to use
Python's universal newline mode.)
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: problem of converting a list to dict

2008-01-09 Thread Matt Nordhoff
bsneddon wrote:
> This seemed to work for me if you are using 2.4 or greater and
> like list comprehension.
 dict([ tuple(a.split("=")) for a in mylist[1:-1]])
> {'mike': 'manager', 'paul': 'employee', 'tom': 'boss'}
> 
> should be faster than looping

That's what he's doing (well, a generator expression, not a listcomp).
It's just split across multiple lines (ew).
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Elementary string-formatting

2008-01-13 Thread Matt Nordhoff
Odysseus wrote:
> Hello, group: I've just begun some introductory tutorials in Python. 
> Taking off from the "word play" exercise at
> 
> 
> 
> I've written a mini-program to tabulate the number of characters in each 
> word in a file. Once the data have been collected in a list, the output 
> is produced by a while loop that steps through it by incrementing an 
> index "i", saying
> 
> print '%2u %6u %4.2f' % \
> (i, wordcounts[i], 100.0 * wordcounts[i] / wordcounts[0])

This isn't very important, but instead of keeping track of the index
yourself, you can use enumerate():

>>> mylist = ['a', 'b', 'c']
>>> for i, item in enumerate(mylist):
... print i, item
...
0 a
1 b
2 c
>>>

Err, it doesn't look like you can make it start at 1 though.


-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ucs2 or ucs4?

2008-01-14 Thread Matt Nordhoff
Neal Becker wrote:
> How do I tell if my python-2.5 is build with ucs2 or ucs4?

You can also check sys.maxunicode.

>>> import sys
>>> sys.maxunicode

If it's 1114111, you're UCS-4. If it's something much lower, you're UCS-2.
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is str/unicode.encode supposed to work? with replace/ignore

2008-01-15 Thread Matt Nordhoff
BerlinBrown wrote:
> With this code, ignore/replace still generate an error
> 
>   # Encode to simple ascii format.
>   field.full_content = field.full_content.encode('ascii', 
> 'replace')
> 
> Error:
> 
> [0/1] 'ascii' codec can't decode byte 0xe2 in position 14317: ordinal
> not in ran
> ge(128)
> 
> The document in question; is a wikipedia document.  I believe they use
> latin-1 unicode or something similar.  I thought replace and ignore
> were supposed to replace and ignore?

Is field.full_content a str or a unicode? You probably haven't decoded
it from a byte string yet.

>>> field.full_content = field.full_content.decode('utf8', 'replace')
>>> field.full_content = field.full_content.encode('ascii', 'replace')

Why do you want to use ASCII? UTF-8 is great. :-)
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: multi-Singleton-like using __new__

2008-02-08 Thread Matt Nordhoff
J Peyret wrote:
> - Same with using try/except KeyError instead of in cls.cache.
> Has_key might be better if you insist on look-before-you-leap, because
> 'in cls.cache' probably expends to uri in cls.cache.keys(), which can
> be rather bad for perfs if the cache is very big.  i.e. dict lookups
> are faster than scanning long lists.

Not true. 'in' is (marginally) faster than has_key. It's also been
preferred to has_key for a while now.

$ python -m timeit -s "d = dict.fromkeys(xrange(5))" "4 in d"
100 loops, best of 3: 0.233 usec per loop
$ python -m timeit -s "d = dict.fromkeys(xrange(5))" "d.has_key(4)"
100 loops, best of 3: 0.321 usec per loop
$ python -m timeit -s "d = dict.fromkeys(xrange(50))" "49 in d"
100 loops, best of 3: 0.253 usec per loop
$ python -m timeit -s "d = dict.fromkeys(xrange(50))"
"d.has_key(49)"
100 loops, best of 3: 0.391 usec per loop
$ python -m timeit -s "d = dict.fromkeys(xrange(50))" "100 in d"
100 loops, best of 3: 0.208 usec per loop
$ python -m timeit -s "d = dict.fromkeys(xrange(50))"
"d.has_key(100)"
100 loops, best of 3: 0.324 usec per loop

FWIW, as comparison:

$ python -m timeit -s "l = range(50)" "0 in l"
1000 loops, best of 3: 0.198 usec per loop
$ python -m timeit -s "l = range(50)" "49 in l"
10 loops, best of 3: 19.8 msec per loop

(Python 2.5.1, Ubuntu. Of course, timings vary a bit, but not much. At
worst, in and has_key are "about the same".)
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: multi-Singleton-like using __new__

2008-02-08 Thread Matt Nordhoff
Steven D'Aprano wrote:
> Except that using has_key() means making an attribute lookup, which takes 
> time.

I was going to say that, but doesn't 'in' require an attribute lookup of
some sort too, of __contains__ or whatever? has_key is probably now just
a wrapper around that, so it would be one more attribute lookup. What's
the difference in performance between attribute lookups from Python code
and from the internal C code?

> Now, if you want to argue that the difference between 0.3 microseconds 
> and 0.2 microseconds is insignificant, I'd agree with you -- for a single 
> lookup. But if you have a loop where you're doing large numbers of 
> lookups, using in will be a significant optimization.

I meant that it's (usually) insignificant, and barely larger than the
usual inaccuracy due to my system's load (I'm tired; what's the word for
that?).
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: different key, same value in dictionaries

2008-02-09 Thread Matt Nordhoff
Gary Herron wrote:
> You could use ImmutableSets as indexes.  (In fact this is the whole
> reason for the existence of ImmutableSets.)
> 
> You could derive your own dictionary type from the builtin dictionary
> type, and map an index operation d[(x,y)] to
> d[ImmutableSet(a,b)].  Then all of d[a,b], d[b,a], d[(a,b)] and d[(b,a)]
> would index the same element.
> 
> See http://docs.python.org/lib/module-sets.html for details of the set
> module.

Since Python 2.4, sets are built-in types. Use "set" and "frozenset"
instead of "sets.Set" and "sets.ImmutableSet", respectively.

If you need Python 2.3 compatibility, you can do something like this:

try:
set, frozenset
except NameError:
from sets import Set as set, ImmutableSet as frozenset
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is there a web visitor counter available in Python ...

2008-02-11 Thread Matt Nordhoff
W. Watson wrote:
> ... that is free for use without advertising that I can use on my web pages? 
> I have no idea is suitable for this. My knowledge of Python is somewhat 
> minimal at this point. Maybe Java is better choice.

You can analyze your web logs. That's more accurate than a hit counter,
though you can't use it for bragging rights.
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: idiom to ask if you are on 32 or 64 bit linux platform?

2008-02-11 Thread Matt Nordhoff
Jon wrote:
> Hello everyone,
> 
> I've got a ctypes wrapper to some code which seems to be different
> when compiled on 32 bit linux compared to 64 bit linux. For the
> windows version I can use sys.platform == 'win32' versus 'linux2' to
> decide whether to get the .dll or .so library to load. Having narrowed
> it down to linux I still need to choose between libKLT32.so and
> libKLT64.so
> 
> Can someone tell me an idiom to choose the right one?
> 
> Thanks!
> 
> Jon

What about the platform module? platform.architecture() should do it.





>>> import platform
>>> platform.architecture()
('32bit', '')

Note that it analyzes an executable (defaulting to the python
interpreter binary), rather than looking in /proc or something. You
might want to do something like that instead. I guess looking at the
python binary is more accurate on a hybrid 32-bit/64-bit system though..
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I execute a command from within python and wait on that command?

2008-02-15 Thread Matt Nordhoff
Rafael Sachetto wrote:
> os.system(command)
> 
> or
> 
> proc = popen2.Popen3(command)
> proc.wait()

I don't know about "cleanly terminat[ing] the command shell", but you
should use the subprocess module now, not any of the other functions
scattered around.
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to overcome the incomplete download with urllib.urlretrieve ?

2008-02-18 Thread Matt Nordhoff
This isn't super-helpful, but...

James Yu wrote:
> This is part of my code that invokes urllib.urlretrieve:
> 
> for i in link2Visit:
> localName = i.split('/')
> i = i.replace(' ', '%20')

You should use urllib.quote or urllib.quote_plus (the latter replaces
spaces with "+" instead of "%20") rather than a half-solution that only
escapes one character.

> tgtPath = ['d:\\', 'work', 'python', 'grab_n_view']
> localPath = ''
> for j in tgtPath:
> localPath = os.path.join(localPath, j)

You could do something like:

tgtPath = os.path.join('d:\\', 'work', 'python', 'grab_n_view')

(and don't repeat it on every loop).

> localPath = os.path.join(localPath, localName[-1])

What about using os.path.basename() instead of the localName business?

> info = urllib.urlretrieve(i, localPath)
> 
> link2Visit stores the url to some photos.
> After the script finishes running, I got a pile of incomplete JPG files,
> each takes only 413 bytes of my disk space.
> Did I miss something before using urllib.urlretrieve ?

Try opening one of the files in a text editor. It could be a "404 Not
Found" error document or something.
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: global variables: to be or not to be

2008-02-22 Thread Matt Nordhoff
icarus wrote:
> I've read 'global variables' are bad.  The ones that are defined as
> 'global' inside a function/method.
> 
> The argument that pops up every now and then is that they are hard to
> keep track of.  I don't know Python well enough to argue with that.
> Just started learning it a few days ago, so I won't get into
> philosophical questions such as "why this? Why not that?".  I'll take
> it as it is, just like I take 1 + 1 = 2 for granted.
> 
> So..."global variables taste bad.  Avoid them."
> 
> But how do I get around it? How do I update and access a variable
> anytime I want? Any easy-to-follow examples? Thanks in advance.

If doing everything inside one function doesn't work, you should use a
class to store state.

Here's a made-up example. Say you create a Python module to make HTTP
request. It stores the URL of the current one in a global variable. But
then anyone who uses your module can only work with one request at a
time. Instead, you should use an object called "HTTPResponse" or
something with an "url" attribute.

I don't know about other languages, but in Python, classes are very easy
to work with, so it should be no big deal. (Well, you may want to change
if your object evaluates to True, or if it's greater or smaller than 20,
or what happens when someone calls str() on it . . .  But you usually
shouldn't need to bother with all that.)
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: clocking subprocesses

2008-03-03 Thread Matt Nordhoff
[EMAIL PROTECTED] wrote:
> On Mar 3, 12:41 pm, Preston  Landers <[EMAIL PROTECTED]> wrote:
>> Run your command through the "time" program.  You can parse the output
>> format of "time", or set a custom output format.  This mostly applies
>> to Unix-like systems but there is probably an equivalent somewhere on
>> Windows.
>>
>> Preston
> 
> Thanks for the quick answer.  That seems to work, though, I'll write a
> timesubprocess() function which runs the program through time and
> spits the formatted out to a file, then parses that file, then returns
> the execution time.  There doesn't appear to be a more elegant way to
> do this.
> 
> Kevin

subprocess can do that easily.

What about something like this?

def timecall(args):
p = subprocess.Popen(['time', '--format', '%e %U %S'] + args,
 stderr=subprocess.PIPE)
p.wait()
timings = p.stderr.readline().split()
assert len(timings) == 3
timings = tuple(float(t) for t in timings)
return timings

It returns (real, user, sys), in seconds. The program's stdout goes to
sys.stdout, I don't know where the program's stderr goes, and it really
doesn't handle errors, but it's got the basic idea.
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: os.system with cgi

2008-03-03 Thread Matt Nordhoff
G wrote:
> Hi,
> 
>I have the following peace of code
> 
> def getBook(textid, path):
> url = geturl(textid)
> if os.path.isfile(path + textid):
> f = open(path + textid)
> else:
> os.system('wget -c ' + url + ' -O ' path + textid)
> f = open(path + textid)
> return f
> 
> The reason I am not using urllib is that I want to have random access
> within the downloaded file.
> 
> When i execute the file from a regular python script I get the file
> downloaded and a handle for the file returned.
> When I execute the file from a python cgi script i get an error saying
> that the file doesn't exist. In other words the call to os.system is not
> running.
> Could someone please point out what the problem with that peace of code
> running as a cgi script.
> 
> Best.

Ew. You could at least use subprocess and not be vulnerable to someone
passing "; rm -rf ~; echo" or something as the path.

If you need random access like that, and urllib2 can't do it, you could
use urllib2 and a StringIO or temporary file or something.

Using wget makes it much harder to handle errors.

Heck, you could just read the file into a string and use slicing, if
it's not too large...
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


[OT] Re: Why """, not '''?

2008-03-05 Thread Matt Nordhoff
Steven D'Aprano wrote:
> Surely it would depend on the type of text: pick up any random English 
> novel containing dialogue, and you're likely to find a couple of dozen 
> pairs of quotation marks per page, against a few apostrophes.

That's an idea... Write a novel in Python docstrings.

Someone make me go to bed now.
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Internet Explorer 8 beta release

2008-03-06 Thread Matt Nordhoff
Colin J. Williams wrote:
> Isn't compliance with the W3C standard 
> the best way of achieving multiple
> browser rendering?

Exactly. IE 8 is supposed to be significantly less horrible. It won't
catch up completely, but perhaps by IE 9 it will.

Or this is all a joke, Microsoft buys Opera and shuts them down.

(Anyway, I marked the OP as spam.)
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Distributed App - C++ with Python for Portability?

2008-03-10 Thread Matt Nordhoff
Paddy wrote:
> After profiling their may be other ways to remove a bottleneck, such
> as
> using existing highly-optimised libraries such as Numpy; Psycho, an
> optimising interpreter that can approach C type speeds for Python
> code;
> and you could create your own C++ based libraries.
> 
> You might want to ask the Mercurial development team how they got
> their
> impressive speed and functionality out of using mainly Python with
> critical regions in C. - Or watch this:
>   http://video.google.com/videoplay?docid=-7724296011317502612

For what you do decide to rewrite in C, you can also use a language like
Cython [1] (which is a fork of Pyrex [2]). It looks mostly like Python,
and is translated to C without you having to write all of the
boilerplate Python C API stuff. Of course, not quite as efficient
well-tuned raw C, but much more pleasant to write.

(FWIW, Bazaar [3], another VCS written in Python similar to Mercurial,
has rewritten parts of two modules in Pyrex. Another one is in raw C,
because that's what was contributed.)

[1] 
[2] 
[3] 
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Distributed App - C++ with Python for Portability?

2008-03-10 Thread Matt Nordhoff
Stefan Behnel wrote:
> And if you really need the efficiency of "well-tuned raw C", it's one function
> call away in your Cython code.

What do you mean by that?

I know nothing about how Cython compares to C in performance, so I said
"well-tuned" because it must be possible to write C that is faster than
Cython, though it may take some effort.
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: is operator

2008-03-10 Thread Matt Nordhoff
Metal Zong wrote:
> The operator is and is not test for object identity: x is y is true if
> and only if x and y are the same objects.
> 
 x = 1
 y = 1
 x is y
> True
> 
> Is this right? Why? Thanks.

I believe Python automatically creates and caches int objects for 0-256,
so whenever you use them, they refer to the same exact objects. Since
ints are immutable, it doesn't matter.
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: is operator

2008-03-10 Thread Matt Nordhoff
[EMAIL PROTECTED] wrote:
>> I believe Python automatically creates and caches int objects for 0-256,
>> so whenever you use them, they refer to the same exact objects. Since
>> ints are immutable, it doesn't matter.
> 
> One of the biggest hits on start-up time, by the way.  ;)

Well, the developers clearly decided it was worth it at the time. Who
knows, perhaps that's changed since then.
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Creating a file with $SIZE

2008-03-12 Thread Matt Nordhoff
Robert Bossy wrote:
> k.i.n.g. wrote:
>> I think I am not clear with my question, I am sorry. Here goes the
>> exact requirement.
>>
>> We use dd command in Linux to create a file with of required size. In
>> similar way, on windows I would like to use python to take the size of
>> the file( 50MB, 1GB ) as input from user and create a uncompressed
>> file of the size given by the user.
>>
>> ex: If user input is 50M, script should create 50Mb of blank or empty
>> file
>>   
> def make_blank_file(path, size):
> f = open(path, 'w')
> f.seek(size - 1)
> f.write('\0')
> f.close()
> 
> I'm not sure the f.seek() trick will work on all platforms, so you can:
> 
> def make_blank_file(path, size):
> f = open(path, 'w')
> f.write('\0' * size)
> f.close()

I point out that a 1 GB string is probably not a good idea.

def make_blank_file(path, size):
chunksize = 10485760 # 10 MB
chunk = '\0' * chunksize
left = size
fh = open(path, 'wb')
while left > chunksize:
fh.write(chunk)
left -= chunksize
if left > 0:
fh.write('\0' * left)
fh.close()

> Cheers,
> RB
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Update pytz timezone definitions

2008-03-14 Thread Matt Nordhoff
_robby wrote:
> I am looking at using pytz in a scheduling application which will be
> used internationally. I would like to be able to update the definition
> files that pytz uses monthly or bi-monthly.
> 
> As far as I can tell, pytz seems to be updated (fairly) regularly to
> the newest tzdata, but I don't want to have to update my pytz, just
> it's definitions.
> 
> http://www.twinsun.com/tz/tz-link.htm says that pytz "compiles tz
> source into Python." Does this mean that there is already a method for
> updating the definitions?
> 
> Any help would be greatly appreciated, even if it is to point out
> something obvious which I over looked.
> 
> - Robby

pytz's build process is rather complicated (e.g., a list of all time
zones is appended to pytz/__init__.py). I really don't think it would be
worth the effort.

python-dateutil [1] [2] provides time zone support similar to pytz's,
among other features. It keeps the time zone files in a tarball and I'm
pretty sure it would be easy to update.

I still don't get why you'd want to go to the effort though. Upgrading
the whole package is easy. It's not like pytz gets a new API every version.

[1] 
[2] 
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: request for Details about Dictionaries in Python

2008-03-14 Thread Matt Nordhoff
Michael Wieher wrote:
> I'm not sure if a well-written file/seek/read algorithm is faster than a
> relational database...
> sure a database can store relations and triggers and all that, but if
> he's just doing a lookup for static data, then I'm thinking disk IO is
> faster for him?  not sure

I would think that rolling your own solution would get complicated
enough that it would be easier to just use SQLite or something. If you
wanted to go to the effort, you could probably get something faster, but
if SQLite is fast enough, who cares?

Hmm, if Perl's on-disk hash tables are good, maybe someone should port
them to Python, or maybe someone already has.

Also, for translations, maybe there's a good library already available.
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unicode/UTF-8 confusion

2008-03-15 Thread Matt Nordhoff
Tom Stambaugh wrote:
> I'm still confused about this, even after days of hacking at it. It's
> time I asked for help. I understand that each of you knows more about
> Python, Javascript, unicode, and programming than me, and I understand
> that each of you has a higher SAT score than me. So please try and be
> gentle with your responses.
>  
> I use simplejson to serialize html strings that the server is delivering
> to a browser. Since the apostrophe is a string terminator in javascript,
> I need to escape any apostrophe embedded in the html.
>  
> Just to be clear, the specific unicode character I'm struggling with is
> described in Python as:
> u'\N{APOSTROPHE}'}. It has a standardized utf-8 value (according to, for
> example, http://www.fileformat.info/info/unicode/char/0027/index.htm)
> of  0x27.
>  
> This can be expressed in several common ways:
> hex: 0x27
> Python literal: u"\u0027"
>  
> Suppose I start with some test string that contains an embedded
> apostrophe -- for example: u"   '   ". I believe that the appropriate
> json serialization of this is (presented as a list to eliminate notation
> ambiguities):
>  
> ['"', ' ', ' ', ' ', '\\', '\\', '0', '0', '2', '7', ' ', ' ', ' ', '"']
>  
> This is a 14-character utf-8 serialization of the above test string.
>  
> I know I can brute-force this, using something like the following:
> def encode(aRawString):
> aReplacement = ''.join(['\\', '0', '0', '2', '7'])
> aCookedString = aRawString.replace("'", aReplacement)
> answer = simplejson.dumps(aCookedString)
> return answer
>  
> I can't even make mailers let me *TYPE* a string literal for the
> replacement string without trying to turn it into an HTML link!
>  
> Anyway, I know that my "encode" function works, but it pains me to add
> that "replace" call before *EVERY* invocation of the simplejson.dumps()
> method. The reason I upgraded to 1.7.4 was to get the c-level speedup
> routine now offered by simplejson -- yet the need to do this apostrophe
> escaping seems to negate this advantage! Is there perhaps some
> combination of dumps keyword arguments, python encode()/str() magic, or
> something similar that accomplishes this same result?
>  
> What is the highest-performance way to get simplejson to emit the
> desired serialization of the given test string?

simplejson handles all necessary escaping of stuff like quotes...
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Generators

2008-03-16 Thread Matt Nordhoff
mpc wrote:



> def concatenate(sequences):
> for seq in sequences:
> for item in seq:
> yield item

You should check out itertools.chain(). It does this. You call it like
"chain(seq1, seq2, ...)" instead of "chain(sequences)" though, which may
be a problem for you.

The rest of itertools might be interesting too:




-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem with PARAGRAPH SEPARATOR

2008-03-20 Thread Matt Nordhoff
[EMAIL PROTECTED] wrote:
> Actually that's what I tried to do, for example:
> outputString = myString.encode('iso-8859-1','ignore')
> 
> However, I always get such messages (the character, that's causing problems 
> varies, but its always higher than 127 ofc...)
> 
> 'ascii' codec can't decode byte 0xc3 in position 51: ordinal not in 
> range(128) 
> 
> It doesn't matter what encoding I use (tried 'utf-8' 'utf-16' 'latin_1' and 
> the iso one). The debugger is showing all the special characters (from french 
> and german language) so I'm wondering why there's still the message about the 
> 'ascii' codec...
> 
> Would that mean that the string "myString" is an ascii-string or what?

That's a *decode* error. If myString is a str object, it has to be
*decoded* before it can be *encoded* to ISO-8859-1 or whatever you want,
and it's defaulting to ASCII.

This page may help:

-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Strange loop behavior

2008-03-26 Thread Matt Nordhoff
Gabriel Rossetti wrote:
> Hello,
> 
> I wrote a program that reads data from a file and puts it in a string, 
> the problem is that it loops infinitely and that's not wanted, here is 
> the code :
> 
> d = repr(f.read(DEFAULT_BUFFER_SIZE))
> while d != "":
> file_str.write(d)
> d = repr(f.read(DEFAULT_BUFFER_SIZE))

FWIW, you could do it like this to avoid duplicating that line of code:

while True:
d = f.read(DEFAULT_BUFFER_SIZE)
if not d:
break
file_str.write(d)

Some people would also compress the whitespace a bit:

while True:
d = f.read(DEFAULT_BUFFER_SIZE)
if not d: break
file_str.write(d)



(I'll comment on your Unicode issues in a minute.)
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: v = json.loads("{'test':'test'}")

2009-01-25 Thread Matt Nordhoff
gert wrote:
> On Jan 25, 11:16 pm, Дамјан Георгиевски  wrote:
>>> raise ValueError(errmsg("Expecting property name", s, end))
>>> http://docs.python.org/library/json.html
>>> What am I doing wrong ?
>> try this
>> v = json.loads('{"test":"test"}')
>>
>> JSON doesn't support single quotes, only double quotes.
> 
> the funny part is when you print(v) you get
> {'test': 'test'}
> 
> Single quotes works in every browser that support json so i
> recommended python should support it too, besides it looks much
> cleaner
> {'test': 'test'}
> {"test": "test"}
> 
> It can not be that hard to support both notation can it ?

There's a difference between JavaScript source code and JSON. AFAICT
from the source [1], Mozilla's JSON parser doesn't accept single quotes.

[1] 
-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: v = json.loads("{'test':'test'}")

2009-01-25 Thread Matt Nordhoff
Matt Nordhoff wrote:
> gert wrote:
>> On Jan 25, 11:16 pm, Дамјан Георгиевски  wrote:
>>>> raise ValueError(errmsg("Expecting property name", s, end))
>>>> http://docs.python.org/library/json.html
>>>> What am I doing wrong ?
>>> try this
>>> v = json.loads('{"test":"test"}')
>>>
>>> JSON doesn't support single quotes, only double quotes.
>> the funny part is when you print(v) you get
>> {'test': 'test'}
>>
>> Single quotes works in every browser that support json so i
>> recommended python should support it too, besides it looks much
>> cleaner
>> {'test': 'test'}
>> {"test": "test"}
>>
>> It can not be that hard to support both notation can it ?
> 
> There's a difference between JavaScript source code and JSON. AFAICT
> from the source [1], Mozilla's JSON parser doesn't accept single quotes.
> 
> [1] <http://mxr.mozilla.org/mozilla-central/source/js/src/json.cpp>

By the way, I forgot to add, according to the ECMA-262 standard (page
18, section 7.8.4) [1], ECMAScript string literals can use either double
or single quotes, so that's not a browser-specific extension.

[1]
<http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf>
(<http://xrl.us/bedr3c>)
-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: time: Daylight savings confusion

2009-02-05 Thread Matt Nordhoff
Tim H wrote:
> On Win XP 64bit, Python 2.6.1 64bit
> 
> I am trying to rename files by their creation time.
> 
> It seems the time module is too smart for its own good here.
> 
> time.localtime(os.path.getctime(f)) returns a value one hour off from
> what windows reports for files that were created when Daylight savings
> time was in effect (ie when the tm_isdst field is one).  I can kludge
> it, but am I missing the "right" way to do it?
> 
> Tim
> 
> full code:



>From what I remember, this is a bug in the Win32 API. Python probably
could hack around it, but, well, I guess it doesn't.

Googling a bit, Perl's Win32::UTCFileTime module [1] provides
workarounds (not for Python, of course, but it could probably be ported)
and explains all the gory details (which I have not read, so maybe I
misunderstand ;-).

[1]


-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: Revision Control

2009-02-18 Thread Matt Nordhoff
Tim Chase wrote:



>> -Mercurial (this is a big up and coming RCS)
> 
> This is currently my favorite:  good branching/merging, fast, written
> mostly in Python (one C extension module, IIRC), and a simple interface
> 
>> -Bazaar (written in Python. Also pretty new. I don't know about Windows
>> support)
> 
> I like Bazaar (bzr), but the last several times I've tried it, it's been
> a little slow.  This has been steadily improving, and I like their
> emphasis on correctness foremost.  It's a lot like mercurial, but is
> pure python (no C extension module) which makes it nice to deploy into
> environments such as my shared web-hosting service where I can't build
> extension C modules or install packages such as hg/git/svn from the
> repository.

FWIW, Bazaar and Mercurial both have about half a dozen C modules. (Most
of Bazaar's are Pyrex, though, not straight C.)

The next release of Mercurial will add support for running without them.
 (Bazaar already supports that, of course.)


-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: generating a liste of characters

2008-12-04 Thread Matt Nordhoff
This is a slightly old post, but oh well...

Shane Geiger wrote:
> import string
> alphabet=list(string.letters[0:26])
> print alphabet

Most of the string module's constants are locale-specific. If you want
the ASCII alphabet instead of the current language's, you need to use
string.ascii_{letters,lowercase,uppercase}.

> Yves Dorfsman wrote:
>> Is there any built in way to generate a list of characters, something
>> along the line of range('a'-'z') ?
>>
>> Right now I am using:
>>
>>   chars  = [ chr(l)  for l in range(0x30, 0x3a) ] # 0 - 9
>>   chars += [ chr(l)  for l in range(0x41, 0x5b) ] # A - Z
>>   chars += [ chr(l)  for l in range(0x61, 0x7b) ] # a - z
>>
>> Is there a better, more straight forward way of doing that ?
>>
>>
>>
>> Thanks.
-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: html codes

2008-12-09 Thread Matt Nordhoff
Daniel Fetchinson wrote:
> Hi folks,
> 
> I came across a javascript library that returns all sorts of html
> codes in the cookies it sets and I need my web framework (written in
> python :)) to decode them. I'm aware of htmlentitydefs but
> htmlentitydefs.entitydefs.keys( ) are of the form '&#xxx' but this
> javascript library uses stuff like '%3A' for the ':' for example. The
> conversion is here:
> 
> http://www.ascii.cl/htmlcodes.htm
> 
> Is there a python package/module/whatever that does the conversion for
> me or do I have to write a little wrapper myself (and introduce bugs
> while doing so :))?
> 
> Cheers,
> Daniel

>>> import urllib
>>> urllib.unquote('%20')
' '


-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: Using the `email' module in a bazaar plugin

2008-12-10 Thread Matt Nordhoff
Julian Smith wrote:
> I don't seem to be able to use the `email' module from within a bazaar
> plugin. Other modules work ok, e.g. nntplib.
> 
> Here's my plugin, cut-down to show just the email problem:
> 
>   import sys
>   print 'sys.version', sys.version
> 
>   import nntplib
>   n = nntplib.NNTP( 'jsmith-ubuntu2' )
>   print n
> 
>   import email
>   m=email.MIMEText.MIMEText('text of email')
> 
> - and here's the output when i run any bazaar command:
> 
>   > bzr status
>   sys.version 2.5.2 (r252:60911, Jul 31 2008, 17:28:52) 
>   [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)]
>   
>   'module' object has no attribute 'MIMEText'
>   Unable to load plugin 'js_notify' from '/home/jsmith/.bazaar/plugins'
>   ...
> 
> The plugin runs fine on its own, it's only when loaded into Bazaar that things
> go wrong.
> 
> I thought perhaps this could be caused by the `email' module's use of
> LazyImporter. I've tried various things such as `from email.mime.text import
> MIMEText', and they fail in similar ways.
> 
> I'm using bzr-1.9, python 2.5.2, on Ubuntu-8.xx.
> 
> Does anyone have any ideas ?
> 
> Thanks,
> 
> - Julian

The built-in email module is probably getting shadowed by another one
(perhaps bzrlib.plugins.email?), so "import email" is importing the
wrong module. You'll have to rename it to something else.

Have your plugin print email.__path__ to see which module it is.
-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: Free place to host python files?

2008-12-17 Thread Matt Nordhoff
James Mills wrote:
> On Wed, Dec 17, 2008 at 10:25 AM, Chris Rebert  wrote:
>> I'll plug Bitbucket (http://bitbucket.org/). It gives you 150MB of
>> Mercurial hosting for free, along with a bug tracker and wiki. And I
>> hear it's implemented using Django.
> 
> FreeHG (http://freehg.org) is pretty good too :)
> 
> cheers
> James

I'll throw in  too. No wiki, though.

And , I guess.
-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: using subprocess module in Python CGI

2008-12-23 Thread Matt Nordhoff
ANURAG BAGARIA wrote:
> Hello,
> 
> I am a Python Newbie and would like to call a short python script via
> browser using a CGI script, but initially I am trying to call the same
> python script directly through python command line. The script intends
> to perform a few command line in a pipe and I have written the script (a
> short one) as follows.
> 
> #!/usr/bin/python
> 
> import cgi, string, os, sys, cgitb, commands, subprocess
> import posixpath, macpath
> #file = "x.tar.gz"
> #comd = "tar -xf %s" % (file)
> #os.system(comd)
> #commands.getoutput('tar -xf x.tar.gz | cd demo; cp README ../')
> comd = [\
> "tar -xf x.tar.gz", \
> "cd demo", \
> "cp README ../", \
>   ]

That's not how subprocess.call() works. You're trying to run an
executable called "tar -xf x.tar.gz", passing it the arguments "cd demo"
and "cp README ../".

> outFile = os.path.join(os.curdir, "output.log")
> outptr = file(outFile, "w")
> errFile = os.path.join(os.curdir, "error.log")
> errptr = file(errFile, "w")
> retval = subprocess.call(comd, 0, None, None, outptr, errptr)
> errptr.close()
> outptr.close()
> if not retval == 0:
> errptr = file(errFile, "r")
> errData = errptr.read()
> errptr.close()
> raise Exception("Error executing command: " + repr(errData))
> 
> 
> but after trying to execute this independently, I get the following
> error which I am unable to interpret :
> 
> Traceback (most recent call last):
>   File "process.py", line 18, in 
> retval = subprocess.call(comd, 0, None, None, outptr, errptr)
>   File "/usr/lib/python2.5/subprocess.py", line 443, in call
> return Popen(*popenargs, **kwargs).wait()
>   File "/usr/lib/python2.5/subprocess.py", line 593, in __init__
> errread, errwrite)
>   File "/usr/lib/python2.5/subprocess.py", line 1135, in _execute_child
> raise child_exception
> 
> 
> Could someone suggest where am I going wrong and if corrected, what is
> the probability of this script being compatible with being called
> through the browser. Thanking you people in advance.

Well, you'd need to output something, but otherwise, sure, why not?

print "Content-Type: text/html"
print
print "..."

> Regards.

Why do you even need to use subprocess to do this? All it's doing is
extracting the README file from a tarball, right? You can use the
tarfile module for that.


-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: Inefficient summing

2008-10-09 Thread Matt Nordhoff
Chris Rebert wrote:
> I personally would probably do:
> 
> from collections import defaultdict
> 
> label2sum = defaultdict(lambda: 0)

FWIW, you can just use:

label2sum = defaultdict(int)

You don't need a lambda.

> for r in rec:
> for key, value in r.iteritems():
> label2sum[key] += value
> 
> ratio = label2sum["F1"] / label2sum["F2"]
> 
> This iterates through each 'r' only once, and (imho) is pretty
> readable provided you know how defaultdicts work. Not everything has
> to unnecessarily be made a one-liner. Coding is about readability
> first, optimization second. And optimized code should not be
> abbreviated, which would make it even harder to understand.
> 
> I probably would have gone with your second solution if performance
> was no object.
> 
> Cheers,
> Chris
-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: Implementing my own Python interpreter

2008-10-13 Thread Matt Nordhoff
Ognjen Bezanov wrote:
> Hello All,
> 
> I am a third year computer science student and I'm the process of
> selection for my final year project.
> 
> One option that was thought up was the idea of implement my own version
> of the python interpreter (I'm referring to CPython here). Either as a
> process running on another OS or as a process running directly on the CPU.
> 
> Now, I can't seem to find a decent source of information on the python
> interpreter. I have made the assumption that Python works very much like
> Java, you have code that is compiled into bytecode, which is then
> executed in a virtual machine. IS this correct? Is there a good source
> to give me an overview of Python internals? (I can look at the code, but
> I would find it easier to understand if I can see the "big picture" as
> well)
> 
> Also, any pro's out there willing to chime on the feasibility of
> implementing python to run directly on the hardware (without an
> underlying OS)? I don't expect 100% compatibility, but would the basics
> (branching, looping, arithmatic) be feasible?
> 
> Thank you,
> 
> 
> Ognjen

FWIW... There are several other implementations of Python:

IronPython (.Net)
Jython (Java)
PyPy (Python) 

You might find working on one of them interesting, or maybe even CPython
itself.
-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: Antigravity module needed.

2008-10-17 Thread Matt Nordhoff
Terry Reedy wrote:
> If Python added an antigravity module to the stdlib,
> what should it say or do?  See
> http://xkcd.com/353/
> (and let your mouse hover).

It was added 2 days ago. :-P


-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: Simple print to stderr

2008-10-27 Thread Matt Nordhoff
RC wrote:
> By default the print statement sends to stdout
> I want to send to stderr
> 
> Try
> 
> print "my meeage", file=sys.stderr
> 
> I got
>> SyntaxError: invalid syntax
> 
> I try
> 
> print "my message", sys.stderr
> 
> But it still sent to stdout.
> What is the syntax?
> 
> I wouldn't understand Python's manual



That's only in Python 3 (or 2.6 with the proper __future__ import).
Before that, print is a statement. You'd do it like this:

print >> sys.stderr, "whatever"

You should look at
,
not Python 3's documentation.
-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: Need help in understanding a python code

2008-11-16 Thread Matt Nordhoff
Benjamin Kaplan wrote:
> If you really believe that, you haven't been following this list long
> enough. Every terminology dispute always includes at least 1 Wikipedia
> link.
> 
> Also, you might want to look at this study:
> http://news.cnet.com/2100-1038_3-5997332.html

That study has been disputed; see the links at the top of
.

/me ducks
-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: A tale of two execs

2009-02-23 Thread Matt Nordhoff
aha wrote:
> Hello All,
>   I am working on a project where I need to support versions of Python
> as old as 2.3. Previously, we distributed Python with our product, but
> this seemed a bit silly so we are no longer doing this.  The problem
> that I am faced with is that we have Python scripts that use the
> subprocess module, and subprocess is not available in Python 2.3.
> Below is the strategy I am thinking about using, however if, you have
> better ideas please let me know.
> 
> def runner(cmd, stdin, stdout, ...):
>   try:
> import subprocess
> sbm = 1
>   except:
> sbm = 0
> 
>   # Now do something
>   if sbm:
> process = subporcess(...)
>   else:
> import popen2
> process = popen2.Popen4(...)
> 
> Has anyone else run into a situation similar to this one?

FWIW, the Mercurial project went the other way: They wrote "popen2",
"popen3" and "Popen3" functions that were wrappers around subprocess:


-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: Set & Frozenset?

2009-03-09 Thread Matt Nordhoff
Alan G Isaac wrote:
>> Hans Larsen schrieb:
>>> How could I "take" an elemment from a set or a frozenset 
> 
> 
> On 3/8/2009 2:06 PM Diez B. Roggisch apparently wrote:
>> You iterate over them. If you only want one value, use
>> iter(the_set).next()
> 
> 
> I recall a claim that
> 
> for result in myset: break
> 
> is the most efficient way to get one result.
> Is this right? (It seems nearly the same.)
> 
> Alan Isaac

Checking Python 2.5 on Linux, your solution is much faster, but seeing
as they both come in under a microsecond, it hardly matters.
-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: print - bug or feature - concatenated format strings in a print statement

2009-03-17 Thread Matt Nordhoff
bdb112 wrote:
> Thanks for all the replies:
> I think I see now - % is a binary operator whose precedence rules are
> shared with the modulo operator regardless of the nature of its
> arguments, for language consistency.
> I understand the arguments behind the format method, but hope that the
> slightly idiosyncratic print( ..% ..) remains, as the vaguely
> pictorial "printf" format string is clearer for a long line with
> several arguments.
> I will use the "implicit string concatenation" to solve my problem but
> it is a little odd that an "invisible" operator is stronger than a
> visible one. (+).

The implicit string concatenation is actually done by the compiler; it
isn't an operator at all. Look:

>>> import dis
>>> def f():
... return "foo" "bar"
...
>>> dis.dis(f)
  2   0 LOAD_CONST   1 ('foobar')
  3 RETURN_VALUE
-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: download x bytes at a time over network

2009-03-17 Thread Matt Nordhoff
Saurabh wrote:
> Heres the reason behind wanting to get chunks at a time.
> Im actually retrieving data from a list of RSS Feeds and need to
> continuously check for latest posts.
> But I dont want to depend on Last-Modified header or the pubDate tag
> in . Because a lot of feeds just output date('now')  instead
> of the actual last-updated timestamp.
> But when continuously checking for latest posts, I dont want to
> bombard other people's bandwidth - so I just want to get chunks of
> bytes at a time and internally check for ... with my
> database against timestamp values.
> Is there a better way to achieve this ?

For the feeds that *do* set Last-Modified properly, won't you be using
*more* bandwidth by downloading part of the feed instead of just using
If-Modified-Since?
-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: unsubscribe to send daily mails

2009-03-26 Thread Matt Nordhoff
Sudheer Rapolu wrote:
> Hello
> 
> Please unsubscribe to send daily mails to me.
> 
> Warm Regards
> Sudheer
> 
> 
> 
> 
> --
> http://mail.python.org/mailman/listinfo/python-list

See the link in the signature of every message, or the headers of every
message:

> List-Unsubscribe: ,
>   
-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: UnicodeEncodeError - opening encoded URLs

2009-03-27 Thread Matt Nordhoff
D4rko wrote:
> Hi!
> 
> I have a problem with urllib2 open() function. My application is
> receiving the following request - as I can see in the developement
> server console it is properly encoded:
> 
> [27/Mar/2009 22:22:29] "GET /[blahblah]/Europa_%C5%9Arodkowa/5 HTTP/
> 1.1" 500 54572
> 
> Then it uses this request parameter as name variable to build
> wikipedia link, and tries to acces it with following code:
> 
>   url = u'http://pl.wikipedia.org/w/index.php?title=' + name +
> '&printable=yes'
>   opener = urllib2.build_opener()
>   opener.addheaders = [('User-agent', 'Mozilla/5.0')]
>   wikipage = opener.open(url)
> 
> Unfortunately, the last line fails with the exception:
> UnicodeEncodeError 'ascii' codec can't encode character u'\u015a' in
> position 30: ordinal not in range(128).  Using urlencode(url) results
> in TypeError "not a valid non-string sequence or mapping object", and
> quote(url)  fails because of KeyError u'\u015a' . How can I properly
> parse this request to make it work (ie. acces
> http://pl.wikipedia.org/wiki/Europa_%C5%9Arodkowa)?

What if you just used a regular byte string for the URL?

>>> url = 'http://pl.wikipedia.org/w/index.php?title=' + name +
'&printable=yes'

(Unless "name" is a unicode object as well.)

(Nice user-agent, BTW. :-P )
-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: Detecting Binary content in files

2009-03-31 Thread Matt Nordhoff
ritu wrote:
> Hi,
> 
> I'm wondering if Python has a utility to detect binary content in
> files? Or if anyone has any ideas on how that can be accomplished? I
> haven't been able to find any useful information to accomplish this
> (my other option is to fire off a perl script from within m python
> script that will tell me whether the file is binary), so any pointers
> will be appreciated.
> 
> Thanks,
> Ritu

There isn't any perfect test. The usual heuristic is to check if there
are any NUL bytes in the file:

>>> '\0' in some_string

That can fail, of course. UTF-16-encoded text will have tons of NUL
bytes, and some binary files may not have any.
-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: safe eval of moderately simple math expressions

2009-04-09 Thread Matt Nordhoff
Joel Hedlund wrote:
> Hi all!
> 
> I'm writing a program that presents a lot of numbers to the user, and I
> want to let the user apply moderately simple arithmentics to these
> numbers. One possibility that comes to mind is to use the eval function,
> but since that sends up all kinds of warning flags in my head, I thought
> I'd put my idea out here first so you guys can tell me if I'm insane. :-)
> 
> This is the gist of it:
> --
> import math
> 
> globals = dict((s, getattr(math, s)) for s in dir(math) if '_' not in s)
> globals.update(__builtins__=None, divmod=divmod, round=round)
> 
> def calc(expr, x):
> if '_' in expr:
> raise ValueError("expr must not contain '_' characters")
> try:
> return eval(expr, globals, dict(x=x))
> except:
> raise ValueError("bad expr or x")
> 
> print calc('cos(x*pi)', 1.33)
> --
> 
> This lets the user do stuff like "exp(-0.01*x)" or "round(100*x)" but
> prevents malevolent stuff like "__import__('os').system('del *.*')" or
> "(t for t in (42).__class__.__base__.__subclasses__() if t.__name__ ==
> 'file').next()" from messing things up.
> 
> I assume there's lots of nasty and absolutely lethal stuff that I've
> missed, and I kindly request you show me the error of my ways.
> 
> Thank you for your time!
> /Joel Hedlund

I'm way too dumb and lazy to provide a working example, but someone
could work around the _ restriction by obfuscating them a bit, like this:

>>> '\x5f'
'_'
>>> getattr(42, '\x5f\x5fclass\x5f\x5f') # __class__


Is that enough to show you the error of your ways? :-D Cuz seriously,
it's a bad idea.

I'm sorry, but I don't know a good solution. The simplicity of eval is
definitely very attractive, but it's just not safe.

(BTW: What if a user tries to do some ridiculously large calculation to
DoS the app? Is that a problem?)
-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: Scrap Posts

2009-04-09 Thread Matt Nordhoff
Avi wrote:
> Hey Folks,
> 
> I love this group and all the awesome and python savvy people who post
> here. However I also see some dumb posts like 'shoes' or something
> related to sex :(
> 
> What can we do about crap like this? Can we clean it up? Or atleast
> flag some for removal.
> 
> Moderators?

FWIW, this newsgroup is mirrored (both ways) as a mailing list. The list
server runs some pretty effective anti-spam software, and you could
subscribe to it:


-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: JSON and Firefox sessionstore.js

2009-04-24 Thread Matt Nordhoff
Steven D'Aprano wrote:
> On Thu, 23 Apr 2009 05:08:35 +0100, I V wrote:
> 
>> For something with at least a vague air of credibility to it, somebody
>> who appears to be a Mozilla developer comments on their bug tracker,
>> that sessionstore.js isn't "pure JSON" (though he only identifies the
>> parantheses, which are much easier to fix, as being a problem):
>>
>> https://bugzilla.mozilla.org/show_bug.cgi?id=407110#c2
> 
> Ah, fantastic! Or rather, damn, I was hoping it was proper JSON and not 
> some sort of bizarre mutant.
> 
> For what it's worth, removing the leading/trailing parentheses gives a 
> different error:



> Presumably that's the error you get when you don't quote your property 
> names properly.

That bug is about changing it *to* JSON-plus-parentheses. The patch
landed [1] (and was subsequently backed out [2] and landed again [3]) in
time for Firefox 3.5, but unless you're running that, it's still
JavaScript source code, generated by toSource() [4]:


()

[1] 
[2] 
[3] 
[4]

()
-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: print(f) for files .. and is print % going away?

2009-04-30 Thread Matt Nordhoff
Esmail wrote:
> Hello all,
> 
> I use the print method with % for formatting my output to
> the console since I am quite familiar with printf from my
> C days, and I like it quite well.
> 
> I am wondering if there is a way to use print to write
> formatted output to files?
> 
> Also, it seems like I read that formatting with print is
> going away eventually and we should start using something
> else? Is that true and if so, what?
> 
> Thanks,
> Esmail

String formatting has nothing to do with the print statement/function.
It's an operator, just like doing "foo" + "bar"; you can use it wherever
you want.

Look:

>>> "%s" % ('foo',)
'foo'
>>> len("%s" % ('foo',))
3
>>> d = {}
>>> d["%s" % ('foo',)] = 1
>>> d
{'foo': 1}
>>> ("%s" % ('foo',)).upper()
'FOO'
>>>

See 
Also see  for
information on the replacement for the old string formatting, Python
2.6's new str.format().
-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: Use of Unicode in Python 2.5 source code literals

2009-05-03 Thread Matt Nordhoff
Uncle Bruce wrote:
> I'm working with Python 2.5.4 and the NLTK (Natural Language
> Toolkit).  I'm an experienced programmer, but new to Python.
> 
> This question arose when I tried to create a literal in my source code
> for a Unicode codepoint greater than 255.  (I also posted this
> question in the NLTK discussion group).
> 
> The Python HELP (at least for version 2.5.4) states:
> 
> +++
> Python supports writing Unicode literals in any encoding, but you have
> to declare the encoding being used. This is done by including a
> special comment as either the first or second line of the source file:
> 
> #!/usr/bin/env python
> # -*- coding: latin-1 -*-
> 
> 
> Based on some experimenting I've done, I suspect that the support for
> Unicode literals in ANY encoding isn't really accurate.  What seems to
> happen is that there must be an 8-bit mapping between the set of
> Unicode literals and what can be used as literals.
> 
> Even when I set Options / General / Default Source Encoding to UTF-8,
> IDLE won't allow Unicode literals (e.g. characters copied and pasted
> from the Windows Character Map program) to be used, even in a quoted
> string, if they represent an ord value greater than 255.
> 
> I noticed, in researching this question, that Marc Andre Lemburg
> stated, back in 2001, "Since Python source code is defined to be
> ASCII..."
> 
> I'm writing code for linguistics (other than English), so I need
> access to lots more characters.  Most of the time, the characters come
> from files, so no problem.  But for some processing tasks, I simply
> must be able to use "real" Unicode literals in the source code.
> (Writing hex escape sequences in a complex regex would be a
> nightmare).
> 
> Was this taken care of in the switch from Python 2.X to 3.X?
> 
> Is there a way to use more than 255 Unicode characters in source code
> literals in Python 2.5.4?
> 
> Also, in the Windows version of Python, how can I tell if it was
> compiled to support 16 bits of Unicode or 32 bits of Unicode?
> 
> Bruce in Toronto

Works for me:

--- snip ---
$ cat snowman.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import unicodedata

snowman = u'☃'

print len(snowman)
print unicodedata.name(snowman)
$ python2.6 snowman.py
1
SNOWMAN
--- snip ---

What did you set the encoding to in the declaration at the top of the
file? The help text you quoted uses latin-1 as an example, an encoding
which, of course, only supports 256 code points. Did you try utf-8 instead?

The regular expression engine's Unicode support is a different question,
and I do not know the answer.

By the way, Python 2.x only supports using non-ASCII characters in
source code in string literals. Python 3 adds support for Unicode
identifiers (e.g. variable names, function argument names, etc.).
-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: Simple way of handling errors

2009-05-07 Thread Matt Nordhoff
Steven D'Aprano wrote:
> On Wed, 06 May 2009 20:21:38 -0700, TomF wrote:
> 
>>> The only reason you would bother going to the time and effort of
>>> catching the error, printing your own error message, and then exiting,
>>> is if you explicitly want to hide the traceback from the user.
>> Well, to me, exposing the user to such raw backtraces is unprofessional,
>> which is why I try to catch user-caused errors.  But I suppose I have an
>> answer to my question.
> 
> That depends on your audience. Not every program is written to be used 
> for a technical incompetent audience. Some users actually *want* to see 
> the errors.
> 
> But certainly there are large classes of applications where you do want 
> to suppress the traceback. That's why I said "if you explicitly want to 
> hide the traceback from the user" rather than "don't do this".
> 
> The idiom I use is to wrap the *entire* application in a single 
> try...except block, and then put all your user-friendly error handling in 
> one place, instead of scattered over the entire application:
> 
> 
> try:
> main(sys.argv[1:])
> except KeyboardInterrupt, SystemExit:

That should be:

except (KeyboardInterrupt, SystemExit):

;-D

> raise
> except Exception, e:
> log(e)
> print >>sys.stderr, str(e)
> sys.exit(1)
> 
> 
> 
> Hope this helps.
-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: confused with subprocess.Popen

2009-05-09 Thread Matt Nordhoff
Soumen banerjee wrote:
> Hello,
> for a certain app, i used to use a command:
> os.system("soundwrapper espeak -f line.txt")
> now, if i wanted to kill espeak, i would have to run:
> os.system("killall espeak")
> since the subprocess module allows sending SIGKILL to the process, i
> decided to switch to using it. However i cant pass the spaced out
> arguments to it. For example:
> a=subprocess.Popen("soundwrapper espeak -f line.txt")
> results in a OSError, no such file or directory.
> How do i use the Popen function?
> Regards
> Soumen

Read subprocess's documentation. It takes a list, not a string:

>>> a = subprocess.Popen(["soundwrapper", "espeak", "-f", "line.txt"])
-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: OT: Can;'t find a Mozilla user group

2009-06-03 Thread Matt Nordhoff
Anthra Norell wrote:
> I can't run Firefox and Thunderbird without getting these upgrade
> ordering windows. I don't touch them, because I have reason to suspect
> that they are some (Russian) virus that hijacks my traffic. Occasionally
> one of these window pops up the very moment I hit a key and next a
> confirmation message appears "reassuring" me that the upgrade will be
> installed the next time I start. I meant to ask Mozilla about their
> upgrade policy and facilities but for all the googling I do I can't find
> a contact address, nor an active user group. Hints will be greatly
> appreciated. Thanks!
> 
> Frederic

Mozilla software updates automatically by default, so that Joe User
won't run some out-of-date, insecure version.

Mozilla's newsgroups are available on , or you
can subscribe to the mailing list gateways at
.
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python without while and other "explosive" statements

2008-05-11 Thread Matt Nordhoff
ivo talvet wrote:
> Hello,
> 
> Is it possible to have a python which not handle the execution of
> "while", "for", and other loop statements ? I would like to allow
> remote execution of python on a public irc channel, so i'm looking for
> techniques which would do so people won't be able to crash my computer
> (while 1: os.fork(1)), or at least won't won't freeze my python in a
> infinite loop, make it unresponsive. Is there a compiling option (or
> better, something i can get with apt-get cos i think compiling myself
> and handle all the metastuff myself is somehow dirty) for have a
> "secure python" (you can guess what i mean by "secure" in my case) or
> must i touch myself the source disable some code lines ? If last
> solution, which modifications in which files should i do ? (sorry for
> my bad english)
> 
> Thanks.
> 
> Ivo

 is a pastebin-like website that lets people upload
code in a dozen different languages, and it runs it. They have info up
about how it's secure at .

I'm pretty sure there's another similar website that's specific to
Python, but I can't remember it. Anyway, it was similar, using virtual
machines, a firewall, and killing stuff that ran for longer than 20
seconds, IIRC.

(Thanks for the link to codepad, habnabit.)
-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: Get all the instances of one class

2008-05-18 Thread Matt Nordhoff
Tommy Nordgren wrote:
> class MyClass : a_base_class
>   memberlist=[]
> 
> #  Insert object in memberlist when created;
> #  note: objects won't be garbage collected until removed from memberlist.

Just to say, if you wanted to go about it that way, you could avoid the
garbage collection problem by using weakrefs:


-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: module import problem

2008-05-24 Thread Matt Nordhoff
Milos Prudek wrote:
> I have a Kubuntu upgrade script that fails to run:
> 
> File "/tmp/kde-root//DistUpgradeFetcherCore.py", 
> line 34, in 
> import GnuPGInterface
> ImportError
> No module named GnuPGInterface
> 
> I got a folder /usr/share/python-support/python-gnupginterface with 
> a "GnuPGInterface.py" but no __init__.py.
> 
> In python command line, print sys.path shows that /usr/share/python-support/ 
> is not among python paths.
> 
> If I cd into /usr/share/python-support/python-gnupginterface and launch 
> Python 
> I can "import GnuPGInterface". But when I run DistUpgradeFetcherCore.py in 
> that folder it always fails with No module named GnuPGInterface.
> 
>  I do not know much about setting python path. 

This doesn't help solve your problem, but /usr/share/python-support
doesn't need to be in Python's path because python-support puts symlinks
to it in /usr/share/python*/site-packages.
-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: Does this path exist?

2008-05-28 Thread Matt Nordhoff
[EMAIL PROTECTED] wrote:
> I wanted to ask for ways to test whether a path exists. I usually use
> os.path.exists(), which does a stat call on the path and returns True
> if it succeeds, or False if it fails (catches os.error). But stat
> calls don't fail only when a path doesn't exist. I see that, at least
> on Windows, the instance of the exception has an attribute 'errno' set
> to 2 when it fails because the path doesn't exist. Is it a portable
> solution to rely on this (haven't tried it on Linux)? Are there other
> ways of testing whether a path exists?
> 
> Thanks,
> Sebastian

"import errno" and see if the exception's errno attribute is set to
errno.ENOENT (which is, yes, 2). It is portable.

If you Google [ENOENT Windows] or whatever, there are some differences
on different platforms, but not many.
-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python 3000 vs. Python 2.x

2008-06-13 Thread Matt Nordhoff
[EMAIL PROTECTED] wrote:
> As a new comer to Python I was wondering which is the best to start
> learning.  I've read that a number of significant features have
> changed between the two versions.  Yet, the majority of Python
> programs out in the world are 2.x and it would be nice to understand
> those as well.  Thanks for all the help.
> 
> Creosote,

You should learn Python 2. Python 3 is only in alpha/beta, and it won't
be very relevant for several years.

Python 3 isn't a whole new language; it just breaks backwards
compatibility to clean up old warts and make other improvements. It
won't be too hard to transition to it when you decide to.
-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: write Python dict (mb with unicode) to a file

2008-06-14 Thread Matt Nordhoff
dmitrey wrote:
> hi all,
> what's the best way to write Python dictionary to a file?
> 
> (and then read)
> 
> There could be unicode field names and values encountered.
> Thank you in advance, D.

pickle/cPickle, perhaps, if you're willing to trust the file (since it's
basically eval()ed)? Or JSON (use simplejson or the enhanced version of
cjson), though I doubt it would be super-fast.
-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: reading from an a gzip file

2008-06-18 Thread Matt Nordhoff
Nader wrote:
> Hello,
> 
> I have a gzip file and I try to read from this file withe the next
> statements:
> 
>  gunziped_file = gzip.GzipFile('gzip-file')
>  input_file = open(gunziped_file,'r')
> 
> But I get the nezt error message:
> 
> Traceback (most recent call last):
>   File "read_sfloc_files.py", line 131, in ?
> input_file = open(gunziped_file,'r')
> TypeError: coercing to Unicode: need string or buffer, instance found
> 
> I think that I do some mistake. Would some body tell me what is my
> mistake?
> 
> Nader

gzip.GzipFile() creates a file-like object. You don't call open() on it;
you use it directly.

$ echo foo >test
$ gzip test
$ python
>>> import gzip
>>> gfh = gzip.GzipFile('test.gz', 'rb')
>>> gfh.read()
'foo\n'
>>> gfh.close()
-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: python/ruby question..

2008-06-19 Thread Matt Nordhoff
Mensanator wrote:
> On Jun 18, 10:33�pm, "bruce" <[EMAIL PROTECTED]> wrote:
>> hi...
>>
>> can someone point me to where/how i would go about calling a ruby app from a
>> python app, and having the python app being able to get a returned value
>> from the ruby script.
>>
>> something like
>>
>> test.py
>> �a = os.exec(testruby.rb)
>>
>> testruby.py
>> �foo = 9
>> �return foo
>>
>> i know this doesn't work... but i've been searching for hours on this with
>> no luck (and yeah, i'm relatively new to both ruby/python!!)
>>
>> thanks
> 
> Well, I don't know anything about Ruby, but here's
> how I do it for C programs (compiled to .exe that
> write to stdout).
> 
> 
> import os
> factor_program = 'factor! -d200 ' # factor!.exe from MIRACL
> 
> n =
> '50818429800343305993022114330311033271249313957919046352679206262204589342623811236647989889145173098650749'
> 
> # call external program and capture stdout
> the_output = os.popen(factor_program+n).readlines()
> 
> print 'n: %s' % n
> for i in the_output:
> print i,



You're supposed to use the subprocess module.

In this case, something like:

import subprocess
factor_program = ['factor!', '-d200']

...

p = subprocess.Popen(factor_program + [n], stdout=subprocess.PIPE)
p.wait() # wait for it to finish; not sure how necessary it is
the_output = p.stdout.readlines()

See subprocess's documentation [1], which includes guides on replacing
os.popen* and other functions with it.

[1] 
-- 
--
http://mail.python.org/mailman/listinfo/python-list

Re: Simple Class/Variable passing question

2008-06-19 Thread Matt Nordhoff
monkeyboy wrote:
> Hello,
> 
> I'm new to python, and PythonCard. In the code below, I'm trying to
> create a member variable (self.currValue) of the class, then just pass
> it to a simple function (MainOutputRoutine) to increment it. I thought
> Python "passed by reference" all variables, but the member variable
> doesn't seem to be incremented. If I make the function just increment
> the member variable directly, without passing it in, it works fine?
> 
> In the code below, "self.currValue" stays at 5, and value evaluates to
> 1? Any insight would be appreciated...
> 
> 
> class TestModel(model.Background):
> 
> def on_initialize(self,event):
> self.currValue = 5
> 
> def on_startBtn_mouseClick(self, event):
> self.MainOutputRoutine(self.currValue)
> self.OutputRoutine(self.currValue)
> 
> def OutputRoutine(self,value):
> self.components.txtBox1.text = str(value)
> 
> def MainOutputRoutine(self,value):
> value = value + 1

That's not how Python works. When you call
"self.MainOutputRoutine(self.currValue)", in that method's scope, the
local name "value" points to the same object as self.currValue does.
When you do "value = value + 1", the local name "value" now points to a
different object. That has no bearing on self.currValue.

Err, I can't find a guide here. Um, read the language spec? I dunno.

However:

>>> my_list = [1]
>>> def function(l):
... l.append(2)
>>> function(my_list)
>>> my_list
[1, 2]

That's because function() is *mutating* the list; it's not changing what
the "l" name points to. It's calling the "append" method of the list
object, which changes said list object. If it were doing, say, "l = 42",
that would only rebind the function's local name "l":

>>> my_list = [1]
>>> def function(l):
... l = 42
>>> function(my_list)
>>> my_list
[1]

Note that strings and integers are immutable, so whenever you think
you're mutating them (e.g. "s.replace('a', 'b')" or "i += 1"), you're
actually getting a whole new, different object, with all that that implies.
-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: images on the web

2008-06-19 Thread Matt Nordhoff
chris wrote:
> I'm creating a data plot and need to display the image to a web page.
> What's the best way of doing this without having to save the image to
> disk? I already have a mod_python script that outputs the data in
> tabular format, but I haven't been able to find anything on adding a
> generated image.

You could use data: URIs [1].

For example, a 43-byte single pixel GIF becomes this URI:



They don't have universal browser support, but that might not be a
problem in this case.

As for generating them with Python, I'm not sure... I just used Hixie's
data: URI kitchen [2] for the above example.

[1] 
[2] 
-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: images on the web

2008-06-19 Thread Matt Nordhoff
Matt Nordhoff wrote:
> chris wrote:
>> I'm creating a data plot and need to display the image to a web page.
>> What's the best way of doing this without having to save the image to
>> disk? I already have a mod_python script that outputs the data in
>> tabular format, but I haven't been able to find anything on adding a
>> generated image.
> 
> You could use data: URIs [1].
> 
> For example, a 43-byte single pixel GIF becomes this URI:
> 
> 
> 
> They don't have universal browser support, but that might not be a
> problem in this case.
> 
> As for generating them with Python, I'm not sure... I just used Hixie's
> data: URI kitchen [2] for the above example.
> 
> [1] <http://tools.ietf.org/html/rfc2397>
> [2] <http://software.hixie.ch/utilities/cgi/data/data>

Oh.. As <http://bitworking.org/news/Sparklines_in_data_URIs_in_Python>
shows, the reason I couldn't find a data: URI Python library is because
they're utterly trivial to generate:

import base64
import urllib

raw_data = create_gif()
uri = 'data:image/gif;base64,' + urllib.quote(base64.b64encode(raw_data))

(And it's even simpler if you leave out the base64-encoding.)
-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: Weird local variables behaviors

2008-06-20 Thread Matt Nordhoff
Sebastjan Trepca wrote:
> Hey,
> 
> can someone please explain this behavior:
> 
> The code:
> 
> def test1(value=1):
> def inner():
> print value
> inner()
> 
> 
> def test2(value=2):
> def inner():
> value = value
> inner()
> 
> test1()
> test2()
> 
> [EMAIL PROTECTED] ~/dev/tests]$ python locals.py
> 1
> Traceback (most recent call last):
>   File "locals.py", line 13, in 
> test2()
>   File "locals.py", line 10, in test2
> inner()
>   File "locals.py", line 9, in inner
> value = value
> UnboundLocalError: local variable 'value' referenced before assignment
> 
> Why can't he find the variable in the second case?
> 
> 
> Thanks, Sebastjan

Python doesn't like when you read a variable that exists in an outer
scope, then try to assign to it in this scope.

(When you do "a = b", "b" is processed first. In this case, Python
doesn't find a "value" variable in this scope, so it checks the outer
scope, and does find it. But then when it gets to the "a = " part...
well, I don't know, but it doesn't like it.)

In Python 3 (and maybe 2.6), you'll be able to put "nonlocal value" in
inner() to tell it to use the outer scope. (It's similar to the "global"
keyword, but uses the next outer scope as opposed to the global scope.)
-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: Windows OS , Bizarre File Pointer Fact

2008-06-27 Thread Matt Nordhoff
Taygun Kekec wrote:
> Code :
> #!/usr/bin/python
> # -*- coding: utf-8 -*-
> import os
> 
> if os.name == 'nt':
> OS_Selection = 0
> elif os.name == 'posix':
> OS_Selection = 1
> else :
> OS_Selection = 1
> 
> del_cmd_os = ( "del","rm")
> filelist = ("ddd.txt","eee.txt","fff.txt")
> 
> # Creating Files
> for elem in filelist:
> open( elem, 'w').write("Selam")
> 
> #
> # Removing Files
> for elem in filelist:
> fp = open( elem, 'r')
> os.system( "%s %s" % (del_cmd_os[OS_Selection], fp.name))
> fp.close()
> #
> 
> Run this code on Windows XP , it says "the file is being used by
> another process" and fails to delete the file. I tried replacing :
> #
> for elem in filelist:
> open( elem, 'w').write("Selam")
> #
> with :
> #
> for elem in filelist:
> fp = open( elem, 'w')
> fp.write("Selam")
> fp.close()
> #
> 
> in case of any interpreter file pointer destructor failure but it
> didnt change anything.
> Do you have any idea why my files cannot be deleted from my disk with
> 2nd part of my code ?

Why are you executing another program just to delete a file?

>>> import os
>>> os.remove('some/file.txt')
-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: Using just the Mako part of Pylons?

2008-06-30 Thread Matt Nordhoff
John Salerno wrote:
> Bruno Desthuilliers wrote:
>> John Salerno a écrit :
>>> I just installed Pylons onto my hosting server so I could try out
>>> templating with Mako, but it seems a little more complicated than that.
>>
>> Err... Actually, it's certainly a little less complicated than that.
>> First point: Mako is totally independant from Pylons. Second point:
>> you don't need any web server to use Mako - it's just another Python
>> package.
> 
> Well, so far I have installed Pylons onto my web server, but a test Mako
> template isn't rendering, it just prints the text to the browser.
> There's got to be something more to do to get it all hooked up so that
> it works.

What do you mean by "the text"? The source code of the template? A
random insult? The generated page? If it's the last, you forgot to set a
Content-Type header.
-- 
--
http://mail.python.org/mailman/listinfo/python-list

Re: Are the following supported in scipy.sparse

2008-07-01 Thread Matt Nordhoff
dingo_1980 wrote:
> I wanted to know if scipy.sparse support or will support the following
> functions which are available in scipy.linalg or scipy or numpy:
> 
> Inverse
> Cholesky
> SVD
> multiply
> power
> append
> eig
> concatenate
> 
> Thanks...

You should probably ask on a SciPy mailing list.


-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: mirroring files and data via http

2008-07-06 Thread Matt Nordhoff
Steve Potter wrote:
> I'm working on a project to create a central administration interface
> for several websites located on different physical servers.
> 
> You can think of the websites as a blog type application. My
> administration application will be used to create new blog posts with
> associated media (images, etc..)
> 
> So I am thinking to setting up a script on each of the sites to "phone
> home" once per day and download any new posts and media files.
> 
> I am thinking of transmitting the post data in an xml format that
> could then be decoded an recorded into the database on the slave
> site.   Are there any easy ways to encode a python dictionary to and
> from xml?
> 
> For the media files I am thinking that the administration interface
> would also provide an xml file with a list of all of the media files
> required along with an http path to retrieve them and a checksum of
> some sort to verify that they were downloaded correctly.
> 
> Basically I am trying to figure out if anything already exists to
> perform some of these functions or if I have to make them from
> scratch.  I know none of it should be too hard, but I hate to re-
> invent the wheel.
> 
> Thanks,
> 
> Steven Potter

It sounds like JSON would be perfect for this. For working with it in
Python, the simplejson module is popular:


-- 
--
http://mail.python.org/mailman/listinfo/python-list


  1   2   >