Re: Google Maps and Python: creating a map, embedding it, adding images, videos, markers, using python

2014-12-19 Thread Daniel da Silva
Kevin, that client library looks like it is for accessing Google Maps
related services, not modifying maps themselves.



On Fri, Dec 19, 2014 at 1:02 AM, Kev Dwyer kevin.p.dw...@gmail.com wrote:

 Veek M wrote:

  I'm messing with Google-Maps. Is there a way I can create a map, embed it
  on a page (CSS/HTML/Javascript for this bit), and add images, videos,
  markers - using python? Any libraries available?

 Hello,

 Googling for google maps python client returns

 https://developers.google.com/api-client-library/python/apis/mapsengine/v1

 as the first result...

 HTH

 Kev

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

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


Re: piping with subprocess

2014-02-01 Thread Daniel da Silva
Try this:

from subprocess import check_output
import sys
check_output(textutil -convert html %s -stdout | pandoc -f html -t
markdown -o %s % sys.argv[1:3], shell=True)




On Sat, Feb 1, 2014 at 7:19 AM, Rick Dooling rpdool...@gmail.com wrote:

 I spent half a day trying to convert this bash script (on Mac)

 textutil -convert html $1 -stdout | pandoc -f html -t markdown -o $2

 into Python using subprocess pipes.

 It works if I save the above into a shell script called convert.sh and
 then do

 subprocess.check_call([convert.sh, file, markdown_file])

 where file and markdown_file are variables.

 But otherwise my piping attempts fail.

 Could someone show me how to pipe in subprocess. Yes, I've read the doc,
 especially

 http://docs.python.org/2/library/subprocess.html#replacing-shell-pipeline

 But I'm a feeble hobbyist, not a computer scientist.

 Thanks

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

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


Re: Chanelling Guido - dict subclasses

2014-01-15 Thread Daniel da Silva
On Tue, Jan 14, 2014 at 8:27 PM, Steven D'Aprano 
steve+comp.lang.pyt...@pearwood.info wrote:

 But reading Guido, I think he's saying that wouldn't be a good idea. I
 don't get it -- it's not a violation of the Liskov Substitution
 Principle, because it's more restrictive, not less. What am I missing?


Just to be pedantic, this *is* a violation of the Liskov Substution
Principle. According to Wikipedia, the principle states:

 if S is a subtype http://en.wikipedia.org/wiki/Subtype of T, then
 objects of type http://en.wikipedia.org/wiki/Datatype T may be replaced
 with objects of type S (i.e., objects of type S may be *substituted* for
 objects of type T) without altering any of the desirable properties of that
 program (correctness, task performed, etc.) 
 [0]http://en.wikipedia.org/wiki/Liskov_substitution_principle


 Since S (TextOnlyDict) is more restrictive, it cannot be replaced for T
(dict) because the program may be using non-string keys.


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


Re: Python 3 __bytes__ method

2014-01-11 Thread Daniel da Silva
Where did you read this? I can't find any documentation about __bytes__ on
google.

Regards,
Daniel


On Sat, Jan 11, 2014 at 7:24 PM, Ethan Furman et...@stoneleaf.us wrote:

 Python 3 has a new method, __bytes__.  The docs say: Called by bytes() to
 compute a byte-string representation of an object. This should return a
 bytes object.

 I must admit I'm not entirely clear how this should be used.  Is anyone
 using this now?  If so, how?

 --
 ~Ethan~
 --
 https://mail.python.org/mailman/listinfo/python-list

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


Re: Python 3 __bytes__ method

2014-01-11 Thread Daniel da Silva
One use case is:
Suppose you have existing function that accepts a *bytes* object. If you
subclass *bytes* and want it to be guaranteed to work with that function,
you can override* __bytes__()* to use the logistics of your subclass
implementation.


On Sat, Jan 11, 2014 at 7:56 PM, Ethan Furman et...@stoneleaf.us wrote:

 On 01/11/2014 04:53 PM, Daniel da Silva wrote:


 Where did you read this? I can't find any documentation about __bytes__
 on google.


 http://docs.python.org/3.3/reference/datamodel.html?
 highlight=__bytes__#object.__bytes__


 --
 ~Ethan~
 --
 https://mail.python.org/mailman/listinfo/python-list

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


