ctypes LoadLibrary search path

2011-08-05 Thread Santoso Wijaya
Hi,

Can anyone enlighten me to the search path mechanism in ctypes' LoadLibrary?
I have a DLL that resides elsewhere that is not in any default search path.
I tried adding the path to `sys.path` before attempting to load said DLL but
I still get "WindowsError: [Error 126] The specified module could not be
found."

Thanks,

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


Re: ctypes LoadLibrary search path

2011-08-05 Thread Santoso Wijaya
Nevermind. I figured it out (d'oh!).

I had to append to `os.environ['PATH']`, not `sys.path`!


~/santa


On Fri, Aug 5, 2011 at 2:13 PM, Santoso Wijaya wrote:

> Hi,
>
> Can anyone enlighten me to the search path mechanism in ctypes'
> LoadLibrary? I have a DLL that resides elsewhere that is not in any default
> search path. I tried adding the path to `sys.path` before attempting to load
> said DLL but I still get "WindowsError: [Error 126] The specified module
> could not be found."
>
> Thanks,
>
> ~/santa
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: subclass urllib2

2011-02-28 Thread Santoso Wijaya
1. Why are you subclassing a module?
2. If you want to "mask" a module's version attribute, just do this:
>>> import urllib2
>>> urllib2.__version__ = 'foo'
>>> print urllib2.__version__
foo

~/santa


On Mon, Feb 28, 2011 at 10:53 AM, monkeys paw  wrote:

> I'm trying to subclass urllib2 in order to mask the
> version attribute. Here's what i'm using:
>
> import urllib2
>
> class myURL(urllib2):
>def __init__(self):
>urllib2.__init__(self)
>self.version = 'firefox'
>
> I get this>
> Traceback (most recent call last):
>  File "", line 1, in 
> TypeError: Error when calling the metaclass bases
> module.__init__() takes at most 2 arguments (3 given)
>
> I don't see where i am supplying 3 arguments. What am i
> missing?
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Darwin build error with MacPorts Python 2.7.1 installed.

2011-03-02 Thread Santoso Wijaya
Hi,

I recently tried to play around with the latest source for Python-2.7.1, and
I came across this build error while trying to build a clean checkout (after
`./configure`, of course):

bash-3.2$ make
./Parser/asdl_c.py -h ./Include ./Parser/Python.asdl
Traceback (most recent call last):
  File
"/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.py",
line 553, in
main()
  File
"/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.py",
line 535, in main
known_paths = addusersitepackages(known_paths)
  File
"/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.py",
line 268, in addusersitepackages
user_site = getusersitepackages()
  File
"/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.py",
line 243, in getusersitepackages
user_base = getuserbase() # this will also set USER_BASE
  File
"/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.py",
line 233, in getuserbase
USER_BASE = get_config_var('userbase')
  File
"/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sysconfig.py",
line 535, in get_config_var
return get_config_vars().get(name)
  File
"/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sysconfig.py",
line 434, in get_config_vars
_init_posix(_CONFIG_VARS)
  File
"/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sysconfig.py",
line 313, in _init_posix
raise
IOError(msg) IOError: $MACOSX_DEPLOYMENT_TARGET mismatch: now "10.4" but
"10.6" during configure
make: *** [Include/Python-ast.h] Error 1

I am using MacPorts with the latest updates. Any thoughts?

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


Re: making a class callable

2011-03-03 Thread Santoso Wijaya
Are you missing an import?

import ohYeah
f = ohYeah.foo('wow')
...

~/santa


On Thu, Mar 3, 2011 at 5:45 PM, dude  wrote:

> I've been struggling with getting my class to behave the way I want
> it.
>
> I have python module called ohYeah.py, defined as follows...
> #File Begin
> class foo:
>
>def __init__(self, arg1):
>print arg1
>self.ohYeah = arg1
>
>def whatwhat(self):
>return self.ohYeah
> #EOF
>
> My goal is to be able to instantiate the class foo from another python
> module, like so:
>
> # Example Usage
> f = foo("wow")
> j = foo("amazing")
> f.whatwhat()
> wow
> j.whatwhat()
> amazing
> #
>
> However, I always get the "module not callable" error.  After entering
> a "def __call__" method in class foo, still get the same problem.  Can
> someone please point me in the right direction for being able to
> achieve the Example Usage above?  I'm sure there is something trivial
> I'm missing, but after digging around online for a day, I couldn't
> find the missing piece.  Thanks in advance.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: import python module from C++ code

2011-03-04 Thread Santoso Wijaya
Have you read the doc [1] on extending/embedding Python?

~/santa

[1] http://docs.python.org/extending/

On Fri, Mar 4, 2011 at 8:27 AM, Arthur Mc Coy <1984docmc...@gmail.com>wrote:

> Hi all,
>
>
> I have a C++ application. I have a .cpp file which is not a main
> program, but a class where I want to call python script
> (doSomething.py file).
>
> I'm using embed python like in a tutorial here:
> http://www.codeproject.com/KB/cpp/embedpython_1.aspx
>
> But the tutorial is bad. It does not explain howto create python
> module which they call in their example. doSomething.py file contains
> two classes, one of them I use externally (its functions).
>
> So I need to PyImport_Import(py_module) by name of this py file, but
> when I try to do that it fails. Please, give me some examples.
>
>
> If needed, I can attach my code as well.
> Thank you, waiting for anybody's response!
> Be happy.
>
> Arthur
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: import python module from C++ code

2011-03-04 Thread Santoso Wijaya
Extending an embedded Python is not that much different than extending
Python proper. There's even this section [1] in that documentation,
conveniently titled, "Extending Embedded Python."

~/santa

[1]
http://docs.python.org/extending/embedding.html#extending-embedded-python

On Fri, Mar 4, 2011 at 9:14 AM, Arthur Mc Coy <1984docmc...@gmail.com>wrote:

> Yes, I did. Here the link
> http://docs.python.org/extending/extending.html#providing-a-c-api-for-an-extension-module
>
> It does not cover .py file embeding. So it is not my case.
>
>
> On Fri, Mar 4, 2011 at 7:03 PM, Santoso Wijaya 
> wrote:
>
>> Have you read the doc [1] on extending/embedding Python?
>>
>> ~/santa
>>
>> [1] http://docs.python.org/extending/
>>
>> On Fri, Mar 4, 2011 at 8:27 AM, Arthur Mc Coy <1984docmc...@gmail.com>wrote:
>>
>>> Hi all,
>>>
>>>
>>> I have a C++ application. I have a .cpp file which is not a main
>>> program, but a class where I want to call python script
>>> (doSomething.py file).
>>>
>>> I'm using embed python like in a tutorial here:
>>> http://www.codeproject.com/KB/cpp/embedpython_1.aspx
>>>
>>> But the tutorial is bad. It does not explain howto create python
>>> module which they call in their example. doSomething.py file contains
>>> two classes, one of them I use externally (its functions).
>>>
>>> So I need to PyImport_Import(py_module) by name of this py file, but
>>> when I try to do that it fails. Please, give me some examples.
>>>
>>>
>>> If needed, I can attach my code as well.
>>> Thank you, waiting for anybody's response!
>>> Be happy.
>>>
>>> Arthur
>>> --
>>> http://mail.python.org/mailman/listinfo/python-list
>>>
>>
>>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What do I need to know in order to write a web application in python?

2011-03-04 Thread Santoso Wijaya
First, learn the language. Second, browse a multitude of popular web
frameworks written in Python, and try your hands on a few of them. I'd
suggest looking into django, pyramid, webpy, ... (others will fill in).

~/santa


On Fri, Mar 4, 2011 at 12:08 PM, ErichCart ErichCart wrote:

> I am currently a Computer Science student, I can write in pascal, C,
> and Java, and recently I learned about Python and fell in love with
> it. I watched some python programming tutorials on youtube, and now I
> can write some programs.
> But what I really want to do is to make a website where people can
> play real-time RISK game online with other players.
>
> Can this be done in Python 3.2?
> What do I need to know in order to make such a website? Which Python
> modules will I need to use?
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What do I need to know in order to write a web application in python?

2011-03-04 Thread Santoso Wijaya
Django is excellent as a CMS builder (think blogs, articles websites),
though it has many uses beyond that, as well. If, ultimately, you want
client-side interactivity, however, you'd have to look into (perhaps in
addition to Python--it can provide some of the backend logic for the website
you want to build) client-side technologies like Flash or JavaScript.

