Dictionary as Keyword Arguments

2008-12-11 Thread bfrederi
I was wondering if I had a dictionary of keywords and values like so:

keyword_arg_dict = {
'attribute': 'stone',
'contents': 'cave people',
'path': '/path/to/cave',
'name': 'Ogg's Cave',
}

And I had a function that accepted keyword arguments like so:

make_dwelling(
attribute='stone',
contents='cave people',
path='/path/to/cave',
name='Ogg's Cave',
)

Is there any way I could use my keyword_arg_dict as my keyword args
for the make_dwelling function, since I am not the creator of the
make_dwelling function, and need to take that dictionary of key-value
pairs and turn it into keyword-value arguments for the make_dwelling
function?
--
http://mail.python.org/mailman/listinfo/python-list


PIL problem

2008-10-08 Thread bfrederi
I am having a problem using PIL. I am trying to crop and image to a
square, starting from the center of the image, but when I try to crop
the image, it won't crop. Here are the relevant code snippets:

### Function I am testing ###
def create_square_image(file_name):
""" Creates a thumbnail sized image and turns it into a square """
image = Image.open(open(file_name))

size_tuple = image.size
width = size_tuple[0]
height = size_tuple[1]

square_length = 75

x1 = (width / 2) - (square_length / 2)
x2 = x1 + square_length
y1 = (height / 2) - (square_length / 2)
y2 = y1 + square_length

image.crop((x1,y1,x2,y2))
image.save(file_name, "JPEG")


### In my tests.py ###
def testCreateSquareImage(self):
""" Test to turn image into a square """
self.convert_file = '/home/bfrederi/square-dissertation.jpg'
tkl_converter.create_square_image(self.convert_file)
image = Image.open(self.convert_file)
if image.size[0] == 75 and image.size[1] == 75:
self.assert_(True)
else:
self.fail("Width: %s Height: %s" % (image.size[0],
image.size[1]))

### Test result ###
FAIL: Test to turn image into a square
--
Traceback (most recent call last):
  File "tests.py", line 462, in testCreateSquareImage
self.fail("Width: %s Height: %s" % (image.size[0], image.size[1]))
AssertionError: Width: 75 Height: 97

--

The image is unchanged. Anyone have any idea what I'm doing wrong?
I've tried opening the file, and outputting to a different file
(instead of overwriting the file), but the new file always comes out
the same as the original.
--
http://mail.python.org/mailman/listinfo/python-list


Re: PIL problem

2008-10-08 Thread bfrederi
On Oct 8, 10:30 am, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
> On Wed, 08 Oct 2008 08:10:02 -0700, bfrederi wrote:
> > I am having a problem using PIL. I am trying to crop and image to a
> > square, starting from the center of the image, but when I try to crop
> > the image, it won't crop. Here are the relevant code snippets:
>
> > ### Function I am testing ###
> > def create_square_image(file_name):
> >     """ Creates a thumbnail sized image and turns it into a square """
> >     image = Image.open(open(file_name))
>
> >     size_tuple = image.size
> >     width = size_tuple[0]
> >     height = size_tuple[1]
>
> >     square_length = 75
>
> >     x1 = (width / 2) - (square_length / 2) x2 = x1 + square_length
> >     y1 = (height / 2) - (square_length / 2) y2 = y1 + square_length
>
> >     image.crop((x1,y1,x2,y2))
>
> This doesn't change `image` but creates and returns a new cropped image
> which you simply ignore.
>
> >     image.save(file_name, "JPEG")
>
> Ciao,
>         Marc 'BlackJack' Rintsch

How do I output it to an actual file then? Or overwrite the existing
file?
--
http://mail.python.org/mailman/listinfo/python-list


Re: PIL problem

2008-10-08 Thread bfrederi
On Oct 8, 10:39 am, bfrederi <[EMAIL PROTECTED]> wrote:
> On Oct 8, 10:30 am, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
>
>
>
> > On Wed, 08 Oct 2008 08:10:02 -0700, bfrederi wrote:
> > > I am having a problem using PIL. I am trying to crop and image to a
> > > square, starting from the center of the image, but when I try to crop
> > > the image, it won't crop. Here are the relevant code snippets:
>
> > > ### Function I am testing ###
> > > def create_square_image(file_name):
> > >     """ Creates a thumbnail sized image and turns it into a square """
> > >     image = Image.open(open(file_name))
>
> > >     size_tuple = image.size
> > >     width = size_tuple[0]
> > >     height = size_tuple[1]
>
> > >     square_length = 75
>
> > >     x1 = (width / 2) - (square_length / 2) x2 = x1 + square_length
> > >     y1 = (height / 2) - (square_length / 2) y2 = y1 + square_length
>
> > >     image.crop((x1,y1,x2,y2))
>
> > This doesn't change `image` but creates and returns a new cropped image
> > which you simply ignore.
>
> > >     image.save(file_name, "JPEG")
>
> > Ciao,
> >         Marc 'BlackJack' Rintsch
>
> How do I output it to an actual file then? Or overwrite the existing
> file?