Re: Python 3 __bytes__ method

2014-01-11 Thread Daniel da Silva
On Sat, Jan 11, 2014 at 9:44 PM, Ethan Furman et...@stoneleaf.us wrote:

 On 01/11/2014 06:19 PM, Daniel da Silva wrote:


 One use case is:
 Suppose you have existing function that accepts a /bytes/ object. If you
 subclass /bytes/ and want it to be guaranteed
 to work with that function, you can override/__bytes__()/ to use the
 logistics of your subclass implementation.


 I don't think so, for two reasons:

 1) bytes objects do not have a __bytes__ method,

 2) if the function is expecting a bytes object, it is unlikely to call
 bytes() on it.


In general __typename__() methods are for explicit typename(obj)
conversion. There is __int__(), __str__(), etc. They are what is behind
int('3') == 3 and str(4) == '4'. If for no other reason, __bytes__() is
there for symmetry. I agree with you that realistic use cases are hard to
think of.

Does that answer your question better?

All the best,
Daniel
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: random number

2012-03-26 Thread Daniel da Silva
If you want it as an int:
random.randint(10, 99)

Or as a string:
s = '%06d' % random.randint(0, 99)



On Mon, Mar 26, 2012 at 2:08 AM, Nikhil Verma varma.nikhi...@gmail.comwrote:

 Hi All

 How can we generate a 6 digit random number from a given number ?

 eg:-

 def number_generator(id):
 random.randint(id,99)

 When i am using this it is sometimes giving me five digit and sometimes 6
 . I want to avoid encryption . Can i have alphanumeric 6 digit random
 number from this .

 Thanks in advance

 --
 Regards
 Nikhil Verma
 +91-958-273-3156


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


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


Best way to administer code updates to server daemon

2011-01-21 Thread Daniel da Silva
Hi,

I am writing a custom IRC server, and I was wondering would be the
best way to administer code updates to the daemon. Am I doomed to have
to restart the server every time I want to do an update (which would
disconnect all clients)? I don't mind doing something a little more
advanced if it means I can keep close to continuous uptime.

Thanks,
Daniel

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


Re: Parsing string for verb noun

2011-01-12 Thread Daniel da Silva
MRAB,

I will check it out. Thanks!

Daniel

On Tue, Jan 11, 2011 at 10:23 PM, MRAB pyt...@mrabarnett.plus.com wrote:

 On 12/01/2011 01:50, Daniel da Silva wrote:

 Hi,

 I have come across a task where I would like to scan a short 20-80
 character line of text for instances of verb  noun. Ideally
 verb  could be of any tense.

 I know quite a bit of programming and computer science, but
 computational linguistics is relatively new to me. If anyone can point
 me in the right direction, I would be very thankful!

  Have a look at the Natural Language Toolkit:

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

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


Parsing string for verb noun

2011-01-11 Thread Daniel da Silva
Hi,

I have come across a task where I would like to scan a short 20-80 character
line of text for instances of verb noun. Ideally verb could be of
any tense.

I know quite a bit of programming and computer science, but computational
linguistics is relatively new to me. If anyone can point me in the right
direction, I would be very thankful!

Cheers,

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


Parsing string for verb noun

2011-01-11 Thread Daniel da Silva
Hi,

I have come across a task where I would like to scan a short 20-80
character line of text for instances of verb noun. Ideally
verb could be of any tense.

I know quite a bit of programming and computer science, but
computational linguistics is relatively new to me. If anyone can point
me in the right direction, I would be very thankful!

Cheers,

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


Re: list displays

2011-01-08 Thread Daniel da Silva
They're called List Comprehensions
http://docs.python.org/tutorial/datastructures.html#list-comprehensions

http://docs.python.org/tutorial/datastructures.html#list-comprehensions

On Sat, Jan 8, 2011 at 4:57 PM, Olive not0read0...@yopmail.com wrote:

 I am a newbie to python. Python supports what I thinks it is called
 list display, for example:

 [i for i in range(10)]
 [i for i in range(10) if i6]

 Does anyone know a good documentation for this. I have read the
 language reference but it is confusing.

 Olive

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

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


