Re: zipfile extracting png files corrupt

2009-10-17 Thread marek . rocki
Have you tried opening the zip file in binary mode? Regards, Marek -- http://mail.python.org/mailman/listinfo/python-list

Re: How do I escape slashes the string formatting operator? (or: why is it behaving this way?)

2009-05-06 Thread marek . rocki
Dustan napisał(a): > Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit > (Intel)] on > win32 > Type "help", "copyright", "credits" or "license" for more information. > >>> 'HELP!%(xyz)/' % {'xyz':' PLEASE! '} > Traceback (most recent call last): > File "", line 1, in > ValueError

Re: Lisp mentality vs. Python mentality

2009-04-25 Thread marek . rocki
Ciprian Dorin, Craciun napisał(a): > Python way: > def compare (a, b, comp = eq) : > if len (a) != len (b) : > return False > for i in xrange (len (a)) : > if not comp (a[i], b[i]) : >return False > return True This is shorter, but I'm not sure if more pythonic: def compare(a, b, compfun

Re: Enumerating k-segmentations of a sequence

2008-11-25 Thread marek . rocki
bullockbefriending bard napisał(a): > I'm not sure if my terminology is precise enough, but what I want to > do is: > > Given an ordered sequence of n items, enumerate all its possible k- > segmentations. > > This is *not* the same as enumerating the k set partitions of the n > items because I am o

Re: Calculating timespan

2008-09-28 Thread marek . rocki
Erhard napisał(a): > I've been looking at the date/time classes and I'm at a loss as to how > to do this (probably too used to other platforms). > > I have two date/time values. One represents 'now' and the other the last > modified time of a file on disk (from stat). I need to calculate the > diff

Re: dict generator question

2008-09-18 Thread marek . rocki
Simon Mullis napisał(a): > Something like: > > dict_of_counts = dict([(v[0:3], "count") for v in l]) > > I can't seem to figure out how to get "count", as I cannot do x += 1 > or x++ as x may or may not yet exist, and I haven't found a way to > create default values. It seems to me that the "count

Re: ctypes initializer

2008-08-23 Thread marek . rocki
castironpi napisał(a): > Is there a way to initialize a ctypes Structure to point to an offset > into a buffer? I don't know if the way I'm doing it is supported. There is a high probability you're abusing ctypes too much, but it's possible. The following seems to work: from ctypes import * clas

Re: newb loop problem

2008-08-13 Thread marek . rocki
Dave napisał(a): > Hey there, having a bit of problem iterating through lists before i go > on any further, here is > a snip of the script. > -- > d = "a1 b1 c1 d1 e1 a2 b2 c2 d2 e2 a3 b3 c3 d3 e3 a4 b4 c4 d4 e4 a5 b5 > c5 d5 e5" > inLst = d.split() > hitLst = [] > > hitNum = 0 > stopCnt = 6 + hitN

Re: Unexpected default arguments behaviour (Maybe bug?)

2008-07-13 Thread marek . rocki
[EMAIL PROTECTED] napisał(a): > Hi, I have just encountered a Python behaviour I wouldn't expect. Take > the following code: > > > class Parent: > a = 1 > > def m (self, param = a): > print "param = %d" % param

Re: Nested generator caveat

2008-07-06 Thread marek . rocki
Excellent explanation by Mark Wooding. I would only like to add that the standard pythonic idiom in such cases seems to be the (ab)use of a default argument to the function, because these get evaluated at the definition time: def gen0(): for i in range(3): def gen1(i = i):

Re: Javascript - Python RSA encryption interoperability

2008-07-04 Thread marek . rocki
Evren Esat Ozkan napisał(a): > Hello, > > I'm trying to encrypt a string with RSA. But it needs to be compitable > with Dave's JavaScript RSA implementation*. I'm already read and tried > lots of different things about RSA and RSA in Python. But could not > produce the same result with the javascri

Re: Wrapping a method twice

