Re: Iterating over files of a huge directory

2012-12-17 Thread marduk


On Mon, Dec 17, 2012, at 10:28 AM, Gilles Lenfant wrote:
> Hi,
> 
> I have googled but did not find an efficient solution to my problem. My
> customer provides a directory with a hge list of files (flat,
> potentially 10+) and I cannot reasonably use os.listdir(this_path)
> unless creating a big memory footprint.
> 
> So I'm looking for an iterator that yields the file names of a directory
> and does not make a giant list of what's in.
> 
> i.e :
> 
> for filename in enumerate_files(some_directory):
> # My cooking...
> 


You could try using opendir[1] which is a binding to the posix call.  I
believe that it returns an iterator (file-like) of the entries in the
directory.

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


Re: Problem with Unicode char in Python 3.3.0

2013-01-06 Thread marduk


On Sun, Jan 6, 2013, at 11:43 AM, Franck Ditter wrote:
> Hi !
> I work on MacOS-X Lion and IDLE/Python 3.3.0
> I can't get the treble key (U1D11E) !
> 
> >>> "\U1D11E"
> SyntaxError: (unicode error) 'unicodeescape' codec can't 
> decode bytes in position 0-6: end of string in escape sequence
> 

You probably meant:

>>> '\U0001d11e'


For that synax you must use either '\u' or '\U' (i.e.
specify either 4 or 8 hex digits).

http://docs.python.org/2/howto/unicode#unicode-literals-in-python-source-code

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


Re: Over 30 types of variables available in python ?

2013-01-07 Thread marduk
So I guess if one *really* wanted to compare C variables to Python
variables, you could say that all python variables are of type void*
except Python does all mallocs/frees and the casting for you.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to tell how many weeks apart two datetimes are?

2013-01-08 Thread marduk


On Tue, Jan 8, 2013, at 04:22 PM, Roy Smith wrote:
> How do you tell how many weeks apart two datetimes (t1 and t2) are?
> The "obvious" solution would be:
> 
> weeks = (t2 - t1) / timedelta(days=7)
> 
> but that doesn't appear to be allowed.  Is there some fundamental
> reason why timedelta division not supported?
> -- 
> http://mail.python.org/mailman/listinfo/python-list

It works for python 3(.2):

>>> x = datetime.timedelta(days=666)
>>> week = datetime.timedelta(days=7)
>>> x / week
95.14285714285714
>>> halfday = datetime.timedelta(hours=12)
>>> x / halfday
1332.0
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What is "self"?

2005-09-22 Thread marduk
On Thu, 2005-09-22 at 21:36 -0400, Wayne Sutton wrote:
> OK, I'm a newbie...
> I'm trying to learn Python & have had fun with it so far.  But I'm having 
> trouble following the many code examples with the object "self."  Can 
> someone explain this usage in plain english?

"self" references the object itself.  It's usually also called "this" on
other languages (C++ & Java I believe).  In Python, when you define a
class method, the reference to the object is passed explicitly rather
than implicitly.

Also, the name "self" is used by convention.  You could use any name if
you wanted, but if you want other people to understand your code then
use "self".

Is that plain English enough?  

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


Re: Class Help

2005-10-01 Thread marduk
On Sat, 2005-10-01 at 18:58 -0400, Ivan Shevanski wrote:
> To continue with my previous problems, now I'm trying out classes.  But I 
> have a problem (which I bet is easily solveable) that I really don't get.  
> The numerous tutorials I've looked at just confsed me.For intance:
> 
> >>>class Xyz:
> ... def y(self):
> ... q = 2
> ...
> >>>Xyz.y()
> Traceback (most recent call last):
>   File "", line 1, in ?
> TypeError: unbound method y() must be called with Xyz instance as first 
> argument
> (got nothing instead)
> 
> 
> So. . .What do I have to do? I know this is an extremley noob question but I 
> think maybe if a person explained it to me I would finally get it =/


When you define a class, say Xyz, your are defining your own type.  Say
that Person is a class.  And person has a method walk():

class Person:
def walk(self):
...

now to use the Person class, you need to create an instance of it.  You
can't just say Person.walk() because Person is a "class"ification, not a
real object.  You need an instance of person.