Funny __future__ imports

2010-12-21 Thread Daniel da Silva
from __future__ import space_shuttle
DeprecationWarning: will be removed in next release


Post yours!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Calling FORTAN dll functions from Python

2010-12-08 Thread Daniel da Silva
I don't know much about fortran+python, but I work with someone who does,
and he absolutely loves this tool:
http://cens.ioc.ee/projects/f2py2e/

http://cens.ioc.ee/projects/f2py2e/Daniel

On Tue, Dec 7, 2010 at 10:19 PM, Dennis Lee Bieber wlfr...@ix.netcom.comwrote:

 On Tue, 07 Dec 2010 12:52:54 +0100, Stefan Behnel stefan...@behnel.de
 declaimed the following in gmane.comp.python.general:

  Alex van der Spek, 07.12.2010 12:11:
   Does anyone know how to call functions from FORTRAN dlls in Python? Is
   it even possible?
 
  Sure, have a look at fwrap and Cython.
 
  Stefan

 Possibly ctypes too...
 --
Wulfraed Dennis Lee Bieber AF6VN
wlfr...@ix.netcom.comHTTP://wlfraed.home.netcom.com/

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

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


Re: Man pages and info pages

2010-11-03 Thread Daniel da Silva
Guys, this really has nothing to do with python.

On Wednesday, November 3, 2010, Hrvoje Niksic hnik...@xemacs.org wrote:
 Teemu Likonen tliko...@iki.fi writes:

     Enter   Follow a link (down to node)
     u       up node level
     h       help (general how-to)
     ?       help (commands)
     s       search

 And don't forget:

       l       last viewed page (aka back)

 That one seems to be the info reader's best-kept secret.
 --
 http://mail.python.org/mailman/listinfo/python-list

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


Re: newbie problem with str.replace

2010-08-05 Thread Daniel da Silva
Also,
   for bestandsnaam in dirs and files:
is probably not doing what you want. Use + to concatenate  lists.


Daniel


On Wed, Aug 4, 2010 at 6:30 AM, Mike Kent mrmak...@cox.net wrote:

 On Aug 4, 9:10 am, BobAalsma bob.aal...@aalsmacons.nl wrote:
  I'm working on a set of scripts and I can't get a replace to work in
  the script - please help.

 
 bestandsnaam_nieuw.replace(KLANTNAAM_OUT,KLANTNAAM_IN)

 I'm not sure what you are intending to do here, but string.replace
 does not do its replacement in-place.  It returns a copy of the
 original string, with the replacement done in the copy.  You are not
 assigning the string returned by string.replace to anything,
 therefore, it is immediately thrown away.

 Secondly, and this is just a guess, but since you are doing the
 string.replace inside of an os.walk loop, you appear to be trying to
 do a filename change.  I hope you realize that this will in no way
 change the name of the file *on disk*; it will only change it in
 memory.
 --
 http://mail.python.org/mailman/listinfo/python-list

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


Re: simple (I hope!) problem

2010-08-04 Thread Daniel da Silva
Why not just add the google app engine lib subdirectories to your python
path?


On Tue, Aug 3, 2010 at 3:09 AM, Jean-Michel Pichavant 
jeanmic...@sequans.com wrote:

 samwyse wrote:

 I'm writing for the Google app engine and have stubbed my toe yet
 again on a simple obstacle.  Non-trivial app engines programs require
 the import of several modules that aren't normally in my PYTHONPATH.
 I'd like to be able to test my code outside of the app engine
 framework.  I've tried several solutions in the past that worked but
 weren't particularly elegant or portable.  Now I've had a new idea.
 Here's my latest attempt:

 import os, re
 if __name__ == '__main__':
pass
 else
from google.appengine.ext import webapp
register = webapp.template.create_template_register()

 This works great, except my code makes use of the resister object in
 several places, like this:

 register.filter(emptylines)

 Fortunately, I don't need the functionality of the object, I just want
 something that won't generate an error when I use it.  So, what is the
 quickest way to to create such an object (replacing the 'pass' in my
 first snippet).  My solution is this:

class C:
def filter(self, *args, **kwds):
pass
register = C()

 but it seems like I should be able to do something better, as
 measured by lines of code, faking more than just a 'filter' method, or
 both.  Any ideas?  Thanks!



 here is a class that accepts any method call without generating an error:

 class Stub(object):
   @staticmethod
   def stub(*arg, **kwarg):
   pass
   def __getattribute__(self, name):
   return Stub.stub


 s = Stub()
 s.foo('bar')
 s.bar
 s.bar('', '', 5)


 JM

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

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


Re: Global variables problem

2010-08-04 Thread Daniel da Silva
Your problem lies somewhere in the use of the Process class, not with global
variables.

If you replace your p = ... and p.start() lines with a direct call to
self.handle_connection(), your code works as expected. I don't know much
about the multiprocessing module, so I can't really comment on what you're
doing wrong, but I hope this points you in the right direction.

Sorry I couldn't be of more help,

Daniel


On Tue, Aug 3, 2010 at 9:48 PM, Navkirat Singh navkir...@gmail.com wrote:


 On 04-Aug-2010, at 9:46 AM, Daniel da Silva wrote:

 Please post approximate code that actually works and displays the problem.

 On Tue, Aug 3, 2010 at 9:00 PM, Navkirat Singh navkir...@gmail.comwrote:

 Hey guys,

 I am using a multiprocessing program, where the new process is supposed to
 change a variable in the main class that it branches out from. This is
 somehow not working, following is an approximate code. Would really
 appreciate any insight into this matter:


 var = {}

 class Something():

def set_var(self):
global var
var = somevalue

def get_var(self):
return var

def newprocess(self):
self.set_var()

def do_multiprocessing(self):
while true:
self.get_var()
new_process = process(target=newprocess)
new_process.start()


 I am really confused here !

 Any help would be awesome : )

 Regards,
 Nav

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



 This is a working code, streamlined, but it is where the problem is:

 from multiprocessing import *

 dicts = 0
 print('global ', dicts)

 class WebServer():
  def set_sessionInfo(self):
 global dicts
 dicts = dicts + 1
  def get_sessionInfo(self):
 return dicts

 def handle_connection(self):
 self.set_sessionInfo()
  def serve_forever(self):
 for x in range(10):
 p = Process(target=self.handle_connection)
 p.start()
 print(self.get_sessionInfo())
 ws = WebServer()
 ws.serve_forever()
 print(dicts)



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


Re: Global variables problem

2010-08-03 Thread Daniel da Silva
Please post approximate code that actually works and displays the problem.

On Tue, Aug 3, 2010 at 9:00 PM, Navkirat Singh navkir...@gmail.com wrote:

 Hey guys,

 I am using a multiprocessing program, where the new process is supposed to
 change a variable in the main class that it branches out from. This is
 somehow not working, following is an approximate code. Would really
 appreciate any insight into this matter:


 var = {}

 class Something():

def set_var(self):
global var
var = somevalue

def get_var(self):
return var

def newprocess(self):
self.set_var()

def do_multiprocessing(self):
while true:
self.get_var()
new_process = process(target=newprocess)
new_process.start()


 I am really confused here !

 Any help would be awesome : )

 Regards,
 Nav

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

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


Re: Aaaargh! global name 'eggz' is not defined

2009-10-29 Thread Daniel da Silva
There are several static analysis tools that can check whether a variable
name is used before it is defined.

At my old workplace we used pylint, so I can recommend that:
http://www.logilab.org/857

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


Re: os system command not found

2009-01-14 Thread Daniel da Silva
On Jan 14, 5:25 am, codicedave davide.pi...@gmail.com wrote:
 Hi all!
 I installed a external program called infomap using the classical
 procedure

 ./configure
 make
 sudo make install

  and it works perfectly in Terminal (Os x) using both bash and tcsh
 shell

 admins-macbook-pro-2:~ unil$ infomap-build

 Usage: infomap-build [-w working_dir] [-p param_file]
        [-D var_1=val_1 ... -D var_N=val_N]
        (-s single_corpus_file | -m multi_file_list)
        model_tag

  but when I call it from python using os.system or subprocess.call I
 get the message sh: infomap-build: command not found.

 Do you know why?

 Many thanks

 Davide