Nevermind, I gotcha. I needed to do this:

def create_square_image(file_name):
""" Creates a thumbnail sized image and turns it into a square """
image = Image.open(open(file_name))

size_tuple = image.size
width = size_tuple[0]
height = size_tuple[1]

square_length = 75

x1 = (width / 2) - (square_length / 2)
x2 = x1 + square_length
y1 = (height / 2) - (square_length / 2)
y2 = y1 + square_length

new_image = image.crop((x1,y1,x2,y2))
try:
new_image.save(file_name, "JPEG")
except IOError:
print "Cannot create square image for", file_name

I needed to output the cropped image by getting the cropped image in
"new_image" and outputting the new file. I see what you were saying.
--
http://mail.python.org/mailman/listinfo/python-list


Re: PIL problem

2008-10-08 Thread bfrederi
On Oct 8, 10:39 am, bfrederi <[EMAIL PROTECTED]> wrote:
> On Oct 8, 10:30 am, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
>
>
>
> > On Wed, 08 Oct 2008 08:10:02 -0700, bfrederi wrote:
> > > I am having a problem using PIL. I am trying to crop and image to a
> > > square, starting from the center of the image, but when I try to crop
> > > the image, it won't crop. Here are the relevant code snippets:
>
> > > ### Function I am testing ###
> > > def create_square_image(file_name):
> > >     """ Creates a thumbnail sized image and turns it into a square """
> > >     image = Image.open(open(file_name))
>
> > >     size_tuple = image.size
> > >     width = size_tuple[0]
> > >     height = size_tuple[1]
>
> > >     square_length = 75
>
> > >     x1 = (width / 2) - (square_length / 2) x2 = x1 + square_length
> > >     y1 = (height / 2) - (square_length / 2) y2 = y1 + square_length
>
> > >     image.crop((x1,y1,x2,y2))
>
> > This doesn't change `image` but creates and returns a new cropped image
> > which you simply ignore.
>
> > >     image.save(file_name, "JPEG")
>
> > Ciao,
> >         Marc 'BlackJack' Rintsch
>
> How do I output it to an actual file then? Or overwrite the existing
> file?

Nevermind, I gotcha. I needed to do this:

def create_square_image(file_name):
""" Creates a thumbnail sized image and turns it into a square """
image = Image.open(open(file_name))

size_tuple = image.size
width = size_tuple[0]
height = size_tuple[1]

square_length = 75

x1 = (width / 2) - (square_length / 2)
x2 = x1 + square_length
y1 = (height / 2) - (square_length / 2)
y2 = y1 + square_length

new_image = image.crop((x1,y1,x2,y2))
try:
new_image.save(file_name, "JPEG")
except IOError:
print "Cannot create square image for", file_name

I needed to output the cropped image by getting the cropped image in
"new_image" and outputting the new file. I see what you were saying.
--
http://mail.python.org/mailman/listinfo/python-list


datetime 'NoneType' sporadic error

2009-12-11 Thread bfrederi
When using the datetime module, I sporadically get " 'NoneType' object
has no attribute 'datetime' " on line 40: http://dpaste.com/hold/132156/

Sorry, I don't have the traceback, this is an error that was sent to
me by one of the users. It only happens occasionally.

Any ideas?
-- 
http://mail.python.org/mailman/listinfo/python-list


urllib2: post request to textarea

2010-04-09 Thread bfrederi
I'm getting a 500 error when attempting to make a post request with
urllib2 to a form with a  tag. When I create the post
request with all the other post data, I don't get a 500. Just the part
of the form that is a . Is this just a coincidence, and my
real problem is something else?

Thanks,
Brandon
-- 
http://mail.python.org/mailman/listinfo/python-list


Issue with xml iterparse

2010-06-03 Thread bfrederi
I am using lxml iterparse and running into a very obscure error. When
I run iterparse on a file, it will occasionally return an element that
has a element.text == None when the element clearly has text in it.

I copy and pasted the problem xml into a python string, used StringIO
to create a file-like object out of it, and ran a test using iterparse
with expected output, and it ran perfectly fine. So it only happens
when I try to run iterparse on the actual file.

So then I tried opening the file, reading the data, turning that data
into a file-like object using StringIO, then running iterparse on it,
and the same problem (element.text == None) occurred.

I even tried this:
f = codecs.open(abbyy_filename, 'r', encoding='utf-8')
file_data = f.read()
file_like_object = StringIO.StringIO(file_data)
for event, element in iterparse(file_like_object, events=("start",
"end")):