jane = Person()

This creates a new person called "jane".  "jane" is an instance of the
class Person.

>>> jane
 <__main__.Person instance at 0x2c723710>

Now we can tell jane to walk:

jane.walk()

So what the error is telling you (in a bit confusing way if you're a
newbie) is that you are calling a method y() but you have not created an
instance of your Xyz class to do y().  Or, to use my analogy, you are
telling Person to walk, but you can't make Person walk, you have to
create a person, jane, and have jane walk.

Hope this helps, but I recommend you read a good intro to
object-oriented programming.

-a

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


Re: "no variable or argument declarations are necessary."

2005-10-03 Thread marduk

>   egold = 0:
>   while egold < 10:
> if test():
>   ego1d = egold + 1
> 

Both pylint and pychecker pick this up.  I wrapped the code in a
function (to prevent importing from running in an infinite loop) and ran
both pylint and pychecker:

plyint: W:  5:myfunc: Unused variable 'ego1d'
pychecker: test.py:4: Local variable (ego1d) not used


I make a habit to run pylint or pychecker on my code often.  They pick
up a lot of stuff like unused variables, etc.

But you can also do this:

/* initialize variables i'm gonna use */
int vara = 0; 
int varb = 0;
while (vara < 10) {
varb = vara + 1;
}

So we can make a similar mistake in C if you type the wrong (declared)
variable name.  Moreover, "gcc -Wall" did not report the "unused"
variable so it might be even more difficult to track down the problem. 


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


Re: "no variable or argument declarations are necessary."

2005-10-04 Thread marduk
On Tue, 2005-10-04 at 11:43 -0700, Paul Rubinhttp: wrote:
> What's the big deal?  Perl has an option for flagging undeclared
> variables with warnings ("perl -w") or errors ("use strict") and Perl
> docs I've seen advise using at least "perl -w" routinely.  Those
> didn't have much impact.  Python already has a "global" declaration;
> how does it de-Pythonize the language if there's also a "local"
> declaration and an option to flag any variable that's not declared as
> one or the other? 

I would be happy with a "local" option. e.g.

def myfunc():
   local spam = ...
   local eggs = ...
   global juice

   breakfast = juice + spam + eggs # raises an exception (undeclared
breakfast)


What I'm *afraid* of is:

def myfunc(MyClass myparam):
   int spam = 6
   str eggs

   # etc

i.e. typed declarations and type checking.  This would annoy the heck
out of me. 

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


Re: Jargons of Info Tech industry

2005-10-04 Thread marduk
[Removed X-posting]

On Tue, 2005-10-04 at 17:14 +, Roedy Green wrote:
> On Tue, 23 Aug 2005 08:32:09 -0500, l v <[EMAIL PROTECTED]> wrote or quoted :
> 
> >I think e-mail should be text only. 
> 
> I disagree.  Your problem  is spam, not HTML. Spam is associated with
> HTML and people have in Pavlovian fashion come to hate HTML.
> 
> But HTML is not the problem! 

And guns don't kill people: people kill people!

Seriously though, plain-text is just plain [n]etiquette (for most
newsgroups/mailing lists (at least the technical ones)).  Follow the
rules and avoid becoming a social outcast.  If your particular forum
allows/encourages HTML then  away!  

( This post is neither x-posted, HTML-ized, closed-captioned nor
simulcast in Spanish or HD )

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


how to keep collection of existing instances and return one on instantiation

2005-10-05 Thread marduk
I couldn't think of a good subject..

Basically, say I have a class

class Spam:
def __init__(self, x):
self.x = x


then if I create two instances:

a = Spam('foo')
b = Spam('foo')

a == b # False

What I *really* want is to keep a collection of all the Spam instances,
and if i try to create a new Spam instance with the same contructor
parameters, then return the existing Spam instance.  I thought new-style
classes would do it:

class Spam(object):
cache = {}
def __new__(cls, x):
if cls.cache.has_key(x):
return cls.cache[x]
def __init__(self, x):
self.x = x
self.cache[x] = self

a = Spam('foo')
b = Spam('foo')