Type which infomap-build from the command line to find out where
infomap-build is.
--
http://mail.python.org/mailman/listinfo/python-list


Re: big objects and avoiding deepcopy?

2008-10-28 Thread Daniel da Silva
http://groups.google.com/group/perl.perl6.language/msg/b0cfa757f0ce1cfd?pli=1

: )


On Mon, Oct 27, 2008 at 1:12 PM, [EMAIL PROTECTED] wrote:

 Robert Kern:
  This is similar to implementing Undo functionality in applications.

 In a quite-high-level language (like Python, but not necessarily in
 Python itself) it may become eventually advantageous to add some (even
 limited) built-in form of undo. Both to give a simpler way to
 implement a undo functionality into user-level programs (that is to
 implement the Undo command in a program with GUI), but more
 importantly to help the programmer too in some more general
 programming tasks.

 So it's probably a feature we'll see in the next generation of high-
 level languages, among few other features that today are missing in
 Python (only sometimes missing for performance reasons).

 Bye,
 bearophile
 --
 http://mail.python.org/mailman/listinfo/python-list




-- 
Daniel da Silva
(240) 678 - 4686
GSFC, GES-DISC 610.2
University of Maryland
--
http://mail.python.org/mailman/listinfo/python-list


Re: Plotting libraries recommendations

2008-10-14 Thread Daniel da Silva
Seconded. If you are familiar with Matlab plotting at all, the interface is
nearly identical, and the graphics are great.

Daniel

On Tue, Oct 14, 2008 at 9:19 AM, eliben [EMAIL PROTECTED] wrote:

 On Oct 14, 1:18 pm, sert [EMAIL PROTECTED] wrote:
  I'm developing a circuit simulation application and will need to
  plot the output of my program. The output will not be functions,
  just a collection of numerical values of the dependent and
  independent variables.

 One good option is matplotlib. It's easy to install and use on all the
 major platforms.

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

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


Re: default value in __init__

2008-10-14 Thread Daniel da Silva
I would just like to add to the discussion that 4as a second year computer
science undergraduate and I noticed the same issue with default arguments,
but I was able to figure out the system of how (and why) python does it the
way it does, without having to consult any documentation offline or online.
If the person notices that arg={} is yielding an unexpecting result, the
next step I would guess they would PROBABLY try setting arg=None and insert
if arg==None: arg={} at the head of their method. It doesn't take a
tremendous amount of cleverness power to try an alternate method that is
logically equivalent to the intention.


Daniel

On Tue, Oct 14, 2008 at 10:25 PM, Dennis Lee Bieber
[EMAIL PROTECTED]wrote:

 On Tue, 14 Oct 2008 20:50:13 +0200, Bruno Desthuilliers
 [EMAIL PROTECTED] declaimed the following in
 comp.lang.python:

 
  As far as I'm concerned, I started with the FineManual(tm)'s tutorial.
 
 Heh... whereas my introduction was the original O'Reilly Python
 text
 book, with a copy of the Amiga Python on disk... By the end of the week,
 I'd mashed together a rudimentary mail send utility in Python using
 ARexx for the client to spool queuing (needed as, at that time, Amiga
 email clients were very raw -- they required stand-alone external POP3
 and SMTP programs to work with spool directories... And the first
 program I used for SMTP created a message per destination address and
 would block the entire outgoing spool if it couldn't connect to any
 particular address... The second program I tried did relay via ISP
 server -- but failed to transfer CC and BCC destinations!)

  I'm afraid I have to say this is *totally* utopic - except perhaps for
  the most braindead and limited language one could imagine. Heck, even
  Java - which has explicitely been designed for morons^Mdummies - would
  fail this test. How could a few examples provide complete
  understanding of attribute lookup rules, the descriptor protocol,
  metaclasses, iterators, and generator expression - just to name a few ?
 
 As far as I'm concerned, Java fails it even for experienced
 programmers... There is just too much overhead in just doing hello
 world that one NEEDS something like Eclipse just to start coding.

If one wants to avoid reading documentation, my suggestion would be
 to install a version of old KK BASIC, circa 1970.
 --