And I got this Traceback:
Traceback (most recent call last):
  File "abbyyParser/parseAbbyy.py", line 391, in 
extension=options.extension,
  File "abbyyParser/parseAbbyy.py", line 103, in __init__
self.generate_output_files()
  File "abbyyParser/parseAbbyy.py", line 164, in generate_output_files
AbbyyDocParse(abby_filename, self.extension, self.output_types)
  File "abbyyParser/parseAbbyy.py", line 239, in __init__
self.parse_doc(abbyy_filename)
  File "abbyyParser/parseAbbyy.py", line 281, in parse_doc
for event, element in iterparse(file_like_object, events=("start",
"end")):
  File "iterparse.pxi", line 484, in lxml.etree.iterparse.__next__
(src/lxml/lxml.etree.c:86333)
TypeError: reading file objects must return plain strings

If I do this:
file_data = f.read().encode("utf-8")

iterparse will run on it, but I still get elements.text with a value
of None when I should not.

My XML file does have diacritics in it, but I've put the proper
encoding at the head of the XML file (). I've also tried using elementree's iterparse, and
I get even more of the same problem with the same files. Any idea what
the problem might be?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Issue with xml iterparse

2010-06-03 Thread bfrederi
On Jun 3, 3:59 pm, Chris Rebert  wrote:
> On Thu, Jun 3, 2010 at 1:44 PM, bfrederi  wrote:
> > I am using lxml iterparse and running into a very obscure error. When
> > I run iterparse on a file, it will occasionally return an element that
> > has a element.text == None when the element clearly has text in it.
>
> > I copy and pasted the problem xml into a python string, used StringIO
> > to create a file-like object out of it, and ran a test using iterparse
> > with expected output, and it ran perfectly fine. So it only happens
> > when I try to run iterparse on the actual file.
>
> > So then I tried opening the file, reading the data, turning that data
> > into a file-like object using StringIO, then running iterparse on it,
> > and the same problem (element.text == None) occurred.
>
> > I even tried this:
> > f = codecs.open(abbyy_filename, 'r', encoding='utf-8')
> > file_data = f.read()
> > file_like_object = StringIO.StringIO(file_data)
> > for event, element in iterparse(file_like_object, events=("start",
> > "end")):
>
> IIRC, XML parsers operate on bytes directly (since they have to
> determine the encoding themselves anyway), not pre-decoded Unicode
> characters, so I think your manual UTF-8 decoding could be the
> problem.
> Have you tried simply:
>
> f = open(abbyy_filename, 'r')
> for event, element in iterparse(f, events=("start", "end")):
>     #whatever
>
> ?
>
> Apologies if you already have, but since you didn't include the
> original, albeit probably trivial, error-causing code, this relatively
> simple error couldn't be ruled out.
>
> Cheers,
> Chris
> --http://blog.rebertia.com

Sorry for not mentioning it, but I tried that as well and it failed.
Here is the relevant class. AbbyyLine and Abbyyword just take the
element's text and writes it to a file/file-like object. parse_doc is
where I use iterparse. The relevant part is very minimal and there is
a lot of fluff to ignore, so I didn't initially post it:

class AbbyyDocParse(object):

"""Takes an abbyy filename and parses the contents"""
def __init__(self, abbyy_filename, extension=DEFAULT_ABBYY_EXT,
format_list=OUTPUT_TYPES, string_only=False):
self.extension = extension
self.format_list = format_list
#Create the file handles for the output files
self.create_filehandles(abbyy_filename, string_only)
#Parse the document
self.parse_doc(abbyy_filename)
#Close the output filehandles
self.close_filehandles(abbyy_filename, string_only)

def create_filehandles(self, abbyy_filename, string_only):
"""Create output filehandles"""
#if output goes to a file
if not string_only:
#Make sure the file is an abbyy file
if not abbyy_filename.endswith(self.extension):
raise ParserException, "Bad abbyy filename given: %s"
\
% (abbyy_filename)
#get the base path and filename for output files
filename = abbyy_filename.replace(self.extension, '')
#Loop through the different formats
for format_type in self.format_list:
#if output goes to a file
if not string_only:
#Create output filename
out_file = "%s%s" % (filename,
OUTPUT_EXTENSIONS.get(format_type))
#Opens the format type filehandle
try:
setattr(self, "%s_handle" % (format_type),
open(out_file,'w'))
except:
raise IOError, "Could not open file: %s" %
(out_file)
#if output goes to a string
else:
#Opens the format type StringIO
try:
setattr(self, "%s_handle" % (format_type),
StringIO.StringIO())
except:
raise IOError, "Could not open string output: %s"
% (out_file)