Well, in this case a and b are identical... to None!  I assume this is
because the test in __new__ fails so it returns None, I need to then
create a new Spam.. but how do I do that without calling __new__ again?
I can't call __init__ because there's no self...

So what is the best/preferred way to do this?

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


Re: how to keep collection of existing instances and return one on instantiation

2005-10-05 Thread marduk
On Wed, 2005-10-05 at 12:56 -0400, Jonathan LaCour wrote:
> Oops, you forgot to return object.__new__(cls, x) in the case the  
> object isn't in the cache.  That should fix it. 

Ahh, that did it.  I didn't even think of calling object...

so the new class looks like:

class Spam(object):
cache = {}
def __new__(cls, x):
if cls.cache.has_key(x):
return cls.cache[x]
else:
new_Spam = object.__new__(cls, x)
cls.cache[x] = new_Spam
return new_Spam
def __init__(self, x):
self.x = x

a = Spam(2)
b = Spam(2)

a == b # => True
id(a) == id(b) # => True

Thanks for all your help.

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


Re: how to keep collection of existing instances and return one on instantiation

2005-10-05 Thread marduk
On Wed, 2005-10-05 at 18:28 +0200, Diez B. Roggisch wrote:
> Use the BORG-pattern. See
> 
> 
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66531
> 
> Together with your caching, that should do the trick.
> 

I looked at the Borg Pattern, but I don't think it was exactly what I
want.

The Borg patten appears to be if you want multiple instances that point
to the same "data".

What I wanted is multiple calls to create a new object with the same
parameters points to the "original" object instead of creating a new
one.

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


Re: how to keep collection of existing instances and return one on instantiation

2005-10-05 Thread marduk
On Wed, 2005-10-05 at 12:56 -0400, Jonathan LaCour wrote:
> > class Spam(object):
> > cache = {}
> > def __new__(cls, x):
> > if cls.cache.has_key(x):
> > return cls.cache[x]
> > def __init__(self, x):
> > self.x = x
> > self.cache[x] = self
> >
> > a = Spam('foo')
> > b = Spam('foo')
> >
> > Well, in this case a and b are identical... to None!  I assume this is
> > because the test in __new__ fails so it returns None, I need to then
> > create a new Spam.. but how do I do that without calling __new__  
> > again?
> > I can't call __init__ because there's no self...
> >
> >
> 
> Oops, you forgot to return object.__new__(cls, x) in the case the  
> object isn't in the cache.  That should fix it.

Okay, one more question... say I then

c = Spam('bar')
del a
del b

I've removed all references to the object, except for the cache.  Do I
have to implement my own garbage collecting is or there some "magical"
way of doing this within Python?  I pretty much want to get rid of the
cache as soon as there are no other references (other than the cache).

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


Re: how to keep collection of existing instances and return one on instantiation

2005-10-05 Thread marduk
On Wed, 2005-10-05 at 19:37 +0200, Diez B. Roggisch wrote:
> > What I wanted is multiple calls to create a new object with the same
> > parameters points to the "original" object instead of creating a new
> > one.
> 
> Read the comments. What you say is essentially the same - the data 
> matters, after all. What do you care if there are several instances
> around?
> 
> Diez 

In my case it matters more that the objects are the same.

