comp.lang.python - 25 new messages in 14 topics - digest
comp.lang.python
http://groups.google.com/group/comp.lang.python?hl=en
comp.lang.python@googlegroups.com
Today's topics:
* subtraction is giving me a syntax error - 6 messages, 5 authors
http://groups.google.com/group/comp.lang.python/t/f8017a980f728348?hl=en
* staticmethod and setattr - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/d0feee9047232812?hl=en
* class inheritance - 3 messages, 3 authors
http://groups.google.com/group/comp.lang.python/t/2512697a901d4752?hl=en
* "Breaking" the __main__ script - 3 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/abf94919b968b79f?hl=en
* Python, Reportlabs, Pil and Windows 7 (64bit) - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/8f0199cb036d7949?hl=en
* OpenOpt 0.28, FuncDesigner 0.18, DerApproximator 0.18 - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/cf61df9bd6674285?hl=en
* Clustering and automated configuration & deployment toolkit with Python - 1
messages, 1 author
http://groups.google.com/group/comp.lang.python/t/6cbd490bfd2eb39a?hl=en
* When to lock data items returned by multiprocessing.Manager? - 1 messages, 1
author
http://groups.google.com/group/comp.lang.python/t/44fb961ecb9e4022?hl=en
* Reverse engineering CRC? - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/b22db1e3e63db596?hl=en
* logging: local functions ==> loss of lineno - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/1282b9ec08adc2a6?hl=en
* Binary data transfer issue - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/94448ce25458f30f?hl=en
* Build Python with XCode - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/c623db340ebcef05?hl=en
* converting datetime with tzinfo to unix timestamp - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/1b56db853fe6407f?hl=en
* result of os.times() is different with 'time' command Options - 1 messages,
1 author
http://groups.google.com/group/comp.lang.python/t/ab45c1ed3e45401c?hl=en
==============================================================================
TOPIC: subtraction is giving me a syntax error
http://groups.google.com/group/comp.lang.python/t/f8017a980f728348?hl=en
==============================================================================
== 1 of 6 ==
Date: Mon, Mar 15 2010 11:00 am
From: MRAB
Joel Pendery wrote:
> So I am trying to write a bit of code and a simple numerical
> subtraction
>
> y_diff = y_diff-H
>
> is giving me the error
>
> Syntaxerror: Non-ASCII character '\x96' in file on line 70, but no
> encoding declared.
>
> Even though I have deleted some lines before it and this line is no
> longer line 70, I am still getting the error every time. I have tried
> to change the encoding of the file to utf-8 but to no avail, I still
> am having this issue. Any ideas?
>
> Thanks in advance
Character '\x96' is an en-dash, which looks like a hyphen/minus sign
'-', but isn't.
== 2 of 6 ==
Date: Mon, Mar 15 2010 11:09 am
From: Grant Edwards
On 2010-03-15, Philip Semanchuk <philip@semanchuk.com> wrote:
> On Mar 15, 2010, at 1:37 PM, Joel Pendery wrote:
>
>> So I am trying to write a bit of code and a simple numerical
>> subtraction
>>
>> y_diff = y_diff-H
>>
>> is giving me the error
>>
>> Syntaxerror: Non-ASCII character '\x96' in file on line 70, but no
>> encoding declared.
[...]
>
> 0x96 is the Win1252 minus sign character. Did you copy & paste this
> code from a Web browser or a word processor?
>
> Delete the character between "y_diff" and "H" and replace it with a
> plain ASCII subtraction sign.
I think somebody needs to stop editing his code with MS Word and start
using a programming editor. ;)
--
Grant Edwards grant.b.edwards Yow! Make me look like
at LINDA RONSTADT again!!
gmail.com
== 3 of 6 ==
Date: Mon, Mar 15 2010 11:43 am
From: Baptiste Carvello
Joel Pendery a écrit :
> So I am trying to write a bit of code and a simple numerical
> subtraction
>
> y_diff = y_diff-H
>
> is giving me the error
>
> Syntaxerror: Non-ASCII character '\x96' in file on line 70, but no
> encoding declared.
>
> Even though I have deleted some lines before it and this line is no
> longer line 70, I am still getting the error every time. I have tried
> to change the encoding of the file to utf-8 but to no avail, I still
> am having this issue. Any ideas?
>
> Thanks in advance
Hello,
I would say that when you press the minus key, your operating system doesn't
encode the standard (ASCII) minus character, but some fancy character, which
Python cannot interpret.
More precisely, I suspect you are unsing Windows with codepage 1252 (latin 1).
With this encoding, you have 2 kinds of minus signs: the standard (45th
character, in hex '\x2d') and the non-standard (150th character, in hex '\x96').
cf:
http://msdn.microsoft.com/en-us/library/cc195054.aspx
Cheers,
Baptiste
== 4 of 6 ==
Date: Mon, Mar 15 2010 3:33 pm
From: Steven D'Aprano
On Mon, 15 Mar 2010 18:09:29 +0000, Grant Edwards wrote:
>> Delete the character between "y_diff" and "H" and replace it with a
>> plain ASCII subtraction sign.
>
> I think somebody needs to stop editing his code with MS Word and start
> using a programming editor. ;)
I've had this error myself, and I've never used Word to edit code. It can
happen if you copy code from a website that "helpfully" converts hyphens
to en-dashes, spaces to non-breaking spaces, or inserts ctrl-Z characters
into strings, etc. They're a devil to debug.
--
Steven
== 5 of 6 ==
Date: Mon, Mar 15 2010 3:48 pm
From: John Machin
On Mar 16, 5:43 am, Baptiste Carvello <baptiste...@free.fr> wrote:
> Joel Pendery a écrit :
> > So I am trying to write a bit of code and a simple numerical
> > subtraction
>
> > y_diff = y_diff-H
>
> > is giving me the error
>
> > Syntaxerror: Non-ASCII character '\x96' in file on line 70, but no
> > encoding declared.
>
>
> I would say that when you press the minus key, your operating system doesn't
> encode the standard (ASCII) minus character, but some fancy character, which
> Python cannot interpret.
The likelihood that any operating system however brain-damaged and in
whatever locale would provide by default a "keyboard" or "input
method" that generated EN DASH when the '-' key is struck is somewhere
between zero and epsilon.
Already advanced theories like "used a word processor instead of a
programmer's editor" and "scraped it off the web" are much more
plausible.
> More precisely, I suspect you are unsing Windows with codepage 1252 (latin 1).
Codepage 1252 is not "latin1" in the generally accepted meaning of
"latin1" i.e. ISO-8859-1. It is a superset. MS in their wisdom or
otherwise chose to use most of the otherwise absolutely wasted slots
assigned to "C1 control characters" in latin1.
> With this encoding, you have 2 kinds of minus signs: the standard (45th
> character, in hex '\x2d') and the non-standard (150th character, in hex '\x96').
>
> cf:http://msdn.microsoft.com/en-us/library/cc195054.aspx
The above link quite correctly says that '\x96` maps to U+2013 EN
DASH. EN DASH is not any kind of minus sign.
Aside: the syndrome causing the problem is apparent with cp125x for x
in range(9)
== 6 of 6 ==
Date: Mon, Mar 15 2010 3:56 pm
From: Grant Edwards
On 2010-03-15, Steven D'Aprano <steve@REMOVE-THIS-cybersource.com.au> wrote:
> On Mon, 15 Mar 2010 18:09:29 +0000, Grant Edwards wrote:
>
>>> Delete the character between "y_diff" and "H" and replace it with a
>>> plain ASCII subtraction sign.
>>
>> I think somebody needs to stop editing his code with MS Word and start
>> using a programming editor. ;)
>
> I've had this error myself, and I've never used Word to edit code. It can
> happen if you copy code from a website that "helpfully" converts hyphens
> to en-dashes, spaces to non-breaking spaces, or inserts ctrl-Z characters
> into strings, etc. They're a devil to debug.
Though it may not be Microsoft Word, I think I'd still maintain that
an editor where you can't see a ctrl-Z or tell the difference between
an ASCII minus and a windows-codepage-whatever isn't a very good
programmer's editor.
--
Grant Edwards grant.b.edwards Yow! HUMAN REPLICAS are
at inserted into VATS of
gmail.com NUTRITIONAL YEAST ...
==============================================================================
TOPIC: staticmethod and setattr
http://groups.google.com/group/comp.lang.python/t/d0feee9047232812?hl=en
==============================================================================
== 1 of 1 ==
Date: Mon, Mar 15 2010 11:25 am
From: Jean-Michel Pichavant
Am Montag, den 15.03.2010, 05:42 -0700 schrieb Michael.Lausch:
>> On Mar 15, 11:40 am, Steven D'Aprano <st...@REMOVE-THIS-
>> cybersource.com.au> wrote:
>>
>>> On Mon, 15 Mar 2010 01:43:02 -0700, Michael.Lausch wrote:
>>>
>>>> Hi,
>>>>
>>>> I managed to get confused by Python, which is not such an easy task.
>>>>
>>>> The problem i have is rooted in marshalling, JSON and Dojo. I need some
>>>> static class in function with the name "$ref" i tried:
>>>> class Foo(object):
>>>> @staticmethod
>>>> def _ref(o):
>>>> pass
>>>>
>>>> setattr(Foo, "$ref", Foo._ref)
>>>>
>>> That doesn't work as expected:
>>>
>>>
>>>>>> Foo.__dict__['_ref'] is Foo.__dict__['$ref']
>>>>>>
>>> False
>>>
>>> Try this instead:
>>>
>>>
>>>>>> setattr(Foo, "$ref", Foo.__dict__['_ref'])
>>>>>> Foo.__dict__['_ref'] is Foo.__dict__['$ref']
>>>>>>
>>> True
>>>
>> Now I'm trying to understand why this is the case.
>> How is Foo.__dict__['_ref'] different from Foo._ref?
>> Shouldn't it return the same attribute?
>>
>> And after further experiments i found out that a making
>> Foo._ref a classmethod does work with my first approach.
>>
>>
When you declared _ref as static, a static object has been stored in Foo.
Using Foo.__dict__ you can access this static object, which is *not* the
_ref function
Using Foo._ref, you trigger the lookup mechanism which do much more than
accessing the dict. Especially, if it finds a __get__ method in the
object, it will return the __get__ result instead of the object itself.
Foo._ref is equivalent in your case to
Foo.__dict__['_ref'].__get__(None, Foo)
JM
==============================================================================
TOPIC: class inheritance
http://groups.google.com/group/comp.lang.python/t/2512697a901d4752?hl=en
==============================================================================
== 1 of 3 ==
Date: Mon, Mar 15 2010 11:24 am
From: Jean-Michel Pichavant
JLundell wrote:
> I've got a subclass of fractions.Fraction called Value; it's a mostly
> trivial class, except that it overrides __eq__ to mean 'nearly equal'.
> However, since Fraction's operations result in a Fraction, not a
> Value, I end up with stuff like this:
>
> x = Value(1) + Value(2)
>
> where x is now a Fraction, not a Value, and x == y uses
> Fraction.__eq__ rather than Value.__eq__.
>
> This appears to be standard Python behavior (int does the same thing).
> I've worked around it by overriding __add__, etc, with functions that
> invoke Fraction but coerce the result. But that's tedious; there are a
> lot of methods to override.
>
> So I'm wondering: is there a more efficient way to accomplish what I'm
> after?
>
I would change the approach.
To the question, "*is* a Value a Fraction", some may tempted to answer
"No" (for instance, a fraction has a denominator, a value has not).
However "Does a Fraction *have* a Value", you could possibly say "Yes".
The value may be then an attribute of the class Fraction, not a subclass.
To test fraction equality, use F1 == F2. In order to test 'nearly
equality', use F1.val() == F2.val().
JM
== 2 of 3 ==
Date: Mon, Mar 15 2010 4:34 pm
From: JLundell
On Mar 13, 1:26 pm, Carl Banks <pavlovevide...@gmail.com> wrote:
> It's a tad unfortunately Python doesn't make this easier. If I had to
> do it more than once I'd probably write a mixin to do it:
>
> class ArithmeticSelfCastMixin(object):
> def __add__(self,other):
> return
> self.__class__(super(ArithmeticSelfCastMixin,self).__add__(other)
> # etc.
>
> class Value(ArithmeticSelfCastMixin,fraction.Fraction):
> pass
>
> However, I want to warn you about overriding __eq__ to mean "almost
> equal": it can have unexpected results so I don't recommend it. Two
> of the main issues with it are:
>
> 1. It violates the transitive property ("If A == B and B == C, then A
> == C") which most programmers expect to be true.
>
> 2. It will give unpredictable results when the objects are used in
> sets or as dictionary keys. Those thow types expect the transitive
> property to be true. If you are going to redefine __eq__ to mean
> "almost equal", then at least define __hash__ to raise
> NotImplementedError so that Python will refuse to use them in sets or
> as dictionary keys:
>
> def __hash__(self): raise NotImplementedError
>
> Carl Banks
It's also unfortunate that Python doesn't have an approximately-equal
operator; it'd come in handy for floating-point applications while
preserving hash. If only there were a ~= or ≈ operator I could
overload. And ~ is unary, so no joy.
My application is in that sense a little like a floating-point app, in
that it needs approximately-equal. And it doesn't need Value to be
hashable; thanks for the NotImplementedError suggestion; I've done
that as a safeguard.
== 3 of 3 ==
Date: Mon, Mar 15 2010 6:03 pm
From: Carl Banks
On Mar 15, 4:34 pm, JLundell <jlund...@pobox.com> wrote:
> It's also unfortunate that Python doesn't have an approximately-equal
> operator; it'd come in handy for floating-point applications while
> preserving hash. If only there were a ~= or ≈ operator I could
> overload. And ~ is unary, so no joy.
One problem with it is that there's no way to make it universal;
different appiplications have different ideas of close. Conceivably
it could be usefully defined for a user type though..
Bacause of this problem almost no languages have an almost equal
operator. I'm curious what languages do, of if there are any with a
trinary operator that also takes a threshold.
Carl Banks
==============================================================================
TOPIC: "Breaking" the __main__ script
http://groups.google.com/group/comp.lang.python/t/abf94919b968b79f?hl=en
==============================================================================
== 1 of 3 ==
Date: Mon, Mar 15 2010 11:39 am
From: Jean-Michel Pichavant
Steve Holden wrote:
> python@bdurham.com wrote:
>
>>>> Any reason you prefer PDB over WinPDB?
>>>> http://winpdb.org/
>>>>
>>> Yes. I don't have Windows except one one PC :P
>>>
>> WinPDB runs on non-Windows platforms :)
>>
>>
> One might reasonably argue that it has a pretty couter-intuitive name, then.
>
> regards
> Steve
>
'Win' may stand for Winner, not Windows :D
JM
== 2 of 3 ==
Date: Mon, Mar 15 2010 11:41 am
From: Jean-Michel Pichavant
vsoler wrote:
> Hello,
>
> I am still learning python, thus developnig small scripts.
>
> Some of them consist only of the main module. While testing them
> (debugging) I sometimes want to stop the script at a certain point,
> with something like stop, break, end or something similar.
>
> What statement can I use?
>
> Vicente Soler
>
import bdb
pdb.set_trace() # put this line anywhere you want a breakpoint in your code.
type n for next, c for continue, s for step into and google for 'python
pdb' for the details.
JM
== 3 of 3 ==
Date: Mon, Mar 15 2010 11:50 am
From: Jean-Michel Pichavant
Jean-Michel Pichavant wrote:
> vsoler wrote:
>> Hello,
>>
>> I am still learning python, thus developnig small scripts.
>>
>> Some of them consist only of the main module. While testing them
>> (debugging) I sometimes want to stop the script at a certain point,
>> with something like stop, break, end or something similar.
>>
>> What statement can I use?
>>
>> Vicente Soler
>>
> import bdb
>
> pdb.set_trace() # put this line anywhere you want a breakpoint in your
> code.
>
> type n for next, c for continue, s for step into and google for
> 'python pdb' for the details.
>
> JM
erratum
import pdb
==============================================================================
TOPIC: Python, Reportlabs, Pil and Windows 7 (64bit)
http://groups.google.com/group/comp.lang.python/t/8f0199cb036d7949?hl=en
==============================================================================
== 1 of 1 ==
Date: Mon, Mar 15 2010 12:10 pm
From: Astley Le Jasper
Hi Robin,
It looks like you've been busy. I'm sorry but you are well over my
head at the moment!
:-)
If you need me to test an install then I'd be happy to help. However,
I just received an email from Christoph Gohlke saying:
" ... There are 64 bit versions of Reportlab and PIL for Python 2.6
for Windows available at http://www.lfd.uci.edu/~gohlke/pythonlibs/
..."
Regards
Neil
==============================================================================
TOPIC: OpenOpt 0.28, FuncDesigner 0.18, DerApproximator 0.18
http://groups.google.com/group/comp.lang.python/t/cf61df9bd6674285?hl=en
==============================================================================
== 1 of 1 ==
Date: Mon, Mar 15 2010 12:13 pm
From: dmitrey
Hi all,
I'm glad to inform you about new release of our free (BSD-licensed)
soft OpenOpt 0.28 (numerical optimization), FuncDesigner 0.18 (CAS
with automatic differentiation), DerApproximator 0.18 (finite-
differeces derivatives approximation).
More details here:
http://forum.openopt.org/viewtopic.php?id=222
Regards, D.
==============================================================================
TOPIC: Clustering and automated configuration & deployment toolkit with Python
http://groups.google.com/group/comp.lang.python/t/6cbd490bfd2eb39a?hl=en
==============================================================================
== 1 of 1 ==
Date: Mon, Mar 15 2010 12:38 pm
From: Philip Semanchuk
On Mar 13, 2010, at 6:21 PM, np map wrote:
> I'd like to write an open source clustering (for computation and
> general use) and automation of configuration/deployment in Python.
> It's main purpose is to be used in academic environments.
> It would be something like running numpy/simpy code (and other custom
> python code) on a set of machines in a distributed fashion (e.g.
> splitting tasks, doing certain bits on some machines, other sub-tasks
> on other machines, etc).
Hi np map,
Are you aware of ipython? My colleagues tell me that it contains some
code for executing processes on remote machines. It sounds like code
which you could build on or borrow from.
HTH
Philip
>
> The cluster could be used in at least two ways:
> - submit code/files via a web interface, monitor the task via the web
> interface and download the results from the master node (user<>web
> interface<>master)
> - run code directly from another machine on the cluster (as if it were
> a subprocess or something like this)
>
>
> Requirements (so far):
> - support the Ubuntu Linux distribution in the initial iteration
> - be easy to extend to other OS-es and package managers
> - try to be 3.x compatible where dual compatibility is possible (2.x
> and 3.x)
> - it will support Python 2.5-2.6
> - document required changes to the 2.x only code to make it work on
> 3.x
> - make it easy to submit code directly from python scripts to the
> cluster (with the right credentials)
> - support key based authentication for job submission
> - should talk to at least one type of RDBMS to store various types of
> data
> - the cluster should be able to kill a task on nodes automatically if
> it executes for too long or requires too much memory (configurable)
> - should be modular (use automation & configuration or just
> clustering)
>
>
> Therefore, I'd like to know a few things:
>
> Is there a clustering toolkit already available for python?
>
> What would the recommended architecture be ?
>
> How should the "user" code interface with the clustering system's
> code?
>
> How should the results be stored (at the node and master level)?
>
> Should threading be supported in the tasks?
>
> How should they be returned to the Master node(s)? (polling, submitted
> by the nodes, etc)
>
> What libraries should be used for this? (e.g. fabric as a library,
> pyro, etc)
>
> Any other suggestions and pieces of advice?
>
> Should Fabric be used in this clustering system for automation? If
> not, what else? Would simply using a wrapper written in python for the
> 'ssh' app be ok?
>
> Would the following architecture be ok?
> Master: splits tasks into sub-tasks, sends them to nodes - provided
> the node's load isn't greater than a certain percentage, gets results,
> stores and provides configuration to nodes, stores results, etc
> Node: runs code, applies configuration, submits the results to the
> master, etc
>
> If this system actually gets python-level code submission inside, how
> should it work?
>
> The reason I posted this set of questions and ideas is that I'd like
> this to be as flexible and usable as possible.
==============================================================================
TOPIC: When to lock data items returned by multiprocessing.Manager?
http://groups.google.com/group/comp.lang.python/t/44fb961ecb9e4022?hl=en
==============================================================================
== 1 of 1 ==
Date: Mon, Mar 15 2010 12:57 pm
From: aahz@pythoncraft.com (Aahz)
In article <4428d674-7fa7-4095-a93d-75ea31a81d9a@15g2000yqi.googlegroups.com>,
Veloz <michaelveloz@gmail.com> wrote:
>
>So I'm using a multiprocessing.Manager instance in my main app and
>asking it to create a dictionary, which I am providing to instances of
>the application that I'm forking off with Process.
>
>The Processes and main app will be reading/writing to the dictionary.
>
>It's not clear to me what I have to "lock" and what I don't have to
>lock. Any ideas?
The way I deal with it is by making sure each key is only updated by a
single process.
--
Aahz (aahz@pythoncraft.com) <*> http://www.pythoncraft.com/
"Many customs in this life persist because they ease friction and promote
productivity as a result of universal agreement, and whether they are
precisely the optimal choices is much less important." --Henry Spencer
==============================================================================
TOPIC: Reverse engineering CRC?
http://groups.google.com/group/comp.lang.python/t/b22db1e3e63db596?hl=en
==============================================================================
== 1 of 2 ==
Date: Mon, Mar 15 2010 1:52 pm
From: jkn
Hi Greg
Just to say thanks for taking the time to write up your work on
this interesting topic.
Cheers
J^n
== 2 of 2 ==
Date: Mon, Mar 15 2010 1:57 pm
From: geremy condra
On Mon, Mar 15, 2010 at 6:29 AM, Gregory Ewing
<greg.ewing@canterbury.ac.nz> wrote:
> I've solved the problem now.
>
> It turned out to be a very standard CRC algorithm, complicated
> by the presence of a few extra bytes of data being checked that
> didn't appear explicitly in the file anywhere.
>
> In the process I developed some very general techniques for
> solving this kind of problem, which I've written about here
> if anyone's interested:
>
> http://www.cosc.canterbury.ac.nz/greg.ewing/essays/CRC-Reverse-Engineering.html
>
> Thanks for everyone's help,
> Greg
Nice writeup, thanks.
Geremy Condra
==============================================================================
TOPIC: logging: local functions ==> loss of lineno
http://groups.google.com/group/comp.lang.python/t/1282b9ec08adc2a6?hl=en
==============================================================================
== 1 of 1 ==
Date: Mon, Mar 15 2010 4:36 pm
From: Hellmut Weber
Am 11.03.2010 12:14, schrieb Peter Otten:
> Hellmut Weber wrote:
>
>> Logging works very well giving the filename and line number of the point
>> where it is called. As long as I use the loggers directly.
>> BUT when I have to wrap the logger call in some other function, I always
>> get file name and line number of the call of the logger inside the
>> wrapping function.
>>
>> Is there a possibility to get this information in this situation too?
>
> The official way is probably to write a custom Logger class that overrides
> the findCaller() method.
>
> Below is a hack that monkey-patches the logging._srcfile attribute to ignore
> user-specified modules in the call stack:
>
> $ cat wrapper.py
> import logging
> import os
> import sys
>
> logger = logging.getLogger()
>
> class SrcFile(object):
> def __init__(self, exclude_files):
> self.files = set(exclude_files)
> def __eq__(self, other):
> return other in self.files
>
> def fixname(filename):
> if filename.lower().endswith((".pyc", ".pyo")):
> filename = filename[:-4] + ".py"
> return os.path.normcase(filename)
>
> if "--monkey" in sys.argv:
> print "patching"
> logging._srcfile = SrcFile([logging._srcfile, fixname(__file__)])
>
> def warn(*args, **kw):
> logger.warn(*args, **kw)
>
> $ cat main.py
> import logging
> logging.basicConfig(format="%(filename)s<%(lineno)s>: %(message)s")
> import wrapper
> wrapper.warn("foo")
> wrapper.warn("bar")
> wrapper.warn("baz")
>
> $ python main.py
> wrapper.py<23>: foo
> wrapper.py<23>: bar
> wrapper.py<23>: baz
>
> $ python main.py --monkey
> patching
> main.py<4>: foo
> main.py<5>: bar
> main.py<6>: baz
>
> $ python -V
> Python 2.6.4
>
> Peter
Hi Peter,
thanks for your help ;-)
I've been offline for a few days so I found your message only today.
I'll try your proposal tomorrow.
Thanks again
Hellmut
--
Dr. Hellmut Weber mail@hellmutweber.de
Degenfeldstraße 2 tel +49-89-3081172
D-80803 München-Schwabing mobil +49-172-8450321
please: No DOCs, no PPTs. why: tinyurl.com/cbgq
==============================================================================
TOPIC: Binary data transfer issue
http://groups.google.com/group/comp.lang.python/t/94448ce25458f30f?hl=en
==============================================================================
== 1 of 2 ==
Date: Mon, Mar 15 2010 7:07 pm
From: Jordan Apgar
Hi all,
I'm trying to transfer a binary file over xmlrpclib. My test file is
a .jpeg file. I can transfer all the data over but when I go to open
the .jpeg I get "Error interpreting JPEG image file (Invalid JPEG file
structure: SOS before SOF)"
here's the code:
===========Various Shared Functions========
#EncodeAES takes a Cipher c and encrypts a String s
EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s)))
#DecodeAES takes a Cipher C and decrypts the cyphertext e
#then removes the padding from the decrypted string
DecodeAES = lambda c, e:
c.decrypt(base64.b64decode(e)).rstrip(PADDING)
CIPHER = AES.new("KEY")
# method to change strings back into tuples
def stringToTuple(string):
if string[0] + string[-1] == "()":
items = string[1:-1]
items = items.split(',')
return items
else:
raise ValueError("Badly formatted string (missing brackets).")
======client=================
def Download(self, FileName, Data_Size=1024):
ErrorCount = 0
Pkt_Num = -1
OldPkt_Num = Pkt_Num
localFile = open("files/"+FileName, "wb")
print
print "Requesting First Packet"
packet = self.client.download(EncodeAES(CIPHER, str(Pkt_Num)))
if packet[0] == "False":
print packet[1]
return False
packet = stringToTuple(DecodeAES(CIPHER, packet))
if int(packet[0]) == -3:
print "Received Error Packet"
else:
print "Received Packet: ", int(packet[0])
print "packet[1], ", packet[2:-1]
Pkt_Num = int(packet[0])
while Pkt_Num >= 0 and ErrorCount <= 10:
if (OldPkt_Num +1) == Pkt_Num:
OldPkt_Num = OldPkt_Num +1
localFile.write(binascii.
a2b_base64(packet[1][2:-1]))
#<<<<!!!!!<<< writing to the file on the client
ErrorCount == 0
else:
ErrorCount = ErrorCount +1
Pkt_Num = OldPkt_Num
print "Requesting Packet: ", Pkt_Num +1
packet = self.client.download(EncodeAES(CIPHER,
str(Pkt_Num)))
packet = stringToTuple(DecodeAES(CIPHER, packet))
Pkt_Num = int(packet[0])
if int(packet[0]) == -3:
print "Received Error Packet"
elif int(packet[0]) == -2:
print "Received EOF"
else:
print "Received Packet: ", int(packet[0])
localFile.close()
if Pkt_Num == -2:
print packet[1][2:-1]
return True
elif Pkt_Num == -3:
os.remove("files/"+FileName)
print packet[1][2:-1]
return False
else:
os.remove("files/"+FileName)
print "Unexpected Packet: File Transfer Stopped"
return False
==========Server Method=============
def download(self, Pkt_Num):
Old_Pkt_Num = self.PKT
DataRate = self.DATA
Pkt_Num = int(DecodeAES(CIPHER,Pkt_Num))
if Pkt_Num == -1 and Old_Pkt_Num == -1:
Pkt_Num = 0
try:
f = open("files/"+self.usr_dict[client][FILENAME],
"rb")
except IOError:
return EncodeAES(CIPHER,
str((-3, "File Could Not be
opened")))
data = binascii.b2a_base64(f.read((Pkt_Num+1 *
DataRate))) #<<<<<<!!!!!!!<<<<<<<< reading from file
f.close()
self.usr_dict[client][PKT] = Pkt_Num
return EncodeAES(CIPHER,
str((Pkt_Num, data)))
elif Pkt_Num == Old_Pkt_Num:
if Pkt_Num * DataRate >=
os.path.getsize("files/"+self.usr_dict[client][FILENAME]):
return EncodeAES(CIPHER,
str((-2, "File Transfer Complete")))
try:
f = open("files/"+self.usr_dict[client][FILENAME],
"rb")
except IOError:
return EncodeAES(CIPHER,
str((-3, "File Could Not be opened")))
data = f.read((Pkt_Num+1 * DataRate))
data = None
data = binascii.b2a_base64(f.read(DataRate))
#<<<<<<<<!!!!!!!<<<<<<< reading from file
f.close()
self.usr_dict[client][PKT] = Pkt_Num +1
return EncodeAES(CIPHER,
str((Pkt_Num+1, data)))
else:
return EncodeAES(CIPHER,
str((-3, "Incorrect Packet Requested")))
Thank you all for the help, I know there are better ways and transfer
protocol modules to but I'd like to do this without them.
~Jordan
== 2 of 2 ==
Date: Mon, Mar 15 2010 8:07 pm
From: MRAB
Jordan Apgar wrote:
> Hi all,
>
> I'm trying to transfer a binary file over xmlrpclib. My test file is
> a .jpeg file. I can transfer all the data over but when I go to open
> the .jpeg I get "Error interpreting JPEG image file (Invalid JPEG file
> structure: SOS before SOF)"
>
> here's the code:
>
> ===========Various Shared Functions========
> #EncodeAES takes a Cipher c and encrypts a String s
> EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s)))
>
> #DecodeAES takes a Cipher C and decrypts the cyphertext e
> #then removes the padding from the decrypted string
> DecodeAES = lambda c, e:
> c.decrypt(base64.b64decode(e)).rstrip(PADDING)
>
>
> CIPHER = AES.new("KEY")
>
>
> # method to change strings back into tuples
> def stringToTuple(string):
> if string[0] + string[-1] == "()":
> items = string[1:-1]
> items = items.split(',')
> return items
> else:
> raise ValueError("Badly formatted string (missing brackets).")
>
> ======client=================
> def Download(self, FileName, Data_Size=1024):
> ErrorCount = 0
> Pkt_Num = -1
> OldPkt_Num = Pkt_Num
> localFile = open("files/"+FileName, "wb")
> print
> print "Requesting First Packet"
> packet = self.client.download(EncodeAES(CIPHER, str(Pkt_Num)))
> if packet[0] == "False":
> print packet[1]
> return False
> packet = stringToTuple(DecodeAES(CIPHER, packet))
> if int(packet[0]) == -3:
> print "Received Error Packet"
> else:
> print "Received Packet: ", int(packet[0])
>
> print "packet[1], ", packet[2:-1]
>
> Pkt_Num = int(packet[0])
>
> while Pkt_Num >= 0 and ErrorCount <= 10:
> if (OldPkt_Num +1) == Pkt_Num:
> OldPkt_Num = OldPkt_Num +1
> localFile.write(binascii.
> a2b_base64(packet[1][2:-1]))
> #<<<<!!!!!<<< writing to the file on the client
> ErrorCount == 0
> else:
> ErrorCount = ErrorCount +1
> Pkt_Num = OldPkt_Num
>
> print "Requesting Packet: ", Pkt_Num +1
> packet = self.client.download(EncodeAES(CIPHER,
> str(Pkt_Num)))
> packet = stringToTuple(DecodeAES(CIPHER, packet))
> Pkt_Num = int(packet[0])
> if int(packet[0]) == -3:
> print "Received Error Packet"
> elif int(packet[0]) == -2:
> print "Received EOF"
> else:
> print "Received Packet: ", int(packet[0])
>
> localFile.close()
>
> if Pkt_Num == -2:
> print packet[1][2:-1]
> return True
> elif Pkt_Num == -3:
> os.remove("files/"+FileName)
> print packet[1][2:-1]
> return False
> else:
> os.remove("files/"+FileName)
> print "Unexpected Packet: File Transfer Stopped"
> return False
>
>
> ==========Server Method=============
> def download(self, Pkt_Num):
>
> Old_Pkt_Num = self.PKT
> DataRate = self.DATA
> Pkt_Num = int(DecodeAES(CIPHER,Pkt_Num))
> if Pkt_Num == -1 and Old_Pkt_Num == -1:
> Pkt_Num = 0
> try:
> f = open("files/"+self.usr_dict[client][FILENAME],
> "rb")
> except IOError:
> return EncodeAES(CIPHER,
> str((-3, "File Could Not be
> opened")))
> data = binascii.b2a_base64(f.read((Pkt_Num+1 *
> DataRate))) #<<<<<<!!!!!!!<<<<<<<< reading from file
I don't understand what you're trying to do when you're reading from
the file. For a start:
Pkt_Num+1 * DataRate
is the same as:
Pkt_Num + (1 * DataRate)
or:
Pkt_Num + DataRate
and that's how many bytes you're reading.
> f.close()
> self.usr_dict[client][PKT] = Pkt_Num
> return EncodeAES(CIPHER,
> str((Pkt_Num, data)))
>
> elif Pkt_Num == Old_Pkt_Num:
> if Pkt_Num * DataRate >=
> os.path.getsize("files/"+self.usr_dict[client][FILENAME]):
> return EncodeAES(CIPHER,
> str((-2, "File Transfer Complete")))
> try:
> f = open("files/"+self.usr_dict[client][FILENAME],
> "rb")
> except IOError:
> return EncodeAES(CIPHER,
> str((-3, "File Could Not be opened")))
> data = f.read((Pkt_Num+1 * DataRate))
Here you're reading:
Pkt_Num + DataRate
bytes again,
> data = None
discarding what you've just read,
> data = binascii.b2a_base64(f.read(DataRate))
> #<<<<<<<<!!!!!!!<<<<<<< reading from file
and then reading:
DataRate
more bytes.
I think what you meant to do is .seek to the position in the file and
then read some bytes:
f.seek(Pkt_Num * DataRate)
data = f.read(DataRate)
ie, send the contents of the file in chunks which are DataRate bytes
long (the last chunk will be shorter if the file size isn't a multiple
of DataRate bytes).
> f.close()
> self.usr_dict[client][PKT] = Pkt_Num +1
>
> return EncodeAES(CIPHER,
> str((Pkt_Num+1, data)))
>
> else:
>
> return EncodeAES(CIPHER,
> str((-3, "Incorrect Packet Requested")))
>
>
>
> Thank you all for the help, I know there are better ways and transfer
> protocol modules to but I'd like to do this without them.
>
I'm not surprised that the transfer is failing! :-)
==============================================================================
TOPIC: Build Python with XCode
http://groups.google.com/group/comp.lang.python/t/c623db340ebcef05?hl=en
==============================================================================
== 1 of 1 ==
Date: Mon, Mar 15 2010 8:15 pm
From: moerchendiser2k3
Hi,
I would like to build Python with Xcode (but without the makefile).
Does anyone know a link where I can get a real xcodeproj with the
current Py2.x sources?
Thanks in advance!! Bye, donnerCobra
==============================================================================
TOPIC: converting datetime with tzinfo to unix timestamp
http://groups.google.com/group/comp.lang.python/t/1b56db853fe6407f?hl=en
==============================================================================
== 1 of 1 ==
Date: Mon, Mar 15 2010 8:52 pm
From: "Gabriel Genellina"
En Fri, 12 Mar 2010 20:15:55 -0300, Michael Torrie <torriem@gmail.com>
escribió:
> On Python 2.5 here.
>
> I've searched and searched but I can't find any way to convert a
> datetime object that includes a timezone (tzinfo) to a unix timestamp.
> Folks on the net say to simply use the timetuple() method of the object
> and feed that to time.mktime(). But that just doesn't seem to work for
> me. At least if my understanding that the unix timestamp is always UTC.
Try time.mktime(datetimeobject.utctimetuple()) instead.
--
Gabriel Genellina
==============================================================================
TOPIC: result of os.times() is different with 'time' command Options
http://groups.google.com/group/comp.lang.python/t/ab45c1ed3e45401c?hl=en
==============================================================================
== 1 of 1 ==
Date: Mon, Mar 15 2010 10:50 pm
From: "Gabriel Genellina"
En Mon, 15 Mar 2010 03:51:28 -0300, hiral <hiralsmaillist@gmail.com>
escribió:
> On Mar 15, 7:14 am, Tim Roberts <t...@probo.com> wrote:
>> hiral<hiralsmaill...@gmail.com> wrote:
>> >Output:
>> >real 0.0m0.0100000002421s
>> >user 0.0m0.0s
>> >sys 0.0m0.0s
>>
>> >Command:
>> >$ time ls
>>
>> >Output:
>> >real 0m0.007s
>> >user 0m0.000s
>> >sys 0m0.000s
>>
>> You can't really do an analysis like this with a task that only takes a
>> few
>> milliseconds. There are too many variables.
>
> Thanks for your explanation.
Have you tested with the highly recursive function in that 2007 thread you
linked to?
This should take time enough to make a problem like this clearly visible.
--
Gabriel Genellina
==============================================================================
You received this message because you are subscribed to the Google Groups "comp.lang.python"
group.
To post to this group, visit http://groups.google.com/group/comp.lang.python?hl=en
To unsubscribe from this group, send email to comp.lang.python+unsubscribe@googlegroups.com
To change the way you get mail from this group, visit:
http://groups.google.com/group/comp.lang.python/subscribe?hl=en
To report abuse, send email explaining the problem to abuse@googlegroups.com
==============================================================================
Google Groups: http://groups.google.com/?hl=en
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home