WulfraedDennis Lee Bieber   KD6MOG
[EMAIL PROTECTED]   [EMAIL PROTECTED]
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff:   [EMAIL PROTECTED])
HTTP://www.bestiaria.com/
 --
 http://mail.python.org/mailman/listinfo/python-list




-- 
Daniel da Silva
(240) 678 - 4686
GSFC, GES-DISC 610.2
University of Maryland
--
http://mail.python.org/mailman/listinfo/python-list


Re: Overloaded Functions

2008-07-30 Thread Daniel da Silva
With a little hacking, you might be able to do something like this:

@overload(f, (int, int, str))
def f1(x, y, z):
pass

@overload(f, (str, str))
def f2(x, y):
pass


The way I would typically do method overloading would be as follows
(this has been tested):

class Person:
def __init__(*args):
argTypes = tuple(map(type,args))

methods = {
(str,int) : Person.initAllInfo,
(str,): Person.initOnlyName,
}

methods[argTypes[1:]](*args)

# 

def initAllInfo(self, name, age):
self.name = name
self.age  = age

def initOnlyName(self, name):
self.name = name
self.age  = 100


With this overload-dictionary approach, it may be possible to
elegantly implement the overloading decorator I described at the top
of this email. I hoped this helped.

Daniel


On Tue, Jul 29, 2008 at 12:05 PM, Tim Henderson [EMAIL PROTECTED] wrote:
 Yes i am aware of that but I want the code to be self documenting, so
 the intent is clear. I actually have an implementation using that
 style which you suggest. I would like cleaner style, like the one i
 suggested in my first post.

 Cheers
 Tim Henderson
 --
 http://mail.python.org/mailman/listinfo/python-list

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


Re: How can I check nbr of cores of computer?

2008-07-30 Thread Daniel da Silva
Single line using /proc/cpuinfo:

numprocs = [ int(line.strip()[-1]) for line in open('/proc/cpuinfo', 'r') if \
  line.startswith('processor') ][-1] + 1


On Wed, Jul 30, 2008 at 2:16 PM, Dan Upton [EMAIL PROTECTED] wrote:

 On Wed, Jul 30, 2008 at 2:22 PM, John Nagle [EMAIL PROTECTED] wrote:
  defn noob wrote:
 
  How can I check how many cores my computer has?
  Is it possible to do this in a Python-app?
 
 Why do you care?  Python can't use more than one of them at
  a time anyway.

 Per Python process, but you might fork multiple processes and want to
 know how many cores there are to know how many to fork, and which
 cores to pin them to.  (I don't know if there's a direct way in Python
 to force it to a certain core, so I instead just wrote an extension to
 interface with sched_setaffinity on Linux.)

 On Linux, an almost assuredly non-ideal way to find out the number of
 cores is to read /proc/cpuinfo and look for the highest-numbered
 processor:  line (and add 1).
 --
 http://mail.python.org/mailman/listinfo/python-list
--
http://mail.python.org/mailman/listinfo/python-list


Re: static variables in Python?

2008-07-29 Thread Daniel da Silva
This is the solution I suggest. It is fairly trivial, and works by
introducing the self.static namespace for a class's static
variables, in contrast to self for the class's instance variables.

 ---

class Static(object):  pass
personStatic = Static()

class Person:
static = personStatic

def __init__(self, name, age):
self.name   = name
self.age= age

def setVersion(self, version):
self.static.version = version

def getVersion(self):
return self.static.version
 ---

Daniel

On Tue, Jul 29, 2008 at 4:40 PM, kj [EMAIL PROTECTED] wrote:


 Yet another noob question...

 Is there a way to mimic C's static variables in Python?  Or something
 like it?  The idea is to equip a given function with a set of
 constants that belong only to it, so as not to clutter the global
 namespace with variables that are not needed elsewhere.

 For example, in Perl one can define a function foo like this

 *foo = do {
  my $x = expensive_call();
  sub {
return do_stuff_with( $x, @_ );
  }
 };

 In this case, foo is defined by assigning to it a closure that has
 an associated variable, $x, in its scope.

 Is there an equivalent in Python?

 Thanks!

 kynn
 --
 NOTE: In my address everything before the first period is backwards;
 and the last period, and everything after it, should be discarded.
 --
 http://mail.python.org/mailman/listinfo/python-list

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