For example I want set([Spam(1), Spam(2),
Spam(3)]).intersect(set([Spam(1), Spam(2)]) to contain two items instead
of 0.

For this and many other reasons it's important that Spam(n) is Spam(n).

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


Re: how to keep collection of existing instances and return one on instantiation

2005-10-05 Thread marduk
On Wed, 2005-10-05 at 19:24 +0200, Peter Otten wrote:
> Use a weakref.WeakValueDictionary as the cache instead of a normal
> dict.
> 
> Peter 

Thanks for the reference to the weakref module.  Until now I've never
had a use for it, but it sounds like what I'm looking for.

-m

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


Re: Let My Terminal Go

2005-10-10 Thread marduk
On Mon, 2005-10-10 at 22:58 -0700, [EMAIL PROTECTED] wrote:
> Hello,
> 
> A user of my application points me to a behavior in gVim,
> the text editor, that I would like to implement in my
> application.
> 
> When gVim is launched from a shell terminal, it completely
> frees the terminal. You can continue to use the terminal for
> whatever purpose you wish, including closing and exiting it,
> without any effect on the running gVim instance.
> 
> How do I implement this in my application written in python?
> I would like to believe it does not involve me forking my
> application in a new process. Maybe there is signal I can
> send to the operating system to achieve this, right?

gvim forks.  Why do you want to avoid it?

import os, sys

pid = os.fork()
if pid !=0:
# exit parent
sys.exit(0)
# child continues


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


Re: piping out binaries properly

2005-10-11 Thread marduk
On Wed, 2005-10-12 at 00:16 -0400, Mike Meyer wrote:
[...]
> It's not normal to write binary content to stdout - you normally write
> it to a file. Open the file with open(name, 'wb') to write binaries.
> 

It is interesting that as a "Unix consultant" you should make that
claim.  Especially since

>>> sys.stdout
', mode 'w' at 0x2aac9198>
  

Indeed there would be a lot of broken Unix systems out there if that
were not the case.

As for the OP, you may want to check how stdout is opened on your
system.  In Windows there are two "write" modes for files, 'w', and 'wb'
where apparently 'w' is text mode and 'wb' is binary.  You may want to
check what mode your stdout is in.  I don't have a Windows box handy
right now to verify.

> There doesn't appear to be any way to retroactively change the mode on
> a file. Which is probably a good thing.
> 
>  Mike Meyer <[EMAIL PROTECTED]>
http://www.mired.org/home/mwm/
> Independent WWW/Perforce/FreeBSD/Unix consultant, email for more
information.


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


Re: grep

2005-10-25 Thread marduk
On Tue, 2005-10-25 at 06:45 +, David Isaac wrote:
> What's the standard replacement for the obsolete grep module?

AFAIK there never was a "grep" module.  There does, however exist a
deprecated "regex" module:

>>> import regex
__main__:1: DeprecationWarning: the regex module is deprecated; please
use the re module


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


Re: pickle

2005-10-25 Thread marduk
On Mon, 2005-10-24 at 23:55 -0700, Shi Mu wrote:
> I got a sample code and tested it but really can not understand the
> use of pickle and dump:
> 
> >>> import pickle
> >>> f = open("try.txt", "w")
> >>> pickle.dump(3.14, f)
> >>> pickle.dump([1,2,3,4], f)
> >>> f.close()

The pickle module "serializes" python objects.  You can "dump" a python
object that can later be loaded:

>>> x = complex(2,3.5)
>>> f = open("cnumber.pickle", "w")
>>> import pickle
>>> pickle.dump(x, f)
>>> f.close()
>>> y = pickle.load(file("cnumber.pickle", "r"))
>>> y
(2+3.5j)


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


Re: I Need Motivation Part 2

2005-11-04 Thread marduk
On Fri, 2005-11-04 at 17:06 +0100, Magnus Lycka wrote:
> If Python appears more complex
> than C++, you must be using a really weird approach.

Or a really weird editor ;-)

-m
> 

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


Re: strip not working on strings?

2005-11-13 Thread marduk
On Sun, 2005-11-13 at 13:16 -0800, [EMAIL PROTECTED] wrote:
> I'm using Python 2.3.5 and when I type the following in the interactive
> prompt I see that strip() is not working as advertised:
> 
> >>>s = 'p p:p'
> >>>s.strip(' :')
> 'p p:p'
> 
> Is this just me or does it not work? I want to get rid of all ' ' and
> ':' in the string. I've checked the doc and from what I can tell this
> is what strip() is supposed to do. Thanks for any help.
> 

According to my docs it says "Return a copy of the string with the
leading and trailing characters removed."  There are no leading or
trailing spaces or colons in 'p p:p'.

What your probably looking for is the .replace() method.

-m

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


Re: is parameter an iterable?