2008-06-25 Thread marek . rocki
Nicolas Girard napisał(a): > prepend(C.f,pre) This wraps f, returns wrapped and binds it to C.f (but the method.__name__ is still wrapped). > append(C.f,post) This wraps wrapped (the one previously returned), returns wrapped (a new one) and binds it to C.wrapped (since that is what its meth

Re: [ctypes] convert pointer to string?

2008-05-21 Thread marek . rocki
Neal Becker napisał(a): > In an earlier post, I was interested in passing a pointer to a structure to > fcntl.ioctl. > > This works: > > c = create_string_buffer (...) > args = struct.pack("iP", len(c), cast (pointer (c), c_void_p).value) > err = fcntl.ioctl(eos_fd, request, args) > > Now to do the

Re: Given a string - execute a function by the same name

2008-04-28 Thread marek . rocki
One solution may be to use globals(): >>> globals()['foo']() Regards, Marek -- http://mail.python.org/mailman/listinfo/python-list

Re: Form sha1.hexdigest to sha1.digest

2008-04-06 Thread marek . rocki
Martin v. Löwis napisał(a): > > Or hexdigest_string.decode('hex') > > I would advise against this, as it's incompatible with Python 3. I didn't know that, you actually made me look it up in the Python 3 FAQ. And yes, the difference is that decode will return bytes type instead of a string. This ma

Re: Form sha1.hexdigest to sha1.digest

2008-04-06 Thread marek . rocki
Martin v. Löwis napisał(a): > > How can convert string from sha1.hexdigest() to string that is the > > same, like from sha1.digest() > > Use binascii.unhexlify. > > HTH, > Martin Or hexdigest_string.decode('hex') -- http://mail.python.org/mailman/listinfo/python-list

Re: Can anyone help me?

2008-03-29 Thread marek . rocki
I love Paul's response. [EMAIL PROTECTED] napisał(a): > My crystal ball broke a few days ago due to overuse, but I recall that OpenGL likes to have power-of-two texture sizes. If that doesn't work, please trim down your code as far as possible (so that it still displays the error) and post it. -

Re: pass float array(list) parameter to C

2008-03-17 Thread marek . rocki
You can also do it with ctypes only; too bad it's not really well documented. c_float is a type of a floating point number and it has * operator defined, so that c_float*4 is a type of a 4-element array of those numbers. So if you want to construct an array of floats from a list of floats, you can

Re: Want - but cannot get - a nested class to inherit from outer class

2008-03-08 Thread marek . rocki
Everybody has already explained why this doesn't work and a possible "solution" using metaclasses was suggested. I tried to implement it, ended up with monkey-patching __bases__, which is certainly an abomination (will it be possible in python 3.0?) but seems to do the trick. class _InheritFromOut

Re: dict.get and str.xsplit

2008-02-26 Thread marek . rocki
[EMAIL PROTECTED] napisał(a): > [EMAIL PROTECTED]: > > As for the original prooblem, why not use > > defaultdict? I think it's the most idiomatic way to achieve what we > > want. And also the fastest one, according to my quick-and-dirty tests: > > It adds the new keys, I can't accept that: Right,

Re: dict.get and str.xsplit

2008-02-26 Thread marek . rocki
[EMAIL PROTECTED] napisał(a): > It's slower still, despite doing the lookup two times half of the > times (half keys are present in the test set). The "in" is an > operator, it's faster than a method call, but I don't understand the > other details. Now the difference between 1.78 and 1.56 is small

Re: Parent instance attribute access

2008-02-25 Thread marek . rocki
Hello. Please post the minimal code that can be run and demonstrates what's wrong. I tried the following and it works fine, args and kwargs are visible: class MyFactory(object): def __init__(self, *args, **kwargs): self.args = args self.kwargs = kwargs clas

Re: Floating point bug?

2008-02-13 Thread marek . rocki
Not a bug. All languages implementing floating point numbers have the same issue. Some just decide to hide it from you. Please read http://docs.python.org/tut/node16.html and particularly http://docs.python.org/tut/node16.html#SECTION001610 Regards, Marek -- http://mail.python.org

Re: Encrypting a short string?

2008-02-11 Thread marek . rocki
erikcw napisal(a): > But that can't be reversed, right? I'd like to be able to decrypt the > data instead of having to store the hash in my database... In such case it seems you have no choice but to use a symmetric encryption algorithm - in other words, your original method. If the strings are ~20

Re: Encrypting a short string?

2008-02-11 Thread marek . rocki
erikcw napisal(a): > Hi, > > I'm trying to devise a scheme to encrypt/obfuscate a short string that > basically contains the user's username and record number from the > database. I'm using this encrypted string to identify emails from a > user. (the string will be in the subject line of the email

Re: Sine Wave Curve Fit Question

2008-01-30 Thread marek . rocki
Iain Mackay napisal(a): > Python Folks > > I'm a newbie to Python and am looking for a library / function that can help > me fit a 1D data vector to a sine wave. I know the frequency of the wave, > so its really only phase and amplitude information I need. > > I can't find anything in the most w

Re: validate string is valid maths

2008-01-28 Thread marek . rocki
I decided to play with it a little bit, but take a different approach than Steven. This seems actually one of the problems where regexp might be a good solution. import re re_signednumber = r'([-+]?\d+)' re_expression = '%s(?:([-+/*])[-+/*]*?%s)*' % (re_signednumber, re_signednumber) for test_cas

Re: Doesn't know what it wants

2008-01-26 Thread marek . rocki
> class vec2d(ctypes.Structure): > """2d vector class, supports vector and scalar operators, >and also provides a bunch of high level functions >""" > __slots__ = ['x', 'y'] > > def __init__(self, x_or_pair, y = None): > > if y == None: > self.x = x_o

Re: Just for fun: Countdown numbers game solver

2008-01-20 Thread marek . rocki
Nice challenge! I came up with something like this: def find_repr(target, numbers): org_reprs = dict((number, str(number)) for number in numbers) curr_reprs = org_reprs while target not in curr_reprs: old_reprs, curr_reprs = curr_reprs, {} fo

Re: Need help with a regular expression

2007-12-19 Thread marek . rocki
On 19 Gru, 13:08, Sharun <[EMAIL PROTECTED]> wrote: > I am trying to find the substring starting with 'aaa', and ending with > ddd OR fff. If ddd is found shouldnt the search stop? Shouldn't > re5.search(str5).group(0) return 'aaa bbb\r\n ccc ddd' ? The documentation for the re module (http://docs

Re: how to convert 3 byte to float

2007-12-09 Thread marek . rocki
Mario M. Mueller napisał(a): > Personally I would expect simple counts (since other seismic formats don't > even think of using floats because most digitizers deliver counts). But I > was told that there are floats inside. > > But if I assume counts I get some reasonable numbers out of the file. I

Re: Magic class member variable initialization with lists

2007-11-14 Thread marek . rocki
This is the expected behaviour. The reference on classes (http:// docs.python.org/ref/class.html) says: > Variables defined in the class definition are class variables; > they are shared by all instances. To define instance variables, > they must be given a value in the __init__() method or in > a

Re: Transfer socket connection between programs

2007-11-12 Thread marek . rocki
JamesHoward napisa (a): > Does anyone know any method to have one program, acting as a server > transfer a socket connection to another program? I looked into > transferring the connection via xml rpc to no avail. It seems to be a > problem of getting access to a programs private memory space and >

Re: Iteration for Factorials

2007-10-24 Thread marek . rocki
Tim Golden napisa (a): > It's only a moment before the metaclass and > the Twisted solution come along. :) I couldn't resist. It's not as elegant as I hoped, but hey, at least it memoizes the intermediate classes :-) class fact_0(object): value = 1 class fact_meta(object): def

Re: name space problem

2007-10-23 Thread marek . rocki
BBands napisa (a): > An example: > > class classA: > def __init__(self): > self.b = 1 > > def doStuff(): > some calcs > a..b = 0 > > a = classA(): > print a.b > doStuff() > print a.b > > That works as hoped, printing 1, 0. > > But, if I move doStuff to another module and: > > im

Re: Using arrays in Python - problems.

2007-10-23 Thread marek . rocki
attackwarningred napisa (a): > The array F(n) is dynamically allocated earlier on and is sized with > reference to shotcount, the number of iterations the model performs. The > problem is I can't get something like this to run in Python using numpy, > and for the size of the array to be sized dyn

Re: generate list of partially accumulated values

2007-09-16 Thread marek . rocki
Those methods of computing partial sums seem to be O(n^2) or worse. What's wrong with an ordinary loop? for i in xrange(1, len(l)): l[i] += l[i - 1] And as for the list of objects: ll = [l[i - 1].method(l[i]) for i in xrange(1, len(l))] + l[-1] -- http://mail.python.org/mailman/listinfo/py

Re: To count number of quadruplets with sum = 0

2007-03-16 Thread marek . rocki
My attempt uses a different approach: create two sorted arrays, n^2 elements each; and then iterate over them looking for matching elements (only one pass is required). I managed to get 58,2250612857 s on my 1,7 MHz machine. It requires numpy for decent performance, though. import numpy import tim

Re: __cmp__ method

2006-06-14 Thread marek . rocki
Python documentation says: >__cmp__( self, other) > Called by comparison operations if rich comparison (see above) is not defined. So it seems you have to redefine rich comparisons __lt__, __gt__, __eq__ etc as well. If all you need is change sorting order, why not use appropriate parameters of so

Re: Linear regression in NumPy

2006-03-17 Thread marek . rocki
nikie napisal(a): > I'm a little bit stuck with NumPy here, and neither the docs nor > trial&error seems to lead me anywhere: > I've got a set of data points (x/y-coordinates) and want to fit a > straight line through them, using LMSE linear regression. Simple > enough. I thought instead of looking

Re: Adding method at runtime - problem with self

2006-03-06 Thread marek . rocki
Thank you all for your responses. That's exactly what I needed to know - how to bind a function to an object so that it would comply with standard calling syntax. This is largely a theoretical issue; I just wanted to improve my understanding of Python's OOP model. Using such features in real life

Re: searching for the number of occurences of a string

2006-03-05 Thread marek . rocki
> hi; > say i have a text file with a string ( say '(XYZ)') and I want to find > the number of line where this string has occured. What is the best way > to do that? I would suggest: # Read file contents lines = file('filename.txt').readlines() # Extract first line number containing 'XYZ' string

Adding method at runtime - problem with self

2006-03-05 Thread marek . rocki
First of all, please don't flame me immediately. I did browse archives and didn't see any solution to my problem. Assume I want to add a method to an object at runtime. Yes, to an object, not a class - because changing a class would have global effects and I want to alter a particular object only.