Re: iterating by twos

2008-07-29 Thread Daniel da Silva
The following method is similar to the others provided, but yields an
index value as well (similar to the enumerate(iterable) function).
This is useful in some (but not all) situations.

If the iterable object's iterator returns an even number of items then
you're fine; otherwise it will throw away the last item because it
doesn't have a full pair to return.

-
import itertools

def enumeratePairs(x):
it = iter(x)

for i in itertools.count():
p = it.next()
q = it.next()
yield i, (p,q)

raise StopIteration
--
 for v in enumeratePairs(hello i am daniel):
... print v
...
(0, ('h', 'e'))
(1, ('l', 'l'))
(2, ('o', ' '))
(3, ('i', ' '))
(4, ('a', 'm'))
(5, (' ', 'd'))
(6, ('a', 'n'))
(7, ('i', 'e'))
---

On Tue, Jul 29, 2008 at 1:36 PM, kj [EMAIL PROTECTED] wrote:



 Is there a special pythonic idiom for iterating over a list (or
 tuple) two elements at a time?

 I mean, other than

 for i in range(0, len(a), 2):
frobnicate(a[i], a[i+1])

 ?

 I think I once saw something like

 for (x, y) in forgotten_expression_using(a):
frobnicate(x, y)

 Or maybe I just dreamt it!  :)

 Thanks!
 --
 NOTE: In my address everything before the first period is backwards;
 and the last period, and everything after it, should be discarded.
 --
 http://mail.python.org/mailman/listinfo/python-list

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


Re: You, spare time and SyntaxError

2008-07-10 Thread Daniel da Silva
I applaud your creativity. Very nice.

On Wed, Jul 9, 2008 at 10:56 AM, [EMAIL PROTECTED] wrote:

 def ine(you):
yourself = what?
go = list(something), list(anything)
be = something
please = be, yourself
yourself = great
for good in yourself:
if you is good:
good in you
please.add(more, good)
else:
def inition(lacks, clarity):
if clarity not in you:
please.remove(everything and go)
for bad in yourself:
list(bad) if bad else (ignore, yourself)
try:
(to, escape, your, fate, but)
except (Exception), son:
if bad in (you, son):
(you is bad, son), so
finally:
if bad in you:
lie, cheat, steal, be, bad
else:
print you, is, yourself
you is good and yourself is not bad
please, go

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

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

Re: this is simple...

2008-06-28 Thread Daniel da Silva
ToshiBoy,

You might want to take a look at the filter() function, it can also be used
for the kind of the thing you're doing.

http://docs.python.org/tut/node7.html#SECTION00713

 B = range(1,27)
 def test(b):
... if b*b in B:
... return True
... else:
... return False
...
 A = filter(test, B)
 A
[1, 2, 3, 4, 5]

Daniel

On Sat, Jun 28, 2008 at 1:00 AM, ToshiBoy [EMAIL PROTECTED] wrote:

 On Jun 28, 2:48 pm, Mel [EMAIL PROTECTED] wrote:
  ToshiBoy wrote:
   I have two lists A and B that are both defined as range(1,27) I want
   to find the entries that are valid for A = BxB
  [ ... ]
   I get, as expected 1,4,9,16,25 printed out being the only members of B
   where the condition is true, but when I print B I get:
 
   [1, 2, 3, 4, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25]
 
   1 to 5 is correct, but why doesn't the remove method remove 7 and
   above? What am I doing wrong here?
 
  Try this:
 
  A = range(1,27)
  B = range(1,27)
  C = []
 
  for b in B:
  print Trying, b
  if b*b in A:
  print b
  C.append (b)
  else:
  print Removing, b
  B.remove(b)
  print 'B', B
  print 'C', C
 
  The essential problem is that your `B.remove`s are pulling the rug out
 from
  under your `for b in B:`.  There are ways to mess with B while you
 iterate.
  Running though B backwards will do: `for b in B[::-1]:`, or iterating
 over
  a copy of B: `for b in B[:]:` or `for b in list(B):`.  Leaving B alone
 and
  building up the desired items in C is probably simplest.
 
  Mel.

 Thank you, of course! :-) Didn't even think of that... that I was
 modifying my iterators...

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

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