2005-11-15 Thread marduk
On Tue, 2005-11-15 at 11:01 -0800, py wrote:
> I have function which takes an argument.  My code needs that argument
> to be an iterable (something i can loop over)...so I dont care if its a
> list, tuple, etc.  So I need a way to make sure that the argument is an
> iterable before using it.  I know I could do...
> 
> def foo(inputVal):
> if isinstance(inputVal, (list, tuple)):
> for val in inputVal:
> # do stuff
> 
> ...however I want to cover any iterable since i just need to loop over
> it.
> 
> any suggestions?
> 

You could probably get away with 

if hasattr(inputVal, '__getitem__')


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


Re: Problems with os.system

2005-09-02 Thread marduk
On Fri, 2005-09-02 at 13:45 -0700, alexLIGO wrote:
> Hi,
> 
> I am trying to run a python script that executes several other programs
> on the bash (under linux) and reads some of the output of those
> programs. This works fine for a couple of os.system() calls, but then
> it does not work anymore. os.system() returns always -1, but when
> executing exactly the same program directly on the linux-bash it works!
> 
> Could this be a memory problem? It happens when there is much data
> stored in a python list. How can I check the memory usage in my python
> script? Can I force python to execute the program on the bash? What can
> I do?

Are you trying to read the output (stdout) of the program?  If so you
should be using os.popen() not os.system().  

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


Re: FTP status problems. (Again)

2005-09-16 Thread marduk
On Fri, 2005-09-16 at 19:27 -0700, Nainto wrote:
> Hello, I have posted before about trying to find the status of an FTP
> uplaod but couldn't get anything to work. After some more searching I
> found
> http://groups.google.com/group/comp.lang.python/browse_thread/thread/76be9a994547db4/91917c906cdc04d4?q=ftp+progress&rnum=1#91917c906cdc04d4
> but it does not seem to work because it just uploads the file and does
> not print a . onto the screen. HEre is the code I have when I'm using
> the code from that link.
> import ftplib
> import os
> class dot_FTP(ftplib.FTP):
>   def storbinary(self, cmd, fp, blocksize=8192):
>   self.voidcmd('TYPE I')
>   conn = self.transfercmd(cmd)
>   while 1:
>   buf = fp.read(blocksize)
>   if not buf: break
>   conn.send(buf)
>   sys.stdout.write('.')
>   sys.stdout.flush()
>   conn.close()
>   return self.voidresp()
> 
> 
> ftp = ftplib.FTP("FTPADDRESS")
> ftp.login("user","pass")
> file = "/file"
> ftp.storbinary("STOR " + file, open(file, "rb"), 1024)
> ftp.quit()
> Does anyone know why this is not working? IS there any other way to
> find out when a chunc has been sent or the bytes uploaded of a file?
> Thanks.
> 

... and I haven't tried this myself, but you should be able to subclass
the builtin file object and prepare your own read() method.  Something
like

class ProgressFile(file):

def read(self, size = None):
print '.',

if size is not None:
return file.read(self, size)
else:
return file.read()

May need some tweaking.. then store the file as

ftp.storbinary("STOR " + file, ProgressFile(file, "rb"), 1024)

Give it a try..

-m

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


Re: FTP status problems. (Again)

2005-09-16 Thread marduk
On Sat, 2005-09-17 at 04:42 +, marduk wrote:
> 
> ... and I haven't tried this myself, but you should be able to subclass
> the builtin file object and prepare your own read() method.  Something
> like
> 
> class ProgressFile(file):
> 
> def read(self, size = None):
> print '.',
> 
> if size is not None:
> return file.read(self, size)
> else:
> return file.read()
> 
> May need some tweaking.. then store the file as
> 
> ftp.storbinary("STOR " + file, ProgressFile(file, "rb"), 1024)
> 
> Give it a try..
> 
> -m
> 

I corrected some errors and made some modifications to my previous post:

class ProgressFile(file):
def read(self, size = None):
from sys import stdout

if size is not None:
buff = file.read(self, size)
if buff:
stdout.write('.')
else:
stdout.write('\n')
return buff
else:
buff = ''
while True:
new_str = file.read(self, 1024)
stdout.write('.')
if new_str:
buff = buff + new_str
else:
stdout.write('\n')
break
return buff


if __name__ == '__main__':
import sys

fname = sys.argv[1]
f = ProgressFile(fname)
f.read()


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