~/santa


On Fri, Mar 4, 2011 at 12:34 PM, ErichCart ErichCart wrote:

> It is just that I want to better my python skills by doing this.
>
> I have heard about Django, can't this be done with Django?
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: having both dynamic and static variables

2011-03-04 Thread Santoso Wijaya
>
> Declaring the *type* of such variables is a different matter I think (and
> probably is not considered 'pythonic'; certainly it's a crude, if effective,
> way of getting extra performance).


I concur. Especially given performance is not a primary goal of Python to
begin with, and--if such a bottleneck can be located--an extension module
can be written to minimize it, anyway.

~/santa


On Fri, Mar 4, 2011 at 1:13 PM, BartC  wrote:

>
> "Steven D'Aprano"  wrote in message
> news:4d6f26a5$0$30003$c3e8da3$54964...@news.astraweb.com...
>
>  On Wed, 02 Mar 2011 19:45:16 -0800, Yingjie Lan wrote:
>>
>>  Hi everyone,
>>>
>>> Variables in Python are resolved dynamically at runtime, which comes at
>>> a performance cost. However, a lot of times we don't need that feature.
>>> Variables can be determined at compile time, which should boost up
>>> speed.
>>>
>> [...]
>>
>> This is a very promising approach taken by a number of projects.
>>
>> Cython and Pyrex are compilers that take Python-like code with static
>> type declarations and use it to produce compiled C code.
>>
>
> I got the impression the OP was talking about simply pinning down certain
> variables, so that a runtime name lookup (if that's in fact what Python
> does) was not necessary.
>
> Declaring the *type* of such variables is a different matter I think (and
> probably is not considered 'pythonic'; certainly it's a crude, if effective,
> way of getting extra performance).
>
> --
> Bartc
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What do I need to know in order to write a web application in python?