def parse_doc(self, abbyy_filename):
"""Parses the abbyy document"""
#Write the first line of the xml doc, if specified
if getattr(self, 'xml_handle', None):
self.xml_handle.write('\n')
#Memory efficient iterparse opens file and loops through
content
for event, element in iterparse(abbyy_filename,
events=("start", "end")):
#ignore the namespace, if it has one
if NAMESPACE_REGEX.search(element.tag, 0):
element_tag = NAMESPACE_REGEX.search(element.tag,
0).group(1)
else:
element_tag = element.tag
#if 

Re: Issue with xml iterparse

2010-06-04 Thread bfrederi
On Jun 3, 4:13 pm, bfrederi  wrote:
> On Jun 3, 3:59 pm, Chris Rebert  wrote:
>
>
>
> > On Thu, Jun 3, 2010 at 1:44 PM, bfrederi  wrote:
> > > I am using lxml iterparse and running into a very obscure error. When
> > > I run iterparse on a file, it will occasionally return an element that
> > > has a element.text == None when the element clearly has text in it.
>
> > > I copy and pasted the problem xml into a python string, used StringIO
> > > to create a file-like object out of it, and ran a test using iterparse
> > > with expected output, and it ran perfectly fine. So it only happens
> > > when I try to run iterparse on the actual file.
>
> > > So then I tried opening the file, reading the data, turning that data
> > > into a file-like object using StringIO, then running iterparse on it,
> > > and the same problem (element.text == None) occurred.
>
> > > I even tried this:
> > > f = codecs.open(abbyy_filename, 'r', encoding='utf-8')
> > > file_data = f.read()
> > > file_like_object = StringIO.StringIO(file_data)
> > > for event, element in iterparse(file_like_object, events=("start",
> > > "end")):
>
> > IIRC, XML parsers operate on bytes directly (since they have to
> > determine the encoding themselves anyway), not pre-decoded Unicode
> > characters, so I think your manual UTF-8 decoding could be the
> > problem.
> > Have you tried simply:
>
> > f = open(abbyy_filename, 'r')
> > for event, element in iterparse(f, events=("start", "end")):
> >     #whatever
>
> > ?
>
> > Apologies if you already have, but since you didn't include the
> > original, albeit probably trivial, error-causing code, this relatively
> > simple error couldn't be ruled out.
>
> > Cheers,
> > Chris
> > --http://blog.rebertia.com
>
> Sorry for not mentioning it, but I tried that as well and it failed.
> Here is the relevant class. AbbyyLine and Abbyyword just take the
> element's text and writes it to a file/file-like object. parse_doc is
> where I use iterparse. The relevant part is very minimal and there is
> a lot of fluff to ignore, so I didn't initially post it:
>
> class AbbyyDocParse(object):
>
>     """Takes an abbyy filename and parses the contents"""
>     def __init__(self, abbyy_filename, extension=DEFAULT_ABBYY_EXT,
>         format_list=OUTPUT_TYPES, string_only=False):
>         self.extension = extension
>         self.format_list = format_list
>         #Create the file handles for the output files
>         self.create_filehandles(abbyy_filename, string_only)
>         #Parse the document
>         self.parse_doc(abbyy_filename)
>         #Close the output filehandles
>         self.close_filehandles(abbyy_filename, string_only)
>
>     def create_filehandles(self, abbyy_filename, string_only):
>         """Create output filehandles"""
>         #if output goes to a file
>         if not string_only:
>             #Make sure the file is an abbyy file
>             if not abbyy_filename.endswith(self.extension):
>                 raise ParserException, "Bad abbyy filename given: %s"
> \
>                     % (abbyy_filename)
>             #get the base path and filename for output files
>             filename = abbyy_filename.replace(self.extension, '')
>         #Loop through the different formats
>         for format_type in self.format_list:
>             #if output goes to a file
>             if not string_only:
>                 #Create output filename
>                 out_file = "%s%s" % (filename,
> OUTPUT_EXTENSIONS.get(format_type))
>                 #Opens the format type filehandle
>                 try:
>                     setattr(self, "%s_handle" % (format_type),
> open(out_file,'w'))
>                 except:
>                     raise IOError, "Could not open file: %s" %
> (out_file)
>             #if output goes to a string
>             else:
>                 #Opens the format type StringIO
>                 try:
>                     setattr(self, "%s_handle" % (format_type),
> StringIO.StringIO())
>                 except:
>                     raise IOError, "Could not open string output: %s"
> % (out_file)
>
>     def parse_doc(self, abbyy_filename):
>         """Parses the abbyy document"""
>         #Write the first line of the xml doc, if specified
>         if getattr(self, &#

Re: base64 Incorrect Padding

2009-07-31 Thread bfrederi
So what if I used a different encoding that isn't ASCII? Like UTF-8?
Would that give me lengths that are multiples of 4 based on how the
characters are represented? Or would I still need to pad with '='?
-- 
http://mail.python.org/mailman/listinfo/python-list