Re: Is there any advantage to using a main() in python scripts?

2013-12-11 Thread marduk

I would agree with the previous post but also add that I've stopped
calling the main function "main()" and usually give it a more
descriptive name, such as "bake_cookies()" or whatever.  I think that
that makes it clearer what it's doing when used as a library and the 'if
__name__ == '__main__'" already implies that it is the "main" script
function.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Sexism in the Ruby community: how does the Python community manage it?

2013-10-17 Thread marduk


On Wed, Oct 16, 2013, at 11:13 PM, Owen Jacobson wrote:

[...]
> 2. What kind of social pressure can we bring to bear to _keep_ Python's 
> package naming conventions as socially neutral as they are, if and when 
> some high-profile dirtbag decides this language is the best language? 
> How can we apply the same pressures to other parts of the Python 
> community?

I'm sorry, but as a member of the hygenically-challenged community I am
greatly offended by the term "dirtbag".

-a
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Issue with my code

2013-02-05 Thread marduk


On Tue, Feb 5, 2013, at 01:38 PM, maiden129 wrote:
> Hi,
> 
> I'm trying to create this program that counts the occurrences of each
> digit in a string which the user have to enter.
> 
> Here is my code:
> 
> s=input("Enter a string, eg(4856w23874): ")
> s=list(s)
> 
> checkS=['0','1','2','3','4','5','6','7','8','9']
> 
> for i in s:
> if i in checkS:
> t=s.count(i)
> if t>1:
> for k in range(1,t):
> s=s.remove(i)
> print(i, "occurs", t,"times.")
> 
> elif t==1:
> print(i,"occurs 1 time.")
> else: pass
> 
> but it keeps showing this error:
> 
>  t=s.count(i)
> AttributeError: 'NoneType' object has no attribute 'count'

s=s.remove(i) does not return a new list but modifies the list  in
place.

So you probably just want

>>> s.remove(i)

Also, there are various inefficiencies in your code, but that is the
main issue with the AttributeError.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Issue with my code

2013-02-05 Thread marduk


On Tue, Feb 5, 2013, at 04:37 PM, darnold wrote:
> On Feb 5, 2:19 pm, maiden129  wrote:
> > How to reverse the two loops?
> >
> 
> s=input("Enter a string, eg(4856w23874): ")
> 
> checkS=['0','1','2','3','4','5','6','7','8','9']
> 
> for digit in checkS:
> t = s.count(digit)
> if t == 0:
> pass
> elif t == 1:
> print(digit,"occurs 1 time.")
> else:
> print(digit, "occurs", t,"times.")
> 
> 
> >>>
> Enter a string, eg(4856w23874): 23493049weee2039412367
> 0 occurs 2 times.
> 1 occurs 1 time.
> 2 occurs 3 times.
> 3 occurs 4 times.
> 4 occurs 3 times.
> 6 occurs 1 time.
> 7 occurs 1 time.
> 9 occurs 3 times.
> >>>

Although that implementation also scans the string 10 times (s.count()),
which may not be as efficient (although it is happening in C, so perhaps
not).

A better solution involves only scanning the string once.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sub-classing datetime

2013-02-07 Thread marduk


On Thu, Feb 7, 2013, at 10:13 AM, Colin J. Williams wrote:
> I'm just making the transition from 2 to 3 for one module.
> 
> With Python 2.7, I had the benefit of mx datetime, but this is not yet 
> available for Python 3.2.
> 
> I find that the 3.2 datetime is not subclassable, for reasons that were 
> known some years back.
> 
> It would help if there was a note in the docs listing the builtin 
> classes which are not subclassable.
> 
> I am retreating to the use of a function.

What makes you think it's not subclassable?:

import datetime

class MyDateTime(datetime.datetime):
def what_date(self):
print(self)


md = MyDateTime.now()
md.what_date()

Seems to work even in 2.7 (excluding the print function).

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


Re: Best method for inter process communications