2011-03-04 Thread Santoso Wijaya
GUI application is a whole other matter entirely. For that, you might want
to look into cross-platform GUI toolkits with Python bindings. E.g., PyQT
[1], wxPython [2], TkInter [3], etc. Python, after all, is just a
cross-platform language. To do all those user-friendly bells and whistles,
you'd need to use the appropriate framework(s) that Python can work with.

~/santa

[1] http://wiki.python.org/moin/PyQt
[2] http://www.wxpython.org/
[3] http://wiki.python.org/moin/TkInter


On Fri, Mar 4, 2011 at 1:48 PM, ErichCart ErichCart wrote:

> By real-time, I mean that I want it to be similar to the way instant
> online chess works. Something like here: instantchess.com, but for
> RISK.
>
> I thought about making such an application, and now that I want to
> practice python I thought that perhaps it can be done with python.
> Now after your answers it seems like a hard task not suitable for a
> beginner.
>
> But really, I just want to make something useful with python. All I
> can do now, is console programs, and that doesn't seem very user
> friendly.
>
> In fact this doesn't necessary need to be web application. For example
> I have a friend who uses Delphi, and he can create all sorts of
> windows applications easily, like he can see the window on the screen
> and he can place buttons, text fields, radio buttons etc. wherever he
> wants and then program the actions of each element. I was able to do
> the same with visual basic in one of my university classes.
>
> What do I need to know in order to be able to do the same with python?
> Which python modules/python development environments do I need to use?
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Numerical representation

2011-03-04 Thread Santoso Wijaya
Have you taken a look at numpy? [1] It was written for exactly this kind of
usage.

~/santa

[1] http://numpy.scipy.org/


On Fri, Mar 4, 2011 at 2:32 PM, Jon Herman  wrote:

> Hello all,
>
> I am new to the Python language and writing a Runge-Kutta-Fellberg 7(8)
> integrator in Python, which requires an extreme numerical precision for my
> particular application. Unfortunately, I can not seem to attain it.
> The interesting part is if I take my exact code and translate it to Matlab
> code (so I use the exact same process and numbers), I get a far superior
> precision (the one I am expecting, in fact). This leads me to think I need
> to call a certain command in my Python script in order to make sure no
> truncation errors are building up over my integration.
>
> Has anyone had similar problems? Is there a difference between how Matlab
> and Python store numbers, and if so how do I make Python more accurate?
>
> I know there is a lot of packages out there, but this in fact overwhelmed
> me a little bit and seems to prevent me from finding the answer to my
> question, so I'm hoping someone with more experience will be able to
> enlighten me!
>
> Best regards,
>
> Jon
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: having both dynamic and static variables

2011-03-05 Thread Santoso Wijaya
A function object can get bound to a name, too:

def foo(x):
return x + 1

foo = lambda x: x - 1
assert foo(1) == 0

~/santa


On Sat, Mar 5, 2011 at 7:46 PM, Corey Richardson  wrote:

> On 03/05/2011 10:23 PM, MRAB wrote:
> > Having a fixed binding could be useful elsewhere, for example, with
> > function definitions:
> > [..]
> >  fixed PI = 3.1415926535897932384626433832795028841971693993751
> >
> >  fixed def squared(x):
> >  return x * x
>
> This question spawns from my ignorance: When would a functions
> definition change? What is the difference between a dynamic function and
> a fixed function?
>
> --
> Corey Richardson
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: having both dynamic and static variables

2011-03-05 Thread Santoso Wijaya
Shouldn't this go to python-ideas?
Anyway, I'm partial to "static".