2007-07-16 Thread marduk
On Mon, 2007-07-16 at 17:22 +, JamesHoward wrote:
> I am looking for a way of performing inter process communication over
> XML between a python program and something else creating XML data.
> 
> What is the best way of performing this communication?  I could bind a
> socket to localhost and perform the data transfer that way, but that
> seems inefficient due to the addition of TCP/IP or UDP/IP overhead.
> Is there a way to stream data via a custom datastream (I.E. not STDIO,
> STDERR, etc)?

In *nix, you could also use a named pipe for cheap RPC (os.mkfifo()).

-a

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


Re: Break up list into groups

2007-07-16 Thread marduk
On Mon, 2007-07-16 at 14:11 -0700, [EMAIL PROTECTED] wrote:
> I can't seem to find an answer to this question anywhere, but I'm
> still looking. My problem is I have a list of values like this:
> 
> l = [0xF0, 1, 2, 3, 0xF0, 4, 5, 6, 0xF1, 7, 8, 0xF2, 9, 10, 11, 12,
> 13, 0xF0, 14, 0xF1, 15]
> 
> A value with bit 0x80 set delineates the start of a new packet of
> information. What I want to do is to group the packets so that 1, 2, 3
> go with the 1st packet tagged 0xF0, 4 ,5, 6 go with the 2nd packet
> tagged 0xF0, 7 & 8 go with the packet tagged 0xF1 and so on. The
> length of the data associated with each tag can vary. I've already
> written an algorithm to do this but I was wondering if some
> combination of itertools functions could do the job faster?
> 
> Here's what I've done and the expected output of the algorithm:
> 
> def splitIntoGroups(data):
> groups = []
> local = []
> 
> for value in data:
> if 0x80 & value:
> if len(local) > 0:
> groups.append(local)
> 
> local = []
> local.append(value)
> else:
> local.append(value)
> 
> if len(local) > 0:
> groups.append(local)
> 
> return groups
> 
> l = [0xF0, 1, 2, 3, 0xF0, 4, 5, 6, 0xF1, 7, 8, 0xF2, 9, 10, 11, 12,
> 13, 0xF0, 14, 0xF1, 15]
> 
> print splitIntoGroups(l)
> 
> Desired result:
> 
> [[240, 1, 2, 3], [240, 4, 5, 6], [241, 7, 8], [242, 9, 10, 11, 12,
> 13], [240, 14], [241, 15]] 

Assuming you meant '0xF0' instead of '0x80' do you mean any value
>=240 starts a new group?  If so:

groups = []
current = [] # probably not necessary, but as a safety
for i in l:
if i >= 240:
current = []
groups.append(current)
current.append(i)


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


Re: Break up list into groups

2007-07-16 Thread marduk
On Mon, 2007-07-16 at 16:31 -0500, marduk wrote:
> Assuming you meant '0xF0' instead of '0x80' do you mean any value
> >=240 starts a new group?  If so:
> 
> groups = []
> current = [] # probably not necessary, but as a safety
> for i in l:
> if i >= 240:
> current = []
> groups.append(current)
> current.append(i)
> 
> 

Misunderstood... actually that should have read

groups = []
current = [] # probably not necessary, but as a safety
for i in l:
if 240 & i:
current = []
groups.append(current)
current.append(i)

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


Re: In a dynamic language, why % operator asks user for type info?

2007-07-16 Thread marduk
On Mon, 2007-07-16 at 17:33 -0700, Karthik Gurusamy wrote:
> Thanks. The above surprised me as I didn't expect that %s will accept
> 42.
> 
> Looks like the implicit conversion doesn't work the other way.
> 
> >>> '%s' % 42
> '42'
> >>> '%d' % '42'
> Traceback (most recent call last):
>   File "", line 1, in 
> TypeError: int argument required
> >>>
> 
> Looks like %s can be used even when I'm sending non-strings.
> >>> '%s foo %s bar' % (25, 25.34)
> '25 foo 25.34 bar'
> >>>
> 
> So %s seems to serve the multi-type placeholder.

According to the docs: http://docs.python.org/lib/typesseq-strings.html

By design, %s "converts any python object using str()".  OTOH it does
not specify that %d, for example, calls int().


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


Re: A way to re-organize a list

2007-07-19 Thread marduk
On Thu, 2007-07-19 at 15:05 +, beginner wrote:
> Hi Everyone,
> 
> I have a simple list reconstruction problem, but I don't really know
> how to do it.
> 
> I have a list that looks like this:
> 
> l=[ ("A", "a", 1), ("A", "a", 2), ("A", "a", 3), ("A", "b", 1), ("A",
> "b", 2), ("B", "a", 1), ("B", "b", 1)]
> 
> What I want to do is to reorganize it in groups, first by the middle
> element of the tuple, and then by the first element. I'd like the
> output look like this:
> 
> out=[
>[#group by first element "A"
>   [("A", "a", 1), ("A", "a", 2), ("A", "a", 3)], #group by
> second element "a"
>   [ ("A", "b", 1), ("A", "b", 2)], #group by second element
> "b"
>],
>[   #group by first element "B"
>   [("B", "a", 1)],
>   [("B", "b", 1)]
>]
> ]

One way of doing it:

def group_by(i, my_list):
d = {}
for t in my_list:
d[t[i]] = d.get(t[i], []) + [t]

return d.values()


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


Re: space / nonspace

2007-07-22 Thread marduk
On Sun, 2007-07-22 at 22:33 +0200, Peter Kleiweg wrote:
> >>> import re
> >>> s = u'a b\u00A0c d'
> >>> s.split()
> [u'a', u'b', u'c', u'd']
> >>> re.findall(r'\S+', s)
> [u'a', u'b\xa0c', u'd']  
> 

If you want the Unicode interpretation of \S+, etc, you pass the
re.UNICODE flag:

>>> re.findall(r'\S+', s,re.UNICODE)
[u'a', u'b', u'c', u'd']

See http://docs.python.org/lib/node46.html

> 
> This isn't documented either:
> 
> >>> s = ' b c '
> >>> s.split()
> ['b', 'c']
> >>> s.split(' ')
> ['', 'b', 'c', '']

I believe the following documents it accurately:
http://docs.python.org/lib/string-methods.html

If sep is not specified or is None, a different splitting
algorithm is applied. First, whitespace characters (spaces,
tabs, newlines, returns, and formfeeds) are stripped from both
ends. Then, words are separated by arbitrary length strings of
whitespace characters. Consecutive whitespace delimiters are
treated as a single delimiter ("'1 2 3'.split()" returns "['1',
'2', '3']"). Splitting an empty string or a string consisting of
just whitespace returns an empty list.

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


Re: Berkely Db. How to iterate over large number of keys "quickly"

2007-08-02 Thread marduk
On Thu, 2007-08-02 at 19:43 +, lazy wrote:
> I have a berkely db and Im using the bsddb module to access it. The Db
> is quite huge (anywhere from 2-30GB). I want to iterate over the keys
> serially.
> I tried using something basic like
> 
> for key in db.keys()
> 
> but this takes lot of time. I guess Python is trying to get the list
> of all keys first and probbaly keep it in memory. Is there a way to
> avoid this, since I just want to access keys serially. I mean is there
> a way I can tell Python to not load all keys, but try to access it as
> the loop progresses(like in a linked list). I could find any accessor
> methonds on bsddb to this with my initial search.
> I am guessing BTree might be a good choice here, but since while the
> Dbs were written it was opened using hashopen, Im not able to use
> btopen when I want to iterate over the db.
> 

try instead

key = db.firstkey()
while key != None:
# do something with db[key]
key = db.nextkey(key)


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


Re: the one python book

2007-08-04 Thread marduk
On Sat, 2007-08-04 at 15:10 +, Michael Tobis wrote:
> Like most people I eventually plan to read Moby Dick, War and Peace,
> and Lutz's Programming Python. Maybe when I retire. 

LOL. Lutz's Programming Python is actually how I learned Python.

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


Re: How to pop random item from a list?

2006-03-09 Thread marduk
On Thu, 2006-03-09 at 21:59 -0800, flamesrock wrote:
> Hi,
> 
> It's been a while since I've played with python.
> 
> My question is... whats the best way to pop a random item from a list??

import random
# ...

item = mylist.pop(random.randint(0,len(mylist)))


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