~/santa


On Sat, Mar 5, 2011 at 8:33 PM, Westley Martínez  wrote:

> On Sat, 2011-03-05 at 18:37 -0800, John Nagle wrote:
> > On 3/2/2011 9:27 PM, Steven D'Aprano wrote:
> > > On Wed, 02 Mar 2011 19:45:16 -0800, Yingjie Lan wrote:
> > >
> > >> Hi everyone,
> > >>
> > >> Variables in Python are resolved dynamically at runtime, which comes
> at
> > >> a performance cost. However, a lot of times we don't need that
> feature.
> > >> Variables can be determined at compile time, which should boost up
> > >> speed.
> > > [...]
> > >
> > > This is a very promising approach taken by a number of projects.
> >
> > It's worth having some syntax for constants.  I'd suggest
> > using "let":
> >
> >   let PI = 3.1415926535897932384626433832795028841971693993751
> >
> > I'd propose the following semantics:
> >
> > 1.  "let" creates an object whose binding is unchangeable.  This
> >  is effectively a constant, provided that the value is immutable.
> >  A compiler may treat such variables as constants for optimization
> >  purposes.
> >
> > 2.  Assignment to a a variable created with "let" produces an error
> >  at compile time or run time.
> >
> > 3.  Names bound with "let" have the same scope as any other name
> >  created in the same context.  Function-local "let" variables
> >  are permitted.
> >
> > 4.  It is an error to use "let" on a name explicitly made "global",
> >  because that would allow access to the variable before it was
> >  initialized.
> >
> > This is close to the semantics of "const" in C/C++, except that
> > there's no notion of a const parameter.
> >
> > "let" allows the usual optimizations - constant folding, hoisting
> > out of loops, compile time arithmetic, unboxing, etc.  Ordinarily,
> > Python compilers have to assume that any variable can be changed
> > at any time from another thread, requiring worst-case code for
> > everything.
> >
> >   John Nagle
> I'm against constants, for the purpose of "programmers should be smart
> enough to not set a variable to another value that should be static",
> but if Python were to have constants I think it would be better to use
> something more descriptive than 'let'. Also, because the defined
> constant is static, I think it would be better to use 'is' instead of
> '='. Example:
>
> constant x is 5
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: having both dynamic and static variables

2011-03-07 Thread Santoso Wijaya
Now you're just muddying the terminology!

~/santa


On Mon, Mar 7, 2011 at 1:20 PM, Paul Rubin  wrote:

> Steven D'Aprano  writes:
> > but I call that a feature, not a bug. If you want an immutable constant,
> > use a tuple, not a list.
>
> Nope:
>
>L = ([1,2],[3,4])  # tuple
>L[0].append(5) # mutate L, in some reasonable sense of "mutate"
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Defining class attributes + inheritance

2011-03-08 Thread Santoso Wijaya
Here's how you do inheritance:

C:\>python
Python 2.7.1 (r271:86832, Nov 27 2010, 17:19:03) [MSC v.1500 64 bit (AMD64)]
on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> class BaseClass(object):
... def __init__(self, a, b, c, d):
... self.a = a
... self.b = b
... self.c = c
... self.d = d
...
>>> class NewClass(BaseClass):
... def __init__(self, a, b, c, d, new):
... super(NewClass, self).__init__(a, b, c, d)
... self.new = new
...
>>> A = BaseClass(1,2,3,4)
>>> print A.a, A.b, A.c, A.d
1 2 3 4
>>> B = NewClass(1,2,3,4,5)
>>> print B.a, B.b, B.c, B.d, B.new
1 2 3 4 5
>>>

~/santa


On Tue, Mar 8, 2011 at 5:00 PM, Martin De Kauwe  wrote:

> On Mar 9, 11:50 am, "Rhodri James" 
> wrote:
> > On Wed, 09 Mar 2011 00:29:18 -, Martin De Kauwe 
>
> > wrote:
> >
> >
> >
> >
> >
> >
> >
> > > On Mar 9, 10:20 am, Ethan Furman  wrote:
> > [snip]
> > >> Just make sure and call the parent's constructor, either with
> >
> > >> class NewClass(BaseClass):
> > >>  def __init__(self, ):
> > >>  BaseClass.__init__(self, other_params)
> >
> > >> or
> >
> > >> class NewClass(BaseClass):
> > >>  def __init__(self, ):
> > >>  super(NewClass, self).__init__()
> >
> > >> ~Ethan~
> >
> > > Hi thanks, but I think I am implementing it wrong then?
> >
> > > BaseClass has 4 attributes and when I tried what you said
> >
> > > class NewClass(BaseClass):
> > > def __init__(self):
> > > super(NewClass, self).__init__(new_thing)
> >
> > > I get the error
> >
> > > TypeError: __init__() takes exactly 1 argument (6 given)
> >
> > Please give us either the rest of the code or the rest of the
> > traceback, or preferably both.  Without one or the other we have
> > little hope of guessing what you've typed.
> >
> > --
> > Rhodri James *-* Wildebeest Herder to the Masses
>
> OK
>
> class BaseClass(object):
>
>def __init__(self, a, b, c, d):
>self.a = a
>self.b = b
>self.c = c
>self.d = d
>
>
> class NewClass(BaseClass):
>def __init__(self):
> super(NewClass, self).__init__(new)
>self.new = new
>print self.new
>
> class PreviousClass:
>def __init__(self, a, b, c, d, new):
>self.a = a
>self.b = b
>self.c = c
>self.d = d
>self.new = new
>print self.new
>
>
> if __name__ == "__main__":
>
>A = PreviousClass(1, 2, 3, 4, 5)
>B = NewClass(1, 2, 3, 4, 5)
>
> $ python test.py
> Traceback (most recent call last):
>  File "model_data.py", line 29, in 
>B = NewClass(1, 2, 3, 4, 5)
> TypeError: __init__() takes exactly 1 argument (6 given)
>
>
>
> So NewClass is my attempt to implement what I was shown and
> PreviousClass was how I was originally solving the issue, i.e. I
> wouldn't inherit the BaseClass.
>
> thanks
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Defining class attributes + inheritance

2011-03-08 Thread Santoso Wijaya
Remember the mantra, "Explitic is better than implicit." ;-)

~/santa


On Tue, Mar 8, 2011 at 7:15 PM, Martin De Kauwe  wrote:

> On Mar 9, 12:53 pm, "Rhodri James" 
> wrote:
> > On Wed, 09 Mar 2011 01:00:29 -, Martin De Kauwe 
>
> > wrote:
> >
> > > class BaseClass(object):
> > >def __init__(self, a, b, c, d):
> > > self.a = a
> > > self.b = b
> > > self.c = c
> > > self.d = d
> >
> > > class NewClass(BaseClass):
> > > def __init__(self):
> > > super(NewClass, self).__init__(new)
> > > self.new = new
> > > print self.new
> >
> > Two things leap out immediately.  First, BaseClass.__init__ takes four
> > parameters besides `self`, but when you call it you only give it one
> > parameter, `new`.  It's not going to like that.  Second, where did `new`
>
> > come from?  It's not a parameter to NewClass.__init__, and it doesn't
> seem
> > to be a global.  That's not going to work well either.
> >
> > However neither of these things are what the traceback is complaining
> > about.
> >
> > >  if __name__ == "__main__":
> >
> > > A = PreviousClass(1, 2, 3, 4, 5)
> > > B = NewClass(1, 2, 3, 4, 5)
> >
> > > $ python test.py
> > > Traceback (most recent call last):
> > >   File "model_data.py", line 29, in 
> > > B = NewClass(1, 2, 3, 4, 5)
> > > TypeError: __init__() takes exactly 1 argument (6 given)
> >
> > When you create your NewClass, you give it five parameters (1, 2, 3, 4,
> 5)
> > plus the implicit `self`, which is the "(6 given)" part of the message.
> > However NewClass.__init__ takes no parameters aside from `self` (i.e. it
>
> > "takes exactly 1 argument").  There's a mismatch between what you've told
>
> > NewClass.__init__ to expect and what you actually deliver to it.
> >
> > The point that may be confusing you is that NewClass.__init__ knows
> > nothing at all about BaseClass.__init__.  It doesn't inherit parameters
> >  from it or anything magical like that; you have to tell it everything.
>  If
> > you want parameters to pass from NewClass.__init__ to BaseClass.__init__
>
> > you will have to provide them somehow.  In this case, I think what you
> > meant was something like this:
> >
> > class NewClass(BaseClass):
> >  def __init__(self, a, b, c, d, new):
> >  super(NewClass, self).__init__(a, b, c, d)
> > self.new = new
> >  # etc
> >
> > --
> > Rhodri James *-* Wildebeest Herder to the Masses
>
> Yep that was it, I think I misunderstood and assumed it knew about
> stuff in the BaseClass constructor. All makes sense now!
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Guido rethinking removal of cmp from sort method

2011-03-13 Thread Santoso Wijaya
I did not even realize such a change occurred in Python 3. I'm still
currently blissful in Python 2 land. I'd be concerned about the impact in
ported libraries (memory footprint? others?)...

~/santa


On Sun, Mar 13, 2011 at 1:35 PM, Dave Abrahams  wrote:

> Steven D'Aprano  pearwood.info> writes:
>
> > If anyone has any use-cases for sorting with a comparison function that
> > either can't be written using a key function, or that perform really
> > badly when done so, this would be a good time to speak up.
>
> I think it's probably provable that there are no cases in the first
> category,
> provided you're willing to do something sufficiently contorted.  However,
> it also seems self-evident to me that many programmers will rightly chafe
> at the idea of creating and tearing down a bunch of objects just to
> compare things for sorting.  Think of the heap churn!  Even if it turns out
> that Python 3 contains some magic implementation detail that makes it
> efficient most of the time, it goes against a natural understanding of the
> computation model
>
> 2p for y'all.
> -Dave
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Get Path of current Script

2011-03-14 Thread Santoso Wijaya
You can make it a required input with default values to sys.argv when you
start the program. Parse the content of the file given by sys.argv, then
cache it in some global module or pass it around as arguments...

That said, if you still want to go the route of knowing where the current
directory of a given script is, I would do:

import os

HERE = os.path.dirname(os.path.abspath(__file__))
CFGFILE = os.path.join(HERE, 'config.cfg')

~/santa


On Mon, Mar 14, 2011 at 11:25 AM, Alexander Schatten wrote:

> They don't. Hm, ok, I am always for best practices. If there is a
> better way to do it I am open for suggestions ;-) How would the best
> practice be to load configuration data from a file.
>
> I mean, this is something very common: you write a program or a script
> and want to load some configuration data.
>
>
> thanks
>
>
> Alex
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A Question on URLLIB

2011-03-14 Thread Santoso Wijaya
Well, have you run into any problem, expected or otherwise, yet?

~/santa


On Mon, Mar 14, 2011 at 11:10 PM, joy99  wrote:

> Dear Group,
> I am trying to construct a web based crawler with Python and for that
> I am using the URLLIB module, and  by doing
> import urllib and then trying with urllib.urlopen("url).
> Am I going fine?
> If some one can kindly highlight if I am doing any mistake.
> Best Regards,
> Subhabrata Banerjee.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: possible to run a python script without installing python?

2011-03-16 Thread Santoso Wijaya
py2exe does this for you...

~/santa


On Wed, Mar 16, 2011 at 8:02 AM, David Jackson  wrote:

> i need to run a python script on any arbitrary server and don't want to do
> an actual installation. i figured i could do a clean install on my machine
> and install whatever libraries would be needed, then zip them all up for
> remote deployment. to avoid bloating, i am wondering which files i can
> safely omit.
>
>
> On Tue, Mar 15, 2011 at 8:48 PM, Katie T  wrote:
>
>>
>> On Tue, Mar 15, 2011 at 8:58 PM, davidj411  wrote:
>>
>>> it seems that if I copy the python.exe binary and the folders
>>> associated with it to a server without python, i can run python.
>>> does anyone know which files are important to copy and which can be
>>> omitted?
>>>
>>> i know about py2exe and have had no luck with it.
>>
>>
>> What's the reason for wanting to avoid installing Python? - are you just
>> trying to save disk space?
>>
>> If it's a case of not having admin rights, you can just copy the Python
>> directory, I don't believe it has any dependencies anywhere else.
>>
>> Katie
>> --
>> CoderStack
>> http://www.coderstack.co.uk
>> The Software Developer Job Board
>>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Memory Usage of Strings

2011-03-16 Thread Santoso Wijaya
??

Python 2.7.1 (r271:86832, Nov 27 2010, 17:19:03) [MSC v.1500 64 bit (AMD64)]
on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> L = []
>>> for i in xrange(10):
... L.append(str(i) * (1000 / len(str(i
...
>>> sys.getsizeof(L)
824464
>>> L = []
>>> for i in xrange(2):
... L.append(str(i) * (5000 / len(str(i
...
>>> sys.getsizeof(L)
178024
>>>

~/santa


On Wed, Mar 16, 2011 at 11:20 AM, Amit Dev  wrote:

> sum(map(len, l)) =>  8200 for 1st case and 9100 for 2nd case.
> Roughly 100MB as I mentioned.
>
> On Wed, Mar 16, 2011 at 11:21 PM, John Gordon  wrote:
> > In  Amit Dev <
> amit...@gmail.com> writes:
> >
> >> I'm observing a strange memory usage pattern with strings. Consider
> >> the following session. Idea is to create a list which holds some
> >> strings so that cumulative characters in the list is 100MB.
> >
> >> >>> l = []
> >> >>> for i in xrange(10):
> >> ...  l.append(str(i) * (1000/len(str(i
> >
> >> This uses around 100MB of memory as expected and 'del l' will clear
> that.
> >
> >> >>> for i in xrange(2):
> >> ...  l.append(str(i) * (5000/len(str(i
> >
> >> This is using 165MB of memory. I really don't understand where the
> >> additional memory usage is coming from.
> >
> >> If I reduce the string size, it remains high till it reaches around
> >> 1000. In that case it is back to 100MB usage.
> >
> > I don't know anything about the internals of python storage -- overhead,
> > possible merging of like strings, etc.  but some simple character
> counting
> > shows that these two loops do not produce the same number of characters.
> >
> > The first loop produces:
> >
> > Ten single-digit values of i which are repeated 1000 times for a total of
> > 1 characters;
> >
> > Ninety two-digit values of i which are repeated 500 times for a total of
> > 45000 characters;
> >
> > Nine hundred three-digit values of i which are repeated 333 times for a
> > total of 299700 characters;
> >
> > Nine thousand four-digit values of i which are repeated 250 times for a
> > total of 225 characters;
> >
> > Ninety thousand five-digit values of i which are repeated 200 times for
> > a total of 1800 characters.
> >
> > All that adds up to a grand total of 20604700 characters.
> >
> > Or, to condense the above long-winded text in table form:
> >
> > range num digits 1000/len(str(i))  total chars
> > 0-910 1  10001
> > 10-99  90 2   50045000
> > 100-999   900 3   333   299700
> > 1000-9000 4   250  225
> > 1-9 9 5   200 1800
> >  
> >  grand total chars   20604700
> >
> > The second loop yields this table:
> >
> > range num digits 5000/len(str(i))  total bytes
> > 0-910 1  50005
> > 10-99  90 2  2500   225000
> > 100-999   900 3  1666  1499400
> > 1000-9000 4  1250 1125
> > 1-1 1 5  1000 1000
> >  
> >  grand total chars   23024400
> >
> > The two loops do not produce the same numbers of characters, so I'm not
> > surprised they do not consume the same amount of storage.
> >
> > P.S.: Please forgive me if I've made some basic math error somewhere.
> >
> > --
> > John Gordon   A is for Amy, who fell down the stairs
> > gor...@panix.com  B is for Basil, assaulted by bears
> >-- Edward Gorey, "The Gashlycrumb Tinies"
> >
> > --
> > http://mail.python.org/mailman/listinfo/python-list
> >
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python 3.2 Debug build

2011-03-17 Thread Santoso Wijaya
Looks like something tripped over whitespaces in path names for svn tools.
Try checking out a working copy from the hg repository?

~/santa


On Thu, Mar 17, 2011 at 3:54 PM, Willis Cheung  wrote:

> Hi all,
>
> I'm trying to build the debug version of Python 3.2. I downloaded the py3k
> folder from the python SVN. Then I opened the pcbuild.sln and tried to build
> the "python" project. However the build failed when I got an error from the
> project "pythoncore" which I think "python" depends on? The error is:
>
> Cannot open source file: 'C:\Program
> Files\py3k\PCbuild\Win32-temp-Debug\pythoncore\\getbuildinfo2.c': No such
> file or directory.
>
>
>
> The log also mentioned the following couple lines before:
>
>
>
> 5>"C:\Program Files\TortoiseSVN\bin\subwcrev.exe" ..
> ..\Modules\getbuildinfo.c "C:\Program
> Files\py3k\PCbuild\Win32-temp-Debug\pythoncore\\getbuildinfo2.c"
> 5>'C:\Program' is not recognized as an internal or external command,
>
>
> I did get the debug build of python 2.7.1 and 3.1.3 to work successfully so
> I'm not quite sure if I'm supposed to do anything different for Python 3.2.
> Can anyone guide me on this? Thank you.
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: py to exe converter

2011-03-21 Thread Santoso Wijaya
There's also py2exe: http://www.py2exe.org/

~/santa


On Mon, Mar 21, 2011 at 11:06 AM, Alan Harris-Reid
wrote:

>  On 19:59, PATRICIA MEDINA wrote:
>
>  I know there is a converter for python 2.x to executable file, but is
> there one for python 3.x yet?
>
>
> I use cx_Freeze without any problems  (//cx-freeze.sourceforge.net/)
>
> HTH
> Alan
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: py to exe converter

2011-03-21 Thread Santoso Wijaya
Nope. All the way to 2.7. [1]

~/santa

[1] http://sourceforge.net/projects/py2exe/files/py2exe/0.6.9/

On Mon, Mar 21, 2011 at 1:29 PM, Alan Harris-Reid
wrote:

>  According to //sourceforge.net/projects/py2exe/files/, the latest version
> only goes as far as Python2.5 :-(
>
> Alan
>
> --
> On 21/03/2011 19:47, Santoso Wijaya wrote:
>
> There's also py2exe: http://www..py2exe.org/ <http://www.py2exe.org/>
>
>  ~/santa
>
>
> On Mon, Mar 21, 2011 at 11:06 AM, Alan Harris-Reid <
> a...@baselinedata.co.uk> wrote:
>
>>  On 19:59, PATRICIA MEDINA wrote:
>>
>>  I know there is a converter for python 2.x to executable file, but is
>> there one for python 3.x yet?
>>
>>
>>  I use cx_Freeze without any problems  (//cx-freeze.sourceforge.net/)
>>
>> HTH
>> Alan
>>
>>
>>
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>>
>
> --
> --
>
> No virus found in this message.
> Checked by AVG - www.avg.com
> Version: 10.0.1204 / Virus Database: 1498/3520 - Release Date: 03/21/11
>
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: where to import

2011-02-17 Thread Santoso Wijaya
As a general rule, I group all my imports in the beginning of the module's
source, even if it is only used a handful of times by a few functions.

However, on such cases as the import is only needed by specific function(s)
zero or once, then I do the import in the function itself. Importing
argparse, for example:

import sys

def api_func(*args, **kwargs):
pass

def main(args):
import argparse

# ...

api_func()

if __name__ == '__main__':
sys.exit(main(sys.argv))

~/santa

~/santa


On Thu, Feb 17, 2011 at 1:45 PM, Terry Reedy  wrote:

> On 2/17/2011 12:27 PM, andrea crotti wrote:
>
>  Well no I wasn't really worried about performances.
>> I just thought that if an external module is really almost never used,
>> it might make sense to import it only when it's really needed.
>>
>
> If the module is only used in one function and the function may called 0 or
> 1 times, them yes, it makes some sense. But as soon as the function is
> called multiple times, or the module is used in more than one function, or
> someone wants to quickly look at the file to determine its dependencies,
> then local imports become more trouble than they are worth.
>
> For instance, suppose a new maintainer adds a function to the module that
> needs something from another module. If all imports are at the top, then it
> is trivial to see what imports are already made and whether they need
> modification.
>
> I believe the stdlib used to have some local imports, and might still, for
> all I know. PEP 8 is is based on long-term experience with the
> multi-maintainer stdlib as well as some stylistic preferences.
>
> --
> Terry Jan Reedy
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: running Python code from external application

2011-02-22 Thread Santoso Wijaya
Sounds like an embedding job. Have you taken a look at this
documentation
?

~/santa


On Tue, Feb 22, 2011 at 4:30 AM, Nadav Chernin wrote:

> Hello,
>
> I need to run Python code from external application. In this external
> application i have 2 options to run: DLL or COM.
>
>
> Can i create dll or com from the Python code?
>
> Or how can i use Python27.dll to call the script and get returned values?
>
> Thanks
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dictionary with additional attributes

2011-02-22 Thread Santoso Wijaya
Instead of inheriting a dict, why not composing a dict into your "model"
class, like:

class Model(object):
def __init__(self, *args, **kwargs):
self._probabilities = defaultdict(lambda: defaultdict(float))

@property
def features(self):
# insert your favorite algorithm to retrieve the set of all
features

~/santa


On Tue, Feb 22, 2011 at 11:49 AM, goodman  wrote:

> Hi, my question is this: Is it a bad idea to create a wrapper class
> for a dictionary so I can add attributes? E.g.:
>
> class DictWithAttrs(dict):
>pass
>
> More information:
> I'm using a nested defaultdict to store a machine-learning model,
> where the first key is a class, the second key is a feature, and the
> value is a probability for the pair. For example:
>
> model = defaultdict(lambda: defaultdict(float))
>
> This works quite well, but now I would like to keep track of the set
> of all features within the model. While I can get the set of classes
> by model.keys(), for the features I would have to do something like
> set(feat for cls in model for feat in model[cls]). Rather than do
> something like:
>
> model['__features__'] = set(feat for cls in model for feat in
> model[cls])
>
> It seems less hackish to put it in an attribute, since that wouldn't
> potentially collide with an actual key in the model:
>
> model.features = set(feat for cls in model for feat in model[cls])
>
> Note that I'm not asking to access dictionary key/values by means of
> attributes, but have additional attributes. I'm looking for best, or
> common, practice for this sort of thing.
>
> Thanks!
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list