Re: Get thread pid

2009-01-30 Thread ma
Actually, the command given "ps axH" uses H which shows threads as if they
were processes. If you check the pid of these "processes," you would find
that they are all equivalent.


On Fri, Jan 30, 2009 at 9:56 AM, Alejandro wrote:

> On Jan 30, 4:00 am, Ove Svensson  wrote:
> > Pidis a process identifier. Threads are not processes. All your threads
> > execute within the context if a single process, hence they should have
> > the samepid. Threads may have athreadid but it is not the same as thepid.
>
> According to this document (http://heather.cs.ucdavis.edu/~matloff/
> Python/PyThreads.pdf),
> at least in Linux, threads are process:
>
> "Here each thread really is a process, and for example will show up on
> Unix systems when one runs the appropriate ps process-list command,
> say ps axH. The threads manager is then the OS."
>
> If you look at my original post, pstree does show different PIDs for
> the threads.
>
> Regards,
> Alejandro.
>
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: Get thread pid

2009-01-30 Thread ma
I think issue here is that you're invoking a system call (using either the
subprocess module or os.popen*) from your threads. Those *are* external
processes and will show up under pstree since they have a parent process. If
you're using subprocess.Popen() the object that is returned has an attribute
'pid' that can be accessed (which would serve your purpose).

Please note that *this is NOT a thread id*


On Fri, Jan 30, 2009 at 11:33 AM, Alejandro
wrote:

> On Jan 30, 9:11 am, Jean-Paul Calderone  wrote:
> > [clarification about threads]
>
> Thank you for the clarification. I will reformulate my question:
>
> pstree and also ntop (but not top) show a number for each thread, like
> for instance:
>
> $pstree -p 9197
> python(9197)€ˆ€{python}(9555)
>  †€{python}(9556)
> †€{python}(9557)
> †€{python}(9558)
> †€{python}(9559)
> †€{python}(9560)
> †€{python}(9561)
> †€{python}(9562)
> †€{python}(9563)
>  „€{python}(9564)
>
> Is is possible to get the number corresponding to each thread?
>
> The reason I am interested is because one of my thread is hogging the
> CPU, and want to find which one is the culprit.
>
> Regards,
> Alejandro.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: Will multithreading make python less popular?

2009-02-17 Thread ma
Very well written response! Thanks Sturla


On Tue, Feb 17, 2009 at 10:50 AM, sturlamolden wrote:

> On 16 Feb, 10:34, rushen...@gmail.com wrote:
>
> > And the story begins here. As i search on the net,  I have found that
> > because of the natural characteristics of python such as GIL, we are
> > not able to write multi threaded programs. Oooops, in a kind of time
> > with lots of cpu cores and we are not able to write multi threaded
> > programs.
>
> The GIL does not prevent multithreaded programs. If it did, why does
> Python have a "threading" module?
>
> The GIL prevents one use of threads: parallel processing in plain
> Python. You can still do parallel processing using processes. Just
> import "multiprocessing" instead of "threading". The two modules have
> fairly similar APIs. You can still use threads to run tasks in the
> background.
>
> The GIL by the way, is an implementation detail. Nobody likes it very
> much. But it is used for making it easier to extend Python with C
> libraries (Python's raison d'etre). Not all C libraries are thread-
> safe. The GIL is also used to synchronize access to reference counts.
> In fact, Ruby is finally going to get a GIL as well. So it can't be
> that bad.
>
> As for parallel processing and multicore processors:
>
> 1. Even if a Python script can only exploit one core, we are always
> running more than one process on the computer. For some reason this
> obvious fact has to be repeated.
>
> 2. Parallel processing implies "need for speed". We have a 200x speed
> penalty form Python alone. The "need for speed" are better served by
> moving computational bottlenecks to C or Fortran. And in this case,
> the GIL does not prevent us from doing parallel processing. The GIL
> only affects the Python portion of the code.
>
> 3. Threads are not designed to be an abstraction for parallel
> processing. For this they are awkward, tedious, and error prone.
> Current threading APIs were designed for asynchronous tasks. Back in
> the 1990s when multithreading became popular, SMPs were luxury only
> few could afford, and multicore processors were unheard of.
>
> 4. The easy way to do parallel processing is not threads but OpenMP,
> MPI, or HPF. Threads are used internally by OpenMP and HPF, but those
> implementation details are taken care of by the compiler. Parallel
> computers have been used by scientists and engineers for three decades
> now, and threads have never been found a useful abstraction for manual
> coding. Unfortunately, this knowledge has not been passed on from
> physicists and engineers to the majority of computer programmers.
> Today, there is a whole generation of misguided computer scientists
> thinking that threads must be the way to use the new multicore
> processors. Take a lesson from those actually experienced with
> parallel computers and learn OpenMP!
>
> 5. If you still insist on parallel processing with Python threads --
> ignoring what you can do with multiprocessing and native C/Fortran
> extensions -- you can still do that as well. Just compile your script
> with Cython or Pyrex and release the GIL manually. The drawback is
> that you cannot touch any Python objects (only C objects) in your GIL-
> free blocks. But after all, the GIL is used to synchronize reference
> counting, so you would have to synchronize access to the Python
> objects anyway.
>
>
> import threading
>
> def _threadproc():
> with nogil:
># we do not hold the GIL here
>pass
> # now we have got the GIL back
> return
>
> def foobar():
>t = threading.Thread(target=_threadproc)
>t.start()
>t.join()
>
> That's it.
>
>
>
>
>
> Sturla Molden
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: Explanation for trailing comma in example from Learning Python?

2009-02-19 Thread ma
A comma is what generates a tuple. It's not the parenthesis;)
http://docs.python.org/library/stdtypes.html#typesseq

"A single item tuple must have a trailing comma, such as (d,)."

On Thu, Feb 19, 2009 at 3:57 PM, alex goretoy
wrote:

> Thank you for clerification Christian,
> when using trailing comma with print statement/function, does it not mean
> to output newline after printed data?
>
> -Alex Goretoy
> http://www.goretoy.com
>
>
>
>
> On Thu, Feb 19, 2009 at 2:54 PM, Christian Heimes wrote:
>
>> Carl Schumann wrote:
>> > I could see the logic in always or never having a trailing comma.   What
>> > I don't understand here is why only the single element case has a
>> > trailing comma.   Any explanations please?
>>
>> Does this code shad some light on the trailing comma? :)
>>
>> >>> (1) == 1
>> True
>> >>> (1,) == 1
>> False
>> >>> type((1))
>> 
>> >>> type((1,))
>> 
>>
>> >>> a = 1
>> >>> a
>> 1
>> >>> a = (1)
>> >>> a
>> 1
>> >>> a = (1,)
>> >>> a
>> (1,)
>> >>> a = 1,
>> >>> a
>> (1,)
>>
>> --
>> 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: Can someone explain this behavior to me?

2009-02-26 Thread ma
I'm pretty sure that Foo is getting replaced once you import Foo, why not
pass the Foo() object to bar's go? I'm sure there are other ways, but yes,
circular imports are indeed evil.


On Thu, Feb 26, 2009 at 5:13 PM, Chris Rebert  wrote:

> On Thu, Feb 26, 2009 at 1:48 PM, Jesse Aldridge 
> wrote:
> > I have one module called foo.py
> > -
> > class Foo:
> >foo = None
> >
> > def get_foo():
> >return Foo.foo
> >
> > if __name__ == "__main__":
> >import bar
> >Foo.foo = "foo"
> >bar.go()
> > -
> > And another one called bar.py
> > -
> > import foo
> >
> > def go():
> >assert foo.get_foo() == "foo"
> > --
> > When I run foo.py, the assertion in bar.py fails.  Why?
>
> Not sure, but circular imports are *evil* anyway, so I'd suggest you
> just rewrite the code to avoid doing any circular imports in the first
> place.
>
> Cheers,
> Chris
>
> --
> Follow the path of the Iguana...
> http://rebertia.com
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: can't find the right simplification

2009-04-23 Thread ma
Not sure I understand what is self.P, but look at the __getitem__ and  
__setitem__ overloads


--Sent from my iPhone

On Apr 23, 2009, at 7:10 PM, Stef Mientki   
wrote:



hello,

I've a program where you can connect snippets of code (which I call  
a "Brick") together to create a program.
To make it easier to create these code snippets, I need some  
simplifications.

For simple parameters ( integer, tupple, list etc)  this works ok,
and is done like this:


class _Simple_Par ( object ) :
  """
  Class to make it more easy to set a Bricks.Par from a control.
  So instead of :
self.Brick.Par [ self.EP[0] ] = Some_Value
  you can write
self.P[0] = Some_Value
  """
  def __init__ ( self, control ) :
  self.Control = control
  def __setitem__ ( self, index, value ) :
  i = self.Control.EP [ index ]
  if i :
  self.Control.Brick.Par [ i ] = value

Now I want a similar simplification for the case that Par is a  
dictionary:

So instead of writing:
self.Brick.Par [ self.EP[0] ] [ 'Filename' ] = Some_Value

I would like to write:
self.P[0] [ 'Filename' ] = Some_Value

But I can't figure out how to accomplish that ?

Any suggestions ?

thanks,
Stef Mientki
--
http://mail.python.org/mailman/listinfo/python-list

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


fcntl and siginfo_t in python

2009-04-28 Thread ma
Developing on a machine with a 2.6.5 kernel, which unfortunately, is not
blessed with inotify and we do not have FAM. I was wondering if there are
pre-built extensions ( a few google searches turned up fruitless) that
utilize fcntl properly and allow for siginfo_t struct support when
attempting to monitor directories/files?

If there aren't any, I'll see if it's feasible to add this functionality as
a py extension. Maybe this would be easier to do in ctypes first? Then
again, ctypes wasn't introduced until 2.5+. Suggestions or hints are
appreciated.

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


Re: fcntl and siginfo_t in python

2009-04-29 Thread ma
 Here's something that I came up with so far, I'm having some issues with
segfaulting, if I want to pass a struct member by ref in ctypes(see below),
if not, I just get a
"Real-time signal 0" sent back to me.

Any ideas?

#!/usr/bin/env python import os, sys, re
try: import fcntl
except ImportError:
 raise OSError, "Only runs on unix!"
 import signal from ctypes import * #ugh, but just for this exercise from
ctypes import util ### # sigaction /
siginfo structs ### # struct sigaction {
# void (*sa_handler)(int); # void (*sa_sigaction)(int, siginfo_t *, void *);
# sigset_t sa_mask; # int sa_flags; # void (*sa_restorer)(void); # }
## class sigval(Union): _fields_ = [
('sival_int', c_int), ('sival_ptr', c_void_p), ] class __siginfo(Structure):
_fields_ = [ ('si_signo', c_int), ('si_errno', c_int), ('si_code', c_int),
('si_pid', c_uint), ('si_uid', c_uint), ('si_status', c_int), ('si_addr',
c_void_p), ('si_value', sigval), ('si_band', c_long), ('pad', c_ulong * 7),
] siginfo_t = __siginfo class __sigaction_u(Union): _fields_ = [
('__sa_handler', CFUNCTYPE(None, c_int)), ('__sa_sigaction', CFUNCTYPE(None,
c_int,  POINTER(siginfo_t),
 c_void_p)), ] sigaction_u = __sigaction_u class __sigaction(Structure):
_fields_ = [ ('__sigaction_u',sigaction_u), ('sa_tramp', CFUNCTYPE(None,
c_void_p,
c_int, c_int,
 POINTER(siginfo_t), c_void_p)), ('sa_mask', c_uint), ('sa_flags', c_int), ]
class sigaction(Structure): _fields_ = [ ('__sigaction_u', sigaction_u),
('sa_mask', c_uint), ('sa_flags', c_int), ]
### # END sigaction / siginfo structs
## #call back that should be signalled,
void so return nothing def detailed_callback(signalno, siginfostruct, data):
print "detailed callback: ", signalno, \
siginfostruct, \
 data #ctypes prototype implementation C_PROTOTYPE_SIGNHANDLER =
CFUNCTYPE(None, c_int,
POINTER(__siginfo), c_void_p) #cast callback sighandler_cb =
C_PROTOTYPE_SIGNHANDLER(detailed_callback)
# # globals/literals
# ABS_PATH_LIBC =
util.find_library('libc') FCNTL_FLAGS=
[fcntl.DN_MODIFY,fcntl.DN_CREATE,fcntl.DN_DELETE,fcntl.DN_RENAME]
FCNTL_BIT_FLAG = reduce(lambda x, y: x | y, FCNTL_FLAGS) |
fcntl.DN_MULTISHOT #get library __clib = cdll.LoadLibrary(ABS_PATH_LIBC)
assert __clib is not None
#struct sigaction act; act = sigaction()

#act.sa_sigaction = handler;
act.__sigaction_u.sa_sigaction = sighandler_cb

#sigemptyset(&act.sa_mask);
#python2.6 has byref(act, offset),how can i port this over?
#maybe addressof(act)+sizeof(sigaction.sa_mask)*(position_in_sigaction)
rc = __clib.sigemptyset(byref(act))
assert rc == 0

#act.sa_flags = SA_SIGINFO;
#/usr/include/bit/signum.h SA_SIGINFO
#TODO: look this up from python act.sa_flags = 4;

#sigaction(SIGRTMIN, &act, NULL);
#signal.signal( signal.SIGRTMIN, sighandler_cb )
rc = __clib.sigaction( signal.SIGRTMIN, byref(act), None ) assert rc == 0

#fd stuff, open cwd, monitor for changes, should invoke
#my callback.. __fd = os.open(os.getcwd(), os.O_RDONLY | os.O_NONBLOCK)
__clib.fcntl( __fd, fcntl.F_SETSIG, signal.SIGRTMIN) __clib.fcntl( __fd,
fcntl.F_NOTIFY, FCNTL_BIT_FLAG ) import time while True: signal.pause()
--
http://mail.python.org/mailman/listinfo/python-list


Re: fcntl and siginfo_t in python

2009-04-30 Thread ma
I attached a clean copy of the .py file in case others couldn't read
it in their emails.
I'll try that and let you know how SIGRTMIN+1 goes!
What about this part?

#sigemptyset(&act.sa_mask);
#python2.6 has byref(act, offset),how can i port this over?
#maybe addressof(act)+sizeof(sigaction.sa_mask)*(position_in_sigaction)
rc = __clib.sigemptyset(byref(act))

Thanks!
Mahmoud



On Thu, Apr 30, 2009 at 7:33 PM, Philip  wrote:
>
> ma  gmail.com> writes:
>
> >
> >
> >
> >
> > Here's something that I came up with so far, I'm having some issues with
> segfaulting, if I want to pass a struct member by ref in ctypes(see below), if
> not, I just get a
> > "Real-time signal 0" sent back to me.
> >
> >
> > Any ideas?
>
> Try "SIGRTMIN+1", per http://souptonuts.sourceforge.net/code/dnotify.c.html
>
> Philip
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list


CtypesFcntl.py
Description: Binary data
--
http://mail.python.org/mailman/listinfo/python-list


ctypes: reference of a struct member?

2009-05-01 Thread ma
If I have this struct in C:

struct spam {
 int ham;
  char foo;
};


if I have this declaration:
struct spam s_;

If I wanted to pass a reference to a function of s_'s foo character, I
can do something like this:

somefunc(&s_.foo)

How do I do the same thing in ctypes?
ctypes.addressof(s_) + ctypes.sizeof(spam.foo)*(1)
--
http://mail.python.org/mailman/listinfo/python-list


Re: ctypes: reference of a struct member?

2009-05-01 Thread ma
ctypes.byref() does not work for struct members.
Try it out.
class s(ctypes.Structure):
_fields_ = [('x',ctypes.c_int)]

a = s()
ctypes.byref(a.x) //this won't work.



On Fri, May 1, 2009 at 2:28 PM, CTO  wrote:
> ctypes.byref()
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: fcntl and siginfo_t in python

2009-05-01 Thread ma
According to man signal,
"The default action for an unhandled real-time signal is to terminate
the receiving process."

This means that my registered callback and sigaction does not work. I
think the only solution would be to try this with a C-extension. Has
anyone had any experience with this before?

I attached my latest copy. Any insight is appreciated.

On Thu, Apr 30, 2009 at 7:37 PM, ma  wrote:
> I attached a clean copy of the .py file in case others couldn't read
> it in their emails.
> I'll try that and let you know how SIGRTMIN+1 goes!
> What about this part?
>
> #sigemptyset(&act.sa_mask);
> #python2.6 has byref(act, offset),how can i port this over?
> #maybe addressof(act)+sizeof(sigaction.sa_mask)*(position_in_sigaction)
> rc = __clib.sigemptyset(byref(act))
>
> Thanks!
> Mahmoud
>
>
>
> On Thu, Apr 30, 2009 at 7:33 PM, Philip  wrote:
>>
>> ma  gmail.com> writes:
>>
>> >
>> >
>> >
>> >
>> > Here's something that I came up with so far, I'm having some issues with
>> segfaulting, if I want to pass a struct member by ref in ctypes(see below), 
>> if
>> not, I just get a
>> > "Real-time signal 0" sent back to me.
>> >
>> >
>> > Any ideas?
>>
>> Try "SIGRTMIN+1", per http://souptonuts.sourceforge.net/code/dnotify.c.html
>>
>> Philip
>>
>>
>>
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>


ctypes_fcntl.py
Description: Binary data
--
http://mail.python.org/mailman/listinfo/python-list


Re: yet another list comprehension question

2009-05-03 Thread ma
This isn't list comprehension, but it's something to keep in mind:
b = filter(lambda x: None not in x, input_list)


On Sat, May 2, 2009 at 10:25 PM, CTO  wrote:

> On May 2, 10:13 pm, Ross  wrote:
> > I'm trying to set up a simple filter using a list comprehension. If I
> > have a list of tuples, a = [(1,2), (3,4), (5,None), (6,7), (8, None)]
> > and I wanted to filter out all tuples containing None, I would like to
> > get the new list b = [(1,2), (3,4),(6,7)].
>
> try this:
>
> b = [i for i in a if None not in i]
>
> > I tried b = [i for i in a if t for t in i is not None]   but I get the
> > error that "t is not defined". What am I doing wrong?
>
> You've got a "for" and an "if" backwards. t isn't defined when the if
> tries to evaluate it.
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: fcntl and siginfo_t in python

2009-05-04 Thread ma
Ok! So, I decided to write a C-extension instead of using ctypes. So
far, I create a module called dnotifier and the handler callback
receives two arguments, the signal number and the respective file
descriptor that was modified.

This works beautifully. Now, I want to release this to the public, so
I'm thinking of making a bit of code cleanup. Should I just pack the
entire siginfo_t struct, right now I just use the fd, into a
dictionary and pass it to the python callback handler function? Maybe
there might be some more suggestions to what data structures to use,
so I'm open right now to any of them.

The siginfo_t structure is defined as follows in C:

union sigval {
int sival_int;
void *sival_ptr;
};

typedef struct {
int si_signo;
int si_code;
union sigval si_value;
int si_errno;
pid_t si_pid;
uid_t si_uid;
void *si_addr;
int si_status;
int si_band;
} siginfo_t;

Thanks,
Mahmoud



On Fri, May 1, 2009 at 3:08 PM, ma  wrote:
>
> According to man signal,
> "The default action for an unhandled real-time signal is to terminate
> the receiving process."
>
> This means that my registered callback and sigaction does not work. I
> think the only solution would be to try this with a C-extension. Has
> anyone had any experience with this before?
>
> I attached my latest copy. Any insight is appreciated.
>
> On Thu, Apr 30, 2009 at 7:37 PM, ma  wrote:
> > I attached a clean copy of the .py file in case others couldn't read
> > it in their emails.
> > I'll try that and let you know how SIGRTMIN+1 goes!
> > What about this part?
> >
> > #sigemptyset(&act.sa_mask);
> > #python2.6 has byref(act, offset),how can i port this over?
> > #maybe addressof(act)+sizeof(sigaction.sa_mask)*(position_in_sigaction)
> > rc = __clib.sigemptyset(byref(act))
> >
> > Thanks!
> > Mahmoud
> >
> >
> >
> > On Thu, Apr 30, 2009 at 7:33 PM, Philip  wrote:
> >>
> >> ma  gmail.com> writes:
> >>
> >> >
> >> >
> >> >
> >> >
> >> > Here's something that I came up with so far, I'm having some issues with
> >> segfaulting, if I want to pass a struct member by ref in ctypes(see 
> >> below), if
> >> not, I just get a
> >> > "Real-time signal 0" sent back to me.
> >> >
> >> >
> >> > Any ideas?
> >>
> >> Try "SIGRTMIN+1", per http://souptonuts.sourceforge.net/code/dnotify.c.html
> >>
> >> Philip
> >>
> >>
> >>
> >> --
> >> http://mail.python.org/mailman/listinfo/python-list
> >
--
http://mail.python.org/mailman/listinfo/python-list


Re: fcntl and siginfo_t in python

2009-05-06 Thread ma

Sure, I'll send you the source files when I get a chance!

--Sent from my iPhone

On May 6, 2009, at 4:03 PM, Philip  wrote:


ma  gmail.com> writes:


Ok! So, I decided to write a C-extension instead of using ctypes...

This works beautifully. Now, I want to release this to the public, so
I'm thinking of making a bit of code cleanup. Should I just pack the
entire siginfo_t struct, right now I just use the fd, into a
dictionary and pass it to the python callback handler function? Maybe
there might be some more suggestions to what data structures to use,
so I'm open right now to any of them.


Could we have a look at your working prototype?

Philip J. Tait
http://subarutelescope.org

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

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


Re: win32 How to make sure a file is completely written?

2009-05-11 Thread ma
You have to wait until IO is ready. In Unix, we accomplish this with
fcntl and the default signal SIGIO, I am not sure how you would do
this in Windows.


On Mon, May 11, 2009 at 9:51 AM, justind  wrote:
> Hello,
>
> I'm using http://code.activestate.com/recipes/156178/ to watch a
> folder in windows. It's working perfectly, but sometimes when I try to
> open the file immediately after receiving the event, it's not ready to
> be opened--if I try to open it with PIL I get "IOError: cannot
> identify image file" and if I try it with a text file, it's empty.
> This doesn't happen all the time, just occasionally. I think the
> problem is that the file isn't completely written because if I make
> the script sleep for a second, it works every time. But that doesn't
> seem very elegant or robust.
>
> What's the proper way to make sure the file is ready to be read?
>
> I'm just passing the file names from the above recipe to a function.
> Something like
>
> def handler(files):
>    for file in files:
>        im = Image.open(file)
>
> Thanks
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: What's the use of the else in try/except/else?

2009-05-13 Thread ma
A really great use for try/except/else would be if an object is
implementing its own __getitem__ method, so you would have something
like this:

class SomeObj(object):
def __getitem__(self, key):
try:
#sometype of assertion here based on key type
except AssertionError, e:
raise TypeError, e #invalid type
else:
#continue processing, etc.. return some index, which 
will auto throw
#an index error if you have some type of indexable 
datastructure
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What's the use of the else in try/except/else?

2009-05-13 Thread ma
That's great to know! Thanks for that explanation, I am refactoring
something and I was going to make ample use of assertion as I thought
it was the same as C's assertion without the NDEBUG flag.


On Thu, May 14, 2009 at 1:03 AM, Steven D'Aprano
 wrote:
> On Thu, 14 May 2009 00:39:35 -0400, ma wrote:
>
>> A really great use for try/except/else would be if an object is
>> implementing its own __getitem__ method, so you would have something
>> like this:
>>
>> class SomeObj(object):
>>     def __getitem__(self, key):
>>               try:
>>                       #sometype of assertion here based on key type
>>               except AssertionError, e:
>>                       raise TypeError, e #invalid type
>
> Why raise AssertionError only to catch it and raise TypeError? Why not
> just raise TypeError in the first place?
>
>
> If you're thinking of writing this:
>
> assert isinstance(key, whatever)
>
> you should be aware that when Python is run with the -O flag (optimize),
> all asserts are disabled, and so your error checking code will not run
> and your program will crash and burn in a horrible flaming mess.
>
>
> [st...@wow-wow ~]$ python -O
> Python 2.5 (r25:51908, Nov  6 2007, 16:54:01)
> [GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> assert None is 2
>>>>
>>>>
>
>
> The assert statement is not for runtime error checking. assert is for
> checking your program logic and invariants. If you've ever written a
> comment like:
>
> # When we get here, then x will always be greater than 3.
>
> (or something like that), that's a great candidate for an assertion:
>
> assert x > 3, "x is unexpectedly less than or equal to three"
>
>
> For error checking, you should do something like this:
>
> if not isinstance(key, whatever):
>    raise ValueError
>
> rather than:
>
> try:
>    if not isinstance(key, whatever):
>        raise AssertionError
> except AssertionError:
>    raise ValueError
>
>
>
>
> --
> Steven
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to get path.py ? http://www.jorendorff.com/ is down

2009-05-22 Thread ma
http://web.archive.org/web/20071105095205/www.jorendorff.com/articles/python/path/


On Thu, May 21, 2009 at 4:10 PM, Kay Schluehr  wrote:

> On 21 Mai, 21:43, Jorge Vargas  wrote:
> > Hello.
> >
> > Anyone knows what is the problem with this package? apparently the
> > author's site is down which prevents pip from installing it. I can
> > download the zip and go from there but It seems most of the docs are
> > gone with the site.
>
> The code comments shall be sufficient. It is a single not very complex
> module that basically  unifies several stdlib APIs as methods of a
> string subclass.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: easiest way to plot x,y graphically during run-time?

2009-06-03 Thread ma
Try out PyChart, it's a very complete and has a great interface. I use
it to generate statistics for some of our production machines:
http://home.gna.org/pychart/

On Wed, Jun 3, 2009 at 1:28 PM, Esmail  wrote:
> Gökhan SEVER wrote:
>>
>> I don't know how easy to use pygame or pyOpenGL for data animation
>> comparing to Mayavi.
>>
>> Mayavi uses VTK as its visualization engine which is an OpenGL based
>> library. I would like to learn more about how alternative tools might be
>> beneficial say for example atmospheric particle simulation or realistic
>> cloud simulation.
>
> I've just started to read more about Particle Swarm Optimization and
> since I plan to implement this in Python, I thought it would be nice
> to visualize the process too, without spending too much on the nitty
> gritty details of graphics.
>
>> Esmail, you may ask your question in Matplotlib users group for more
>> detailed input.
>
> good idea, thanks,
>
> Esmail
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to test if a file is a symbolic link?

2009-10-28 Thread ma

import os

if os.path.islink('symbolic_link'):
print "hello."

Cheers,
Mahmoud Abdelkader

On Oct 28, 2009, at 11:19 PM, Peng Yu  wrote:


'symbolic_link' is a symbolic link in the current directory. I run
'python main.py', but it does not return me anything. I want to check
if a file is a symbolic link. I'm wondering what is the correct way to
do so?

$cat main.py
import stat
import os

st = os.stat('symbolic_link')
if stat.S_ISLNK(st.st_mode):
 print "Hello"
--
http://mail.python.org/mailman/listinfo/python-list

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


TWiki Python API Wrapper

2009-06-29 Thread ma
Has anyone come across a decent python API wrapper for TWiki? I'm trying to
automate some reports and logs to automatically post, create topics, and
re-arrange a few things on our TWiki, but my googleFu has failed me :(

I did find an interesting module in Perl,
http://cpanratings.perl.org/dist/WWW-Mechanize-TWiki . Looking at the TWiki
documentations, I found a perl API reference:
http://twiki.org/cgi-bin/view/TWiki/TWikiFuncDotPm . A proof of concept,
using these two perl modules, was generated by a blog post here:
http://roberthanson.blogspot.com/2006/01/copying-from-blogger-to-twiki-with.html

Before I make my own pythonic port, using mechanize, and wrapping around the
aforementioned TWiki api, I wanted to see if anyone had any other ideas,
approaches, or modules to help expedite the task?

Thanks!
Mahmoud Abdelkader
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [0, 0, 0, 1, 1, 1, 0] ... remove all 0 values

2009-07-08 Thread ma
filter(lambda x: x, your_list)

On Wed, Jul 8, 2009 at 10:44 AM, Daniel Austria  wrote:

> Hi python - hackers,
>
> just one question. How can i remove all 0 values in a list? Sure - i
> can loop over it, but that s not a neat style.  list.remove() will
> only remove the first occurence. Doing that while no exception is
> raised is also uncool, right?
>
> Some suggestions?
>
>
> Best,
> Dan
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


The state of pySerial

2013-05-29 Thread Ma Xiaojun
Hi, all.

pySerial is probably "the solution" for serial port programming.
Physical serial port is dead on PC but USB-to-Serial give it a second
life. Serial port stuff won't interest end users at all. But it is
still used in the EE world and so on. Arduino uses it to upload
programs. Sensors may use serial port to communicate with PC. GSM
Modem also uses serial port to communicate with PC.

Unforunately, pySerial project doesn't seem to have a good state. I
find pySerial + Python 3.3 broken on my machine (Python 2.7 is OK) .
There are unanswered outstanding bugs, PyPI page has 2.6 while SF
homepage still gives 2.5.

Any idea?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How clean/elegant is Python's syntax?

2013-05-29 Thread Ma Xiaojun
A bit more context.

If visiting z.cn (Amazon China), one can see that there are plenty of
new (published in 2010 or later) books on QBASIC, Visual Basic, Visual
Foxpro.

This is weird, if one want to do development legally these tools won't
be a option for new programmers.

However, I also like to know whether there are any technical arguments
that Python is superior in the context of education.

For pure procedural paradigm, I haven't seen much advantages of Python.
Yes, Python has true OOP but I don't like this argument since I don't
like Java-ism true OOP.
Yes, Python has much more libraries. But it seems that Python is more
useful and suitable in CLI and Web applications. People are still
discussing whether to replace tkinter with wxPython or not. VB and VFP
people are never bothered with such issue.
-- 
http://mail.python.org/mailman/listinfo/python-list


How clean/elegant is Python's syntax?

2013-05-29 Thread Ma Xiaojun
Hi, list.

I hope this is not a duplicate of older question. If so, drop me a
link is enough.

I've used Python here and there, just for the sweet libraries though.

For the core language, I have mixed feeling. On one hand, I find that
Python has some sweet feature that is quite useful. On the other hand,
I often find Pyhton snippets around hard to understand. I admit that I
never learned Python very formally; I've programmed in many other
languages already.

Code snippets in BASIC or Pascal seems quite obvious to understand
(Unless I don't understand the algorithm) .

Can someone inspire me?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How clean/elegant is Python's syntax?

2013-05-29 Thread Ma Xiaojun
On Thu, May 30, 2013 at 8:24 AM, Dan Stromberg  wrote:
> I'm finding it kind of hard to imagine not finding Python's syntax and
> semantics pretty graceful.
>
> About the only thing I don't like is:
>
>var = 1,
>
> That binds var to a tuple (singleton) value, instead of 1.
>
> Oh, and method decorators seem much more complex than they should've been.

Yes, you touched something. IMHO, Python has far more built-in
features so it looks at least complicated from time to time.

For example, some people use "generating 9x9 multiplication table" as
an programming exercise.

What interest me is a one liner:
print '\n'.join(['\t'.join(['%d*%d=%d' % (j,i,i*j) for i in
range(1,10)]) for j in range(1,10)])

I don't like code like this. But Python at least allow such practise.

> But on the whole, python is a pretty beautiful language.  It's not just
> another rehash of Pascal though; if that's what you want you might be better
> off looking elsewhere.

That's a fair point.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The state of pySerial

2013-05-29 Thread Ma Xiaojun
I've already mailed the author, waiting for reply.

For Windows people, downloading a exe get you pySerial 2.5, which
list_ports and miniterm feature seems not included. To use 2.6,
download the tar.gz and use standard "setup.py install" to install it
(assume you have .py associated) . There is no C compiling involved in
the installation process.

For whether Python 3.3 is supported or not. I observed something like:
http://paste.ubuntu.com/5715275/ .

miniterm works for Python 3.3 at this time.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How clean/elegant is Python's syntax?

2013-05-30 Thread Ma Xiaojun
On Thu, May 30, 2013 at 2:18 PM, Steven D'Aprano
 wrote:
> Which people? "People" can discuss any rubbish they like. For many
> reasons, tkinter will not be replaced. For the standard library, it is a
> good, stable, powerful but not cutting-edge GUI library. If you don't
> like it, you can install a third-party framework like wxPython. Using
> tkinter is not compulsory.

I'm new to tkinter and find tkdocs.com seems quite good.
But tkdocs.com's Python code sample is in Python 3.
And wxPython doesn't support Python 3 yet.
( May not be big issue but it's kind of bad. )

I observation about tkinter is that it seems lack of sophisticated features.
For example, there is nothing like DataWindow in PowerBuilder?

Python's IDLE is written in tkinter.
But anyone willing to use IDLE is a successful example of tkinter?
I actually use Gedit more than PyDev, etc.
But the non-fancy state of IDLE does reflect something, I guess.

> In the case of VB and VFP, they aren't bothered by such issues because
> they're used to closed-source, proprietary programming where you use what
> you are given and like it. In the open-source world, if you don't like
> what you are given, you find something else, and if you can't find it,
> you make it yourself.

I doesn't mean that choices are bad. I just kind of doubt that whether
Python with a open source GUI toolkit can cover the features provided
by VB standard controls and some external Windows built-in controls.
I'm almost sure that tkinter lacks the features provided by
sophisticated controls.
I have limited knowledge to VFP.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How clean/elegant is Python's syntax?

2013-05-30 Thread Ma Xiaojun
On Fri, May 31, 2013 at 1:28 AM, Chris Angelico  wrote:
> for (int i=0;i {
> //do something with foo[i]
> }

This is interesting!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How clean/elegant is Python's syntax?

2013-05-30 Thread Ma Xiaojun
functional VS imperative?

mechanical thinking VS mathematical thinking?

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


Re: Cancel threads after timeout

2013-01-27 Thread Jason Ma
I am not sure which database you're using, but all the mainstream RDBMS is
server-client Architecture, when you're running one DML(including the
SELECT), the RDBMS setting up a server process, the query running in that
process instead of your client process, so I guess your problem can solved
in the database layer more elegant than in your program.
1. Investigate the reason why you're take more time
2. Find your DBA or yourself, setting the related parameters in the
database.
3. If possible, using some embedded language in the database (like PL/SQL
in Oracle), it is more convenient.

Regards,
Jason


2013/1/26 hyperboreean 

> Here's the use case I want to implement - I have to generate a report
> from multiple database servers. This report should be generated every 2
> hours. Sometimes it happens that a query on one of the database servers
> takes longer than expected and impedes the generation of this report
> (that's right, the queries are ran sequential). What I am trying to
> achieve is to parallelize the queries on each database server and to be
> able to cancel one of them if it takes longer than X minutes.
> threading.Thread doesn't support this and seems that in
> general programming languages don't implement a way to cancel threads
> from the outside.
>
> Now, I've read all the stackoverflow threads about killing a thread,
> canceling a thread after a timeout, but all of them imply that you are
> able to check from within the thread if you should end the computation
> or not - that's not really my case, where the computation is a SQL
> query.
>
> So, what I have in mind is something like: the main loop starts a
> threading.Thread which in turn is responsible for starting another
> thread in which the actual computation happens (could be a
> threading.Thread or a multiprocessing.Process) *and* checks if the
> specified timeout has passed. If the time is up, it exits, letting the
> main loop know.
>
> Lots of words, no code - let me know if you have any suggestions, ideas
> to this rant.
>
> Thanks!
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Best wishes,

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


Why monkey patching on module object doesn't work ?

2016-08-17 Thread Shiyao Ma
Hi,

I am using Python2.

For the following snippet,

http://ideone.com/i36pKO

I'd suppose the dummy_func would be invoked, but seems not.

Indeed, heapq.heapify does invoke cmp_lt per here:
https://hg.python.org/cpython/file/2.7/Lib/heapq.py#l136

So why this way of monkey patching failed?



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


The order of iterable de-referencing in assignment?

2016-08-24 Thread Shiyao Ma
Hi,

Given a = [1, 2]

a.extend(a) makes a = [1,2, 1,2]

One might guess a.extend(a) would turn into an infinite loop.  It turns out 
here Python first gets all the items of `a' and then append them to `a', so the 
infinite loop is avoided.

My question is, is there any doc on the behavior of things like this?

Another related example might be:
a[:] = a
Hopefully Python first gets all the items on the *right* side and then assigns 
them to the left.

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


Regarding the parsing of await expression.

2016-11-06 Thread Shiyao Ma
Hi,

In the pep, 
https://www.python.org/dev/peps/pep-0492/#examples-of-await-expressions 

It is said,

await await coro()  is SyntaxError, instead, we should use await (await coro())
Why? because of await is not left-associative?


also, for 
await -coro() , it should be written as,  await (-coro())

I don't understand the point here.  Why can't the parser figure out it indeed 
is await (-coro()) ?


Is it due to the fact Python uses LL(1) or just because of current impl doesn't 
do that?

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


How to simulate C style integer division?

2016-01-21 Thread Shiyao Ma
Hi,

I wanna simulate C style integer division in Python3.

So far what I've got is:
# a, b = 3, 4

import math
result = float(a) / b
if result > 0:
  result = math.floor(result)
else:
  result = math.ceil(result)


I found it's too laborious. Any quick way?

-- 

吾輩は猫である。ホームーページはhttps://introo.me 。
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Can you use self in __str__

2014-11-27 Thread Shiyao Ma
2014-11-28 9:26 GMT+08:00 Seymore4Head :
> def __str__(self):
> s = "Hand contains "
> for x in self.hand:
> s = s + str(x) + " "
> return s
>
> This is part of a Hand class.  I need a hand for the dealer and a hand
> for the player.
> dealer=Hand()
> player=Hand()
> This prints out 'Hand contains " foo bar
> for both the dealer's hand and the player's hand.
>
> Is there a way to include "self" in the __string__ so it reads
> Dealer hand contains foo bar
> Player hand contains foo bar

I bet you want the object name (aka, dealer, player) be included in
the string 's'.
To that end, you need to access the namespace where 'self' is in.
But I dunno where the namespace 'self' resides in.
Does PyObject has a field to store the namespace of an object?
Appreciated if anyone could
inform me on this.

Now, make a little assumption that the instance lives in the module
level. Then we can do
this:

#!/usr/bin/env python

class Hand(object):
def __init__(self):
self.hand = [1, 2, 3, 4]

def __str__(self):
s = self._myname + " hand contains "
for x in self.hand:
s = s + str(x) + " "
return s

@property
def _myname(self):
# get the module
mod = self.__module__
import sys
ns = vars(sys.modules[mod])
# NB only works the instance is at the module level
for name, obj in ns.iteritems():
if id(obj) == id(self):
break
else:
#nothing found
return ""
return name

John = Hand()
print(John)

# this prints
# John hand contains 1 2 3 4

bad indentation with my wasavi plugin, see paste:

https://bpaste.net/show/f5b86957295f


What if it's in the local namespace of a function or method? IDK, try
to get that thing first.


Regards






--

吾輩は猫である。ホームーページはhttp://introo.me。
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Can you use self in __str__

2014-11-28 Thread Shiyao Ma
2014-11-28 13:00 GMT+08:00 Chris Angelico :
> On Fri, Nov 28, 2014 at 2:04 PM, Shiyao Ma  wrote:
>> What if it's in the local namespace of a function or method? IDK, try
>> to get that thing first.
>
Sure enough. I will even avoid using "id" as it's dependent on CPython
implementation. :)

> What if it's in multiple namespaces? What if it's not in any at all?
> Your solution is not going to work in the general case, AND it's a bad
> idea.
> Please don't do this in any code that I'll ever have to
> maintain.
No way. You will never get my project code in such a form. I am way
more peculiar about readability and robustness than most of my other
ppl.
The point is, the bpate is just for demonstration to show there is a
*possible* way to find the name even you didn't specify it at first
hand.




-- 

吾輩は猫である。ホームーページはhttp://introo.me。
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Can you use self in __str__

2014-11-28 Thread Shiyao Ma
2014-11-29 11:36 GMT+08:00 Chris Angelico :
> You can use id() on any object. You are guaranteed to get back an
> integer which is both stable and unique among all ids of objects that
> exist at the same time as the one you called it on. For as long as the
> object continues to exist, that number *will* stay the same. Sometimes
> that's all you need; for instance, imagine a simple mail server which
> produces logs like this:
>
> [142857] Beginning processing of message
> [142857] Parsing headers
> [314159] Beginning processing of message
> [314159] Parsing headers
> [142857] Storing in destination mailbox
> [314159] Connecting to destination server
> [142857] Finished processing of message
> [314159] Message accepted by destination
> [271871] Beginning processing of message
> [314159] Finished processing of message
>
> You can easily see, from the initial numbers, what log lines are
> associated with what messages. (Note that emails have their own IDs,
> which could in theory be used, but the id() of an internal dict can be
> used even before the email's ID has been read - as you see from the
> example, a log entry for "Parsing headers" has to be made prior to
> info from the headers being used.) It's not a problem if another
> 142857 comes up later on; there's a very clear begin and end to the
> message, and you're guaranteed that nothing can possibly be
> interspersed with a colliding ID.
>
> In other implementations of Python, these numbers might look less
> arbitrary (Jython, I believe, allocates them sequentially); but the
> code will work just as well on any compliant implementation of the
> language, because everything I've said above is a language guarantee,
> not a CPython implementation detail.
>
> ChrisA


Thanks. Informed.
The implementation dependent thing is "id(obj)" == virtual mem addr
(which caused my bad feeling).
Apparently have nothing to do with it here, though.

And 'id' is seemingly great to differentiate each obj.



--

吾輩は猫である。ホームーページはhttp://introo.me。
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Tuple of lists concatenation - function vs comprehension

2014-12-07 Thread Shiyao Ma
On 12/07, Ivan Evstegneev wrote:
> Hello,
>
> When I have worked  in Python shell (IDLE) I found this issue:
>
> >>>x = ([1, 2], [3, 4], [5, 6])
> >>>L = []
> >>>for I in x:
>   L.extend(i)
>
> >>>L
> [1,2,3,4,5,6]
>
> But when I try to make comprehension using above expression, I get this:
>
> >>>x = ([1, 2], [3, 4], [5, 6])
> >>>L = []
> >>> [L.extend(i) for i in x]
> [None, None, None]

Yes. per the doc, list.extend() returns None.

>
> But this works fine within function:
>
> >>> def myfunc():
>   x = ([1, 2], [3, 4], [5, 6])
>   L = []
>   [L.extend(i) for i in x]
>   print(L)
>
> >>>myfunc()
> [1, 2, 3, 4, 5, 6]

This is also so true, as you are print the var 'L'.

>
> The question is why I'm getting empty list while working with comprehension
> in interactive console?

You are also getting [None]*3 in comprenhension inside a function.

-- 
Shiyao Ma
http://introo.me
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Setting default_factory of defaultdict to key

2014-12-07 Thread Shiyao Ma
On Dec 07 at 11:31 -0500, Dave Angel wrote:
> Since this clearly is intended to be part of the earlier thread, please make
> it so by using reply-list or whatever equivalent your email program has.

Kinda OT. But interested what's the difference between reply-list and to.
In addition, based on what information a thread is formed?

-- 
Shiyao Ma
http://introo.me
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python re.search simple question

2014-12-07 Thread Shiyao Ma
On Dec 08 at 12:22 +0530, Ganesh Pal wrote:
> Hi Folks ,
>
> This  might seem to be very trivial question but iam breaking my head over
> it for a while .
>
>  My understanding is that re.search should search for the match anywhere in
> the string .
>
>
> why is re.search failing in the below case  ??
>
> >>> pattern
> 'Token-based migrations cannot be mixed with level-based: [prev 0 , now 1]'

I sense a bad smell. Be sure to escape the bracket [ ]

> >>> text
> ' LogMessage {type NDMP_LOG_DBG} {msg_id 0} {msg The process id for NDMP
> service is 0x9c216370} {associated_msg 0} {associated_msg_seq 0} Source
> filer:DartOS  Error: Token-based migrations cannot be mixed with
> level-based: [prev 0 , now 1]'
>
> >>> if (re.search(pattern,text)):
> ...print "Hi"
> ... else:
> ... print "BYE"
> ...

so see here: https://bpaste.net/show/d2f1cf66a492 . It prints "HI"

/me always wishes code is sent without others doing some extra formatting 
before testing.

Hope that helps.




-- 
Shiyao Ma
http://introo.me
-- 
https://mail.python.org/mailman/listinfo/python-list


Nested loops is strangely slow, totally at a loss.

2014-12-09 Thread Shiyao Ma
When doing nested loop, the very first iteration of the innermost loop ends 
ultimately slow.

Let's the code speak.

The following code is quite contrived. Actually it's derived from my 3d-dct 
script.
The actual difference is way more significant than this example.

In case of any evil of gmail, bpaste here: https://bpaste.net/show/edfef62edb17

# this constructs a space_len x space_len x space_len 3D coordinate
import timeit
from itertools import product
space_len = 580
space = product(xrange(space_len), xrange(space_len), xrange(space_len))


sparse_cloud = product(xrange(1), xrange(1), xrange(1))

for i, j, k in sparse_cloud:
ts = timeit.default_timer()
if (i, j, k) in space: pass
te = timeit.default_timer()
print("A, finish a loop with ", te-ts)
print("Done Test A")


sparse_cloud = product(xrange(1000), xrange(1000), xrange(1000))
for i, j, k in sparse_cloud:
ts = timeit.default_timer()
if (i, j, k) in space: pass
te = timeit.default_timer()
print("B, finish a loop with ", te-ts)
print("Done Test B")


# example output
"""
('A, finish a loop with ', 2.1457672119140625e-06)
Done Test A
('B, finish a loop with ', 8.736134052276611)
('B, finish a loop with ', 1.9073486328125e-06)
('B, finish a loop with ', 0.0)
('B, finish a loop with ', 0.0)
('B, finish a loop with ', 1.1920928955078125e-06)
('B, finish a loop with ', 9.5367431640625e-07)
('B, finish a loop with ', 9.5367431640625e-07)
...
"""

We can see that the first iteration of B ends rather slow, 8.7 seconds here.

Why? I am curious about the internals, what's happening under the hood that 
makes this happen?

Thanks in advance!


-- 
Shiyao Ma
http://introo.me
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Nested loops is strangely slow, totally at a loss.

2014-12-09 Thread Shiyao Ma
One thing to note, the logic of using "in" is not of concern here.
This is a *contrived* example, the problem is the slowness of the first
iteration.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Nested loops is strangely slow, totally at a loss.

2014-12-10 Thread Shiyao Ma
Thanks guys.

I was only aware of a limited iterables which themselves are iterators, e.g., 
the generator.

Seems like its really a pitfall. Any glossary, list on the iterables that 
*might* exhaust themselves?


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


Re: problem with for and if

2015-01-05 Thread Shiyao Ma
On Jan 05 at 06:27 -0800, Dariusz Mysior wrote:
> I want search count of szukana in zmienna but code below counting all 12 
> letters from "traktorzysta" word
>
> szukana="t"
> zmienna="traktorzysta"
>
>
> def gen():
> count=int(0)
> for a in zmienna:
> if szukana in zmienna:
> count+=1
> else:
> continue
> return count

def gen():
count = 0
for a in zmienna:
# we assume szukana is a single letter
if szukana == a:
count+=1
else:
continue
return count

More preferably, you should repetitively use "str.find"

Or just use `max(0,len(zmienna.split(szukana))-1)`

-- 
Shiyao Ma
http://introo.me
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: problem with for and if

2015-01-05 Thread Shiyao Ma
On Jan 05 at 22:38 +0800, Shiyao Ma wrote:

> More preferably, you should repetitively use "str.find"
>
> Or just use `max(0,len(zmienna.split(szukana))-1)`

Forgot there was a `str.count`, ;).

-- 
Shiyao Ma
http://introo.me
-- 
https://mail.python.org/mailman/listinfo/python-list


Weird behavior on __dict__ attr

2015-02-09 Thread Shiyao Ma
Hi.

My context is a little hard to reproduce.

NS3 is a network simulation tool written in C++. I am using its Python binding.

So the class I am dealing with is from a .so file.

Say, I do the following:

%

import ns.network.Node as Node

# Node is a class
# it has a __dict__ attr

# Now I instantiate an instance of Node
n = Node()

# I checked, there is no __dict__ on 'n'
# but the following succeeds.

n.foobar = 3



My understanding is the foobar is stored in n.__dict__, but seemingly n has no 
__dict__.

So where does the foobar go?


TIA.



-- 
Shiyao Ma
http://introo.me
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Bug? ( () == [] ) != ( ().__eq__([]) )

2013-08-07 Thread Shiyao Ma
Sorry. I don't quite get it. As you said, it first tries,
leftOperand.__eq__(rightOperand) then if it returns NotImplemented, it goes
to invoke rightOperand.__eq__(leftOperand). But for any reason, [] == ()
returns false, why?


On Mon, Aug 5, 2013 at 7:06 AM, Chris Angelico  wrote:

> On Sun, Aug 4, 2013 at 11:35 PM, Markus Rother 
> wrote:
> > Hello,
> >
> > The following behaviour seen in 3.2 seems very strange to me:
> >
> > As expected:
>  () == []
> > False
> >
> > However:
>  ().__eq__([])
> > NotImplemented
>  [].__eq__(())
> > NotImplemented
>
> You don't normally want to be calling dunder methods directly. The
> reasoning behind this behaviour goes back to a few things, including a
> way to handle "1 == Foo()" where Foo is a custom type that implements
> __eq__; obviously the integer 1 won't know whether it's equal to a Foo
> instance or not, so it has to defer to the second operand to get a
> result. This deferral is done by returning NotImplemented, which is an
> object, and so is true by default. I don't see any particular reason
> for it to be false, as you shouldn't normally be using it; it's more
> like a "null" state, it means "I don't know if we're equal or not". If
> neither side knows whether they're equal, then they're presumed to be
> unequal, but you can't determine that from a single call to __eq__.
>
> ChrisA
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
http://about.me/introom
-- 
http://mail.python.org/mailman/listinfo/python-list


Why PyINCREF on _PyFalseStruct and _PyTrueStruct?

2015-04-08 Thread Shiyao Ma
Hi.

While reading the rich_compare of PyLongObject, I noticed this line:

https://hg.python.org/cpython/file/a49737bd6086/Objects/longobject.c#l2785

It increments the ob_ref of the builtin True/False object.

Initializing the ob_ref of True/False to one so that they won't be
garbage collected if fair enough. Why do we increment it?

I don't see the reason behind it, since these two objects should
always stay in the memory and never participate the garbage collecting
system.


Regards.

-- 

吾輩は猫である。ホームーページはhttp://introo.me。
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why PyINCREF on _PyFalseStruct and _PyTrueStruct?

2015-04-08 Thread Shiyao Ma
On Wed, Apr 8, 2015 at 11:24 AM, Ian Kelly  wrote:
> The ref count is incremented because the caller will decrement it when
> it's done with the reference.

That makes sense.

To be generic, the caller won't check what the returned result is. It
just takes it as a normal PyObject. Traditionally, for functions like
above, the reference is assumed to be transferred to the caller.

Regards.

-- 

吾輩は猫である。ホームーページはhttp://introo.me。
-- 
https://mail.python.org/mailman/listinfo/python-list


Where is the c source code of the import mechanism that ignores invalid directory?

2015-07-21 Thread Shiyao Ma
Hi,

It looks to me that the import system of Python will ignore invalid
directories and cache the result in memory.

For example, the following code:
paste here: https://bpaste.net/show/b144deb42620

#!/usr/bin/env python3
import sysimport osimport shutil
sys.path.append("./test")shutil.rmtree("./test", ignore_errors=True)
try:
import fooexcept ImportError:
os.mkdir("./test")
with open("./test/foo.py", "w") as f:
f.write("print(3)")

import foo

the second import foo will fail even though it's there. This is because
when doing the first import foo, the directory .test doesn't exist, and
Python ignores that directory forever.


I am interested in the c side implementation of this "ignoring" part.


Any body help me to pinpoint the exact c source location?


Thanks.


-- 

吾輩は猫である。ホームーページはhttps://introo.me 。
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Where is the c source code of the import mechanism that ignores invalid directory?

2015-07-21 Thread Shiyao Ma
Yep. I followed from bltmodule.c(the import function) and got to the
import.c file, and finally got lost.


Regards.

On Tue, Jul 21, 2015 at 12:16 PM, Mark Lawrence 
wrote:

> On 21/07/2015 16:35, Shiyao Ma wrote:
>
>> Hi,
>>
>> It looks to me that the import system of Python will ignore invalid
>> directories and cache the result in memory.
>>
>> For example, the following code:
>> paste here: https://bpaste.net/show/b144deb42620
>>
>> #!/usr/bin/env python3
>>
>> import  sys
>> import  os
>> import  shutil
>>
>> sys.path.append("./test")
>> shutil.rmtree("./test",  ignore_errors=True)
>>
>> try:
>>  import  foo
>> except  ImportError:
>>  os.mkdir("./test")
>>  with  open("./test/foo.py",  "w")  as  f:
>>  f.write("print(3)")
>>
>>  import  foo
>>
>> the second import foo will fail even though it's there. This is because
>> when doing the first import foo, the directory .test doesn't exist, and
>> Python ignores that directory forever.
>>
>>
>> I am interested in the c side implementation of this "ignoring" part.
>>
>>
>> Any body help me to pinpoint the exact c source location?
>>
>> Thanks.
>>
>> --
>>
>> 吾輩は猫である。ホームーページはhttps://introo.me <http://introo.me>。
>>
>>
> Start here https://hg.python.org/cpython/file/6629773fef63/Python/import.c
>
> --
> My fellow Pythonistas, ask not what our language can do for you, ask
> what you can do for our language.
>
> Mark Lawrence
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>



-- 

吾輩は猫である。ホームーページはhttps://introo.me <http://introo.me>。
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: None in string => TypeError?

2014-06-09 Thread Shiyao Ma
2014-06-09 23:34 GMT+08:00 Roy Smith :

> We noticed recently that:
>
> >>> None in 'foo'
>
> raises (at least in Python 2.7)
>
> TypeError: 'in ' requires string as left operand, not NoneType
>
> This is surprising.  The description of the 'in' operatator is, 'True if
> an item of s is equal to x, else False '.  From that, I would assume it
> behaves as if it were written:
>
> for item in iterable:
> if item == x:
> return True
> else:
> return False
>
> why the extra type check for str.__contains__()?  That seems very
> unpythonic.  Duck typing, and all that.
>

It's a little bit inconsistent.  But it's clearly documented here:
https://docs.python.org/3/reference/expressions.html#in

Which, according to its own logic, the string is not  a *container* type.
It's just some chars, and that totally makes sense for to restrict the type
of x in "str" to be convertible to type str. On the other hand, containers
like list, and tuple, they are heterogeneous by default in Python, so a
item by item comparison is needed.



-- 

吾輩は猫である。ホームーページはhttp://introo.me。
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: try/except/finally

2014-06-09 Thread Shiyao Ma
It would be great if someone could discuss it from the viewpoint of
bytecode. e.g., how the stack is popped, etc.


2014-06-09 17:40 GMT+08:00 Marko Rauhamaa :

> Philip Shaw :
>
> > OTOH, it could just be that Guido didn't think of banning [return from
> > finally] when exceptions were first added and doesn't want to
> > introduce an incompatability later.
>
> You don't have to ban all nonsensical things. Most guns allow you to
> shoot yourself in the foot, even those with static type checking.
>
>
> Marko
> --
> https://mail.python.org/mailman/listinfo/python-list
>



-- 

吾輩は猫である。ホームーページはhttp://introo.me。
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: First time I looked at Python was(...)

2014-06-10 Thread Shiyao Ma
I wonder if it's opensourced. I am kinda interested in its implementation.
On the whole, the performance is rather good.


2014-06-10 22:39 GMT+08:00 Mark H Harris :

> On 6/9/14 3:54 PM, Carlos Anselmo Dias wrote:
>
>> Hi ...
>>
>> I'm finishing my messages with this ...
>>
>> The first time I looked into Python was +- 10 years ago ... and in the
>> last 10 years I did not spent more than 30 minutes looking at ... but I
>> like it ... it's easy to read ... even if I'm not familiar with the
>> syntax of ...
>>
>> When you look at the script I provided you in my first post ... if
>> you're capable of thinking about it ... yoy can see countless
>> terabytes/petabytes of information indexed .. it doesn't matter what
>> you're daling with ...it might be millions of databases or billions of
>> files ...
>>
>> I spent the last two days thinking about what I want to implement(...)
>> ... looking at your posts ... thinking in the wideness and in the
>> particularity of the detail ...
>>
>> I really consider that Python is one good option(probably the best) ...
>> the programmers need less lines of code to achieve what must be achieved
>> ... and this is one great advantage ...
>>
>> If you read what I wrote in my first post ->'Python team(...)' and if
>> somehow you're capable of visualize that integrated with logs ,etc ...
>> advertisement included, manipulation of the search string in the client
>> apis, etc ... you're very probably very capable of ...
>>
>> (...)
>>
>> Best regards,
>> Carlos
>>
>
> This is the funniest troll I have see in a while... and a bot to boot!
>
> ~cool
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>



-- 

吾輩は猫である。ホームーページはhttp://introo.me。
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how can i get the source code of goagent.exe?

2014-07-02 Thread Shiyao Ma
Ask on the goagent googlecode?


exe is fow win, dig out more on the linux version. I bet it should be
delivered with py source in that version.


Regards.

2014-07-03 10:20 GMT+08:00 liuerfire Wang :
> Hi 水静流深
> the source code is on https://github.com/goagent/goagent
>
> Hi Terry,
> GoAgent, a tool to help cross the GFW, is written by Python not Go. So this
> may be not off-topic. :P
>
>
> On Thu, Jul 3, 2014 at 6:40 AM, 水静流深 <1248283...@qq.com> wrote:
>>
>> There is a open source project-goagent,when you download it and extract
>> it,
>> code.google.com/p/goagent/downloads‍
>>
>>  in the local diretory, a file named goagent.exe in it.
>>
>> how can i get the source code of goagent.exe ,not the binary form ,the
>> text form.
>>
>>
>>
>>
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>>
>
>
>
> --
> Best regards.
> /**
> google+: +liuerfire twitter: @liuerfire
> 蛋疼不蛋疼的都可以试着点一下~^_^~
> ***/
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>



-- 

吾輩は猫である。ホームーページはhttp://introo.me。
-- 
https://mail.python.org/mailman/listinfo/python-list


How do you use `help` when write your code

2014-07-06 Thread Shiyao Ma
Hi Pythonistas

I often heard people mention use help(ob) as a way of documentation
look up. Personally I seldom/never do that. My normal workflow is use
ipython, obj? or obj?? for quick look up or use docs.python.org for a
detailed read.


Do you use `help`? How does it integrate into your workflow?

Or instead, what similar tools do you use?


Regards.

shiyao

-- 

吾輩は猫である。ホームーページはhttp://introo.me。
-- 
https://mail.python.org/mailman/listinfo/python-list


Python3+Vim dev environment

2014-07-17 Thread Shiyao Ma
Hi.

Anyone with working experience on setting up Python3 dev with vim?
functionalities needed: code completion and jump to defintion

YCM suffices but only with py2.
Any vim (plugin) for py3?

Or do you have any experience both running YCM and jedi-vim(for py3) ?
How's that going?


Regards

-- 

吾輩は猫である。ホームーページはhttp://introo.me。
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: PyWart(2.7.8) IDLE is more buggy than "Joe's apartment"!

2014-07-21 Thread Shiyao Ma
No intent to pollute this thread.

But really interested in the invalid@invalid.invalid mailing address.
And,,, obviously, I cannot send to invalid@invalid.invalid, so

How does you(he) make this?

2014-07-21 22:27 GMT+08:00 Grant Edwards :

> I was always taught that it's a "bug" is when a program doesn't do
> what a reasonable user expects -- that it's got nothing to do with the
> programmer's intent.
>
> --
> --
> https://mail.python.org/mailman/listinfo/python-list



-- 

吾輩は猫である。ホームーページはhttp://introo.me。
-- 
https://mail.python.org/mailman/listinfo/python-list


What's the function location that reads the cached .pyc file from disk.

2014-09-15 Thread Shiyao Ma
Hi.

what's the location of the function that reads the .pyc file ?

I bet it should lie in somewhere in
https://hg.python.org/cpython/file/322ee2f2e922/Lib/importlib

But what's the actual location?


Btw, why I need it?
I want to know the structure of a .pyc file. Of course the function
that reads the .pyc must know something about it.
(I am aware of the structure of a typical .pyc file from some clicks
of google pages, but I am interested in the *source* and the most
authoritative answer, aka, the source code).

Thanks.


-- 

吾輩は猫である。ホームーページはhttp://introo.me。
-- 
https://mail.python.org/mailman/listinfo/python-list


Understanding co_lnotab

2014-09-26 Thread Shiyao Ma
When reading the notes on co_lnotab

I totally got lost at this
line:https://hg.python.org/cpython/file/fd0c02c3df31/Objects/lnotab_notes.txt#l31

It says,"In case #b, there's no way to know
 from looking at the table later how many were written."


No way to know "what" is written?
And why no way to know "how many" times?


Regards


-- 

吾輩は猫である。ホームーページはhttp://introo.me。
-- 
https://mail.python.org/mailman/listinfo/python-list


How to quickly set up a multithreaded server that can handle http file post.

2013-03-11 Thread Shiyao Ma
Today I come across a problem.
Basically, my need is that I want to launch a http server that can not only
support get but also support post (including post file).
My first idea is to use -m http.sever. However, it only supports get.
Later I find some one extended basehttpserver and made it  support post.
However it is not multithreaded.

Is it easy to write a RELIABLE (I mean under normal cases) multithreaded
server that suits my need.
Also, are there any already invented wheel I can use?

Thx


-- 
My gpg pubring is available via: gpg --keyserver
subkeys.pgp.net--recv-keys 307CF736

More on: http://about.me/introom
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to quickly set up a multithreaded server that can handle http file post.

2013-03-11 Thread Shiyao Ma
Yes, sounds good. I should give it a try.

On Tue, Mar 12, 2013 at 1:02 AM, Xavier L.  wrote:

> On 13-03-11 10:42 AM, Shiyao Ma wrote:
>
>> Today I come across a problem.
>> Basically, my need is that I want to launch a http server that can not
>> only support get but also support post (including post file).
>> My first idea is to use -m http.sever. However, it only supports get.
>> Later I find some one extended basehttpserver and made it  support post.
>> However it is not multithreaded.
>>
>> Is it easy to write a RELIABLE (I mean under normal cases) multithreaded
>> server that suits my need.
>> Also, are there any already invented wheel I can use?
>>
>> Thx
>>
>>
>> --
>> My gpg pubring is available via: gpg --keyserver subkeys.pgp.net
>> <http://subkeys.pgp.net> --recv-keys 307CF736
>>
>> More on: http://about.me/introom
>>
>>  The best would be to use an existing webserver, such as Apache, Nginx,
> Lighttpd, etc. with python running as a fcgi or cgi script.
>
> Those webservers have been tested for longer and there is no need to
> reinvent the wheel.
>
> X
> --
> http://mail.python.org/**mailman/listinfo/python-list<http://mail.python.org/mailman/listinfo/python-list>
>



-- 
My gpg pubring is available via: gpg --keyserver
subkeys.pgp.net--recv-keys 307CF736

More on: http://about.me/introom
-- 
http://mail.python.org/mailman/listinfo/python-list


python3 string format

2013-03-25 Thread Shiyao Ma
HI.
one thing confuses me.
It is said in the pep3101 that "{}".format (x) will invoke the method
x.__format__
However, I looked at the src of python3 and found:
in class str(object), the format simply contains a pass statement
in class int(object), things is the same.

So, what's the mechanism that "{}" works?
Thx

-- 
My gpg pubring is available via: gpg --keyserver
subkeys.pgp.net--recv-keys 307CF736

More on: http://about.me/introom
-- 
http://mail.python.org/mailman/listinfo/python-list


At a loss on python scoping.

2013-03-25 Thread Shiyao Ma
Hi,
suppose I have a file like this:
class A:
r = 5
def func(self, s):
self.s = s
a = A()
print(a.r)# this should print 5, but where does py store the name of r

a.func(3)
print(a.s)# this should print 3, also where does py store this name.
what's the underlying difference between the above example?


-- 
My gpg pubring is available via: gpg --keyserver
subkeys.pgp.net--recv-keys 307CF736

More on: http://about.me/introom
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: At a loss on python scoping.

2013-03-25 Thread Shiyao Ma
PS, I now python's scoping rule is lexical rule (aka static rule). How does
LEGB apply to class?

On Tue, Mar 26, 2013 at 2:17 PM, Shiyao Ma  wrote:

> Hi,
> suppose I have a file like this:
> class A:
> r = 5
> def func(self, s):
> self.s = s
> a = A()
> print(a.r)# this should print 5, but where does py store the name of r
>
> a.func(3)
> print(a.s)# this should print 3, also where does py store this name.
> what's the underlying difference between the above example?
>
>
> --
> My gpg pubring is available via: gpg --keyserver subkeys.pgp.net--recv-keys 
> 307CF736
>
> More on: http://about.me/introom
>
>


-- 
My gpg pubring is available via: gpg --keyserver
subkeys.pgp.net--recv-keys 307CF736

More on: http://about.me/introom
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: At a loss on python scoping.

2013-03-26 Thread Shiyao Ma
Sorry for my obscure description.
"the name of r" , AFAIK, everything in python is just a reference. For
example, a = 3, means a points to a small integer; b= [] means b points to
a list somewhere in the memory. So I call r as the name of r.

To clarify my question.
say I wanna look up a.r
I guess the first step is to look inside a, maybe in the __dict__?
As proved in my ipython, the __dict__ is empty.
So, it will look up in A.__dict__
Here comes my first question, where does the scope of class A falls in when
considering LEGB.

On Tue, Mar 26, 2013 at 2:29 PM, Chris Angelico  wrote:

> On Tue, Mar 26, 2013 at 5:17 PM, Shiyao Ma  wrote:
> > class A:
> > r = 5
> > def func(self, s):
> > self.s = s
> > a = A()
> > print(a.r)# this should print 5, but where does py store the name of
> r
>
> What do you mean by "the name of r"?
>
> ChrisA
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
My gpg pubring is available via: gpg --keyserver
subkeys.pgp.net--recv-keys 307CF736

More on: http://about.me/introom
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python3 string format

2013-03-26 Thread Shiyao Ma
Thx for your reply.
I am using pycharm and simply press "go to declaration" which directs me to
a py file, containing the following code:
def format(*args, **kwargs): # known special case of str.format
"""
S.format(*args, **kwargs) -> string

Return a formatted version of S, using substitutions from args and
kwargs.
The substitutions are identified by braces ('{' and '}').
"""
pass
I am curious how you find the corresponding c source code.

On Tue, Mar 26, 2013 at 2:16 PM, Ian Kelly  wrote:

> On Mon, Mar 25, 2013 at 10:24 PM, Shiyao Ma  wrote:
> > HI.
> > one thing confuses me.
> > It is said in the pep3101 that "{}".format (x) will invoke the method
> > x.__format__
> > However, I looked at the src of python3 and found:
> > in class str(object), the format simply contains a pass statement
> > in class int(object), things is the same.
>
> I don't know what source you're looking at.  In CPython, both of those
> types are implemented in C, not Python, so there would be no pass
> statements involved.
>
> The int.__format__ method is implemented at:
>
> http://hg.python.org/cpython/file/3c437e591499/Objects/longobject.c#l4373
>
> It mainly just calls the _PyLong_FormatAdvancedWriter function, which is
> at:
>
>
> http://hg.python.org/cpython/file/3c437e591499/Python/formatter_unicode.c#l1399
>
> The str.__format__ method similarly is implemented at:
>
>
> http://hg.python.org/cpython/file/3c437e591499/Objects/unicodeobject.c#l12851
>
> and calls the _PyUnicode_FormatAdvancedWriter function at:
>
>
> http://hg.python.org/cpython/file/3c437e591499/Python/formatter_unicode.c#l1363
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
My gpg pubring is available via: gpg --keyserver
subkeys.pgp.net--recv-keys 307CF736

More on: http://about.me/introom
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: At a loss on python scoping.

2013-03-26 Thread Shiyao Ma
Thx, really a nice and detailed explanation.

On Tue, Mar 26, 2013 at 6:07 PM, Dave Angel  wrote:

> On 03/26/2013 02:17 AM, Shiyao Ma wrote:
>
>> Hi,
>> suppose I have a file like this:
>> class A:
>>  r = 5
>>  def func(self, s):
>>  self.s = s
>> a = A()
>> print(a.r)# this should print 5, but where does py store the name of r
>>
>> a.func(3)
>> print(a.s)# this should print 3, also where does py store this name.
>> what's the underlying difference between the above example?
>>
>>
> I don't think this is a scoping question at all.  These references are
> fully qualified, so scoping doesn't enter in.
>
> The class A has a dictionary containing the names of r and func.  These
> are class attributes.  Each instance has a dictionary which will contain
> the name s AFTER the A.func() is called.  Ideally such an attribute will be
> assigned in the __init__() method, in which case every instance will have s
> in its dictionary.
>
> When you use a.qqq  the attribute qqq is searched for in the instance
> dictionary and, if not found, in the class dictionary.  If still not found,
> in the parent classes' dictionary(s).
>
> You can use dir(A) and dir(a) to look at these dictionaries, but it shows
> you the combination of them, so it's not as clear.  In other words, dir(a)
> shows you both dictionaries, merged.  (Seems to me dir also sometimes
> censors some of the names, but that's a vague memory. It's never left out
> anything I cared about, so maybe it's things like single-underscore names,
> or maybe just a poor memory.)
>
>
> --
> DaveA
> --
> http://mail.python.org/**mailman/listinfo/python-list<http://mail.python.org/mailman/listinfo/python-list>
>



-- 
My gpg pubring is available via: gpg --keyserver
subkeys.pgp.net--recv-keys 307CF736

More on: http://about.me/introom
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: At a loss on python scoping.

2013-03-26 Thread Shiyao Ma
After read Dave's answer, I think I confused LEGB with attribute lookup.
So, a.r has nothing to do with LEGB.

On Tue, Mar 26, 2013 at 7:03 PM, Shiyao Ma  wrote:

> Thx, really a nice and detailed explanation.
>
>
> On Tue, Mar 26, 2013 at 6:07 PM, Dave Angel  wrote:
>
>> On 03/26/2013 02:17 AM, Shiyao Ma wrote:
>>
>>> Hi,
>>> suppose I have a file like this:
>>> class A:
>>>  r = 5
>>>  def func(self, s):
>>>  self.s = s
>>> a = A()
>>> print(a.r)# this should print 5, but where does py store the name of
>>> r
>>>
>>> a.func(3)
>>> print(a.s)# this should print 3, also where does py store this name.
>>> what's the underlying difference between the above example?
>>>
>>>
>> I don't think this is a scoping question at all.  These references are
>> fully qualified, so scoping doesn't enter in.
>>
>> The class A has a dictionary containing the names of r and func.  These
>> are class attributes.  Each instance has a dictionary which will contain
>> the name s AFTER the A.func() is called.  Ideally such an attribute will be
>> assigned in the __init__() method, in which case every instance will have s
>> in its dictionary.
>>
>> When you use a.qqq  the attribute qqq is searched for in the instance
>> dictionary and, if not found, in the class dictionary.  If still not found,
>> in the parent classes' dictionary(s).
>>
>> You can use dir(A) and dir(a) to look at these dictionaries, but it shows
>> you the combination of them, so it's not as clear.  In other words, dir(a)
>> shows you both dictionaries, merged.  (Seems to me dir also sometimes
>> censors some of the names, but that's a vague memory. It's never left out
>> anything I cared about, so maybe it's things like single-underscore names,
>> or maybe just a poor memory.)
>>
>>
>> --
>> DaveA
>> --
>> http://mail.python.org/**mailman/listinfo/python-list<http://mail.python.org/mailman/listinfo/python-list>
>>
>
>
>
> --
> My gpg pubring is available via: gpg --keyserver subkeys.pgp.net--recv-keys 
> 307CF736
>
> More on: http://about.me/introom
>
>


-- 
My gpg pubring is available via: gpg --keyserver
subkeys.pgp.net--recv-keys 307CF736

More on: http://about.me/introom
-- 
http://mail.python.org/mailman/listinfo/python-list


CHRIST: THE ARRIVAL

2005-12-25 Thread Antoll MA
www.antollma.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Injecting a global into a defined function??

2009-01-15 Thread Cong Ma
Hi,

I'd appreciate your hints on this problem. I'm writing a module in which several
functions can alter the value of a global variable (I know this sounds evil,
please forgive me...). What I'm trying to do is to eliminate the "global foo"
lines in those functions' bodies and to use a decorator for the same task. For
example:

@global_injected("SPAM")
def foo():
... ...

will have the same effect as

def foo():
global SPAM
... ...

Leaving the evilness of globals aside, I wonder how I can implement this (for
Python 2.x). I'd like to hear your opinions. Thank you.

Regards,
Cong.

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


Re: Injecting a global into a defined function??

2009-01-15 Thread Cong Ma
Terry Reedy wrote:
> Not to me.  You are using the module as a singleton class. The
> alternative is to write a class, make the functions methods, and
> instantiate the class.  If that instance must be a singleton, more work
> is required.  If multiple instances make sense, you can go the class
> route when you need to.

I've thought of this too, but it turns out those functions are related to each
other very loosely. They do a lot of completely different things independently
and have just one common global variable to share. IMHO it would reduce the
readability of code because the role of this class is very unclear. Anyway,
"readability" is mostly a subjective matter and I think what you pointed out is
a good idea in general.

>> For example:
>>
>> @global_injected("SPAM")
>> def foo():
>> ... ...
>>
>> will have the same effect as
>>
>> def foo():
>> global SPAM
>> ... ...
> 
> More keystrokes for no gain.

That's intended. I want these "evil" (or "foolish and senseless") functions to
"stand out" in the sourcecode. They have their evil marks right before the
definition, not buried in the function body.

> Decorators are usally intended to wrap the input function with another
> function.  But they can be used to modify the input function and return
> it altered.  The latter is what you are proposing.
> 
> You of course have to use SPAM as a local variable.  Then you might be
> able to write an implementation-specific convert_to_global(name)
> function that would rewrite the code part of the code object of the
> function to what it would have been had you been sensible and used the
> 'global' directive.  This would mean finding the code that loads and
> stores 'SPAM' as a local variable and convert is to code that loads and
> stores it as a global variable.  If the replacement takes more bytes
> than the original, then the jump offsets all have to be fixed.  Also
> needing fixing would be the line number table that is used to match code
> units to lines in the Python code for error tracebacks.
> 
> An alterntive would be to de-compile the code, insert the global
> directive, and recompile.  A non-decorator alternative would be to write
> a function with two parameters that takes a tuple of names and the code
> quoted as a string as arguments.  It would insert the global statement
> into the string, execute it, and return the function.

Thank you for your elaborate explanation :)

Regards,
Cong.

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


How to get a directory file descriptor?

2008-11-24 Thread Cong Ma
Dear all,

Can you give me some hint on getting a directory file descriptor in Python?
Besides, what's good about os.fchdir() if I can't get a directory fd in the
first place?

Thanks for your reply.

Regards,
Cong.

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


Re: How to get a directory file descriptor?

2008-11-25 Thread Cong Ma
r0g wrote:
> Cong Ma wrote:
>> Dear all,
>>
>> Can you give me some hint on getting a directory file descriptor in Python?
>> Besides, what's good about os.fchdir() if I can't get a directory fd in the
>> first place?
>>
>> Thanks for your reply.
>>
>> Regards,
>> Cong.
>>
> 
> for each in os.listdir(os.getcwd()):
>   print each
> 
> Roger.
> --
> http://mail.python.org/mailman/listinfo/python-list
> 
Roger,

It seemed I didn't make it clearly enough...

Your code fetches a bunch of strings representing file names in the working
directory, which is fine. But what I want is something like an integer file
descriptor, like the one returned by os.open() for files, or the Linux dirfd()
call, which returns an integer for a pointer to a DIR stream.

Regards,
Cong.

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


Re: How to get a directory file descriptor?

2008-11-25 Thread Cong Ma
alex23 wrote:
> On Nov 26, 12:31 am, "D'Arcy J.M. Cain" <[EMAIL PROTECTED]> wrote:
>> Is this what you want?
>>
>> ofiles = [open(x) for x in os.listdir(os.getcwd())]
> 
> 'open' returns a "file object", whereas the OP is after "file
> descriptors", which are returned by 'os.open'.
> 
> --
> http://mail.python.org/mailman/listinfo/python-list
> 
You got it.

So if I can't seem to get directory file descriptors, the only way to use
os.fchdir() in Python is to embed Python in C code?

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


Re: How to get a directory file descriptor?

2008-11-26 Thread Cong Ma
alex23 wrote:
> On Nov 26, 3:26 pm, greg <[EMAIL PROTECTED]> wrote:
>> os.O_DIRECTORY must be fairly new -- it doesn't exist
>> in my 2.5 installation. But os.O_RDONLY seems to work
>> just as well for this purpose.
> 
> Which OS are you using?
> 
> Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
> [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
 import os
 hasattr(os, 'O_DIRECTORY')
> True
> 
> I'm pretty certain it was present under Windows XP as well.
> 
> 
> --
> http://mail.python.org/mailman/listinfo/python-list
> 
Hello,

Thanks for your reply. I checked my Python 2.5 install on Linux and there's the
O_DIRECTORY flag. However this is not mentioned anywhere in the Library 
Reference.

There's another question regarding to this flag though: I checked the manual of
the Linux system call open(2), in which there's a paragraph on this flag:

   O_DIRECTORY
  If pathname is not a directory, cause the open  to  fail.   This
  flag is Linux-specific, and was added in kernel version 2.1.126,
  to avoid denial-of-service problems if opendir(3) is called on a
  FIFO  or  tape  device,  but  should  not be used outside of the
  implementation of opendir(3).

Note the "should not be used outside of the implementation of opendir(3)" part.
Does that mean using Python's os.open() with the O_DIRECTORY flag is a Bad
Thing? In C, I think, calling opendir() followed by dirfd() should be the
correct way of doing this?

Thanks.

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


Re: How to get a directory file descriptor?

2008-11-26 Thread Cong Ma
Nick Craig-Wood wrote:
> Here is how you do exactly that in python using ctypes
> 
> from ctypes import CDLL, c_char_p, c_int, Structure, POINTER
> from ctypes.util import find_library
> 
> class c_dir(Structure):
> """Opaque type for directory entries, corresponds to struct DIR"""
> c_dir_p = POINTER(c_dir)
> 
> c_lib = CDLL(find_library("c"))
> opendir = c_lib.opendir
> opendir.argtypes = [c_char_p]
> opendir.restype = c_dir_p
> dirfd = c_lib.dirfd
> dirfd.argtypes = [c_dir_p]
> dirfd.restype = c_int
> closedir = c_lib.closedir
> closedir.argtypes = [c_dir_p]
> closedir.restype = c_int
> 
> dir_p = opendir(".")
> print "dir_p = %r" % dir_p
> dir_fd = dirfd(dir_p)
> print "dir_fd = %r" % dir_fd
> print "closed (rc %r)" % closedir(dir_p)
> 
> Which prints on my linux machine
> 
> dir_p = 
> dir_fd = 3
> closed (rc 0)
> 
> I don't know why os doesn't wrap - opendir, closedir, dirfd, readdir
> etc - I guess because except if you are doing something esoteric, then
> os.list and os.walk do everything you could possibly want.
> 

Thank you so much Nick. I'll take a look into the ctypes module.

I guess the reason of Python library not including those wrappers has something
to do with standard compliance. The fchdir(2) manual says "comforming to
POSIX.1-2001" but the opendir(3) manual says "fdopendir() is specified in
POSIX.1-2008."

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


Re: Don't you just love writing this sort of thing :)

2008-12-04 Thread Cong Ma
Lawrence D'Oliveiro wrote:
> for \
> Entry \
> in \
> sorted \
>   (
> f for f in os.listdir(PatchesDir) if PatchDatePat.search(f) != 
> None
>   ) \
> :
> Patch = (open, 
> gzip.GzipFile)[Entry.endswith(".gz")](os.path.join(PatchesDir, Entry), "r")
> ... read from Patch ...
> Patch.close()
> #end for
> 
> --
> http://mail.python.org/mailman/listinfo/python-list
> 
The "if ... != None" is not necessary...  "if PatchDatePat.search(f)" is OK.
And I don't like it...

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


[Python 2.x] Pickling a datetime.tzinfo subclass instance?

2008-12-08 Thread Cong Ma
Hello,

I'm writing a program that pickles an instance of a custom subclass of
datetime.tzinfo. I followed the guides given in the Library Reference (version
2.5.2, chapter 5.1.6), which contain the note:

"Special requirement for pickling: A tzinfo subclass must have an __init__
method that can be called with no arguments, else it can be pickled but possibly
not unpickled again. This is a technical requirement that may be relaxed in the
future."

I tried this with an example "FixedOffset" subclass instance given in the
Example section in the manual. It indeed failed to unpickle. To work around
this, I found two possible solutions:
1. Modify the __init__ method so that it takes optional arguments with default
values;
2. Implement the __getinitargs__ method so that it does the opposite of
__init__: returning a tuple from the instance's internal state that can be used
to re-initialize an instance, retaining the old value.

My questions:
1. Is the "technical limitation" fixed in version 2.6 or 3.0? I can't check it
for myself now... Python.org seems down and I can't find the docs.
2. To stick with version 2.5, which of the above 2 methods is better? Both seems
 to unpickle to the correct result, but are there subtle side-effects? Or there
are better solutions?

Regards,
Cong.

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


Re: [Python 2.x] Pickling a datetime.tzinfo subclass instance?

2008-12-09 Thread Cong Ma
Gabriel Genellina wrote:
> En Mon, 08 Dec 2008 12:34:03 -0200, Cong Ma <[EMAIL PROTECTED]> escribió:
> 
>> I'm writing a program that pickles an instance of a custom subclass of
>> datetime.tzinfo. I followed the guides given in the Library Reference
>> (version
>> 2.5.2, chapter 5.1.6), which contain the note:
>>
>> "Special requirement for pickling: A tzinfo subclass must have an
>> __init__
>> method that can be called with no arguments, else it can be pickled
>> but possibly
>> not unpickled again. This is a technical requirement that may be
>> relaxed in the
>> future."
>>
>> I tried this with an example "FixedOffset" subclass instance given in the
>> Example section in the manual. It indeed failed to unpickle. To work
>> around
>> this, I found two possible solutions:
>> 1. Modify the __init__ method so that it takes optional arguments with
>> default
>> values;
> 
> Doing that still works with 2.6 and 3.0
> 
>> 2. Implement the __getinitargs__ method so that it does the opposite of
>> __init__: returning a tuple from the instance's internal state that
>> can be used
>> to re-initialize an instance, retaining the old value.
> 
> In fact, it doesn't matter *what* it returns, as far as they're valid
> arguments to __init__
> 
>> My questions:
>> 1. Is the "technical limitation" fixed in version 2.6 or 3.0? I can't
>> check it
>> for myself now... Python.org seems down and I can't find the docs.
> 
> No, they behave the same (odd) way.
> 
>> 2. To stick with version 2.5, which of the above 2 methods is better?
>> Both seems
>>  to unpickle to the correct result, but are there subtle side-effects?
>> Or there
>> are better solutions?
> 
> I'd use method 1, just because the __getinitargs__ are useless.
> 

Gabriel,

Thank you so much for your great explanations. Had you not told me this, I would
hardly be able to find it out by myself :)

Regards,
Cong.

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


Arisingsoft provides the Norton antivirus all in one security suite.

2011-01-03 Thread mani ma
hai,

Uses : The package includes a personal firewall, phishing protection
and the ability to detect and remove malware.
Norton 360 is compatible with 32-bit editions of Windows XP and 32-bit
or 64-bit editions of Windows Vista.Windows 7 support has been added.
Reviews cited Norton 360's low resource usage, relative to Norton
Internet Security 2007, and phishing protection.

 
http://www.arisingsoft.com/2010_11_14_archive.html
 http://www.arisingsoft.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


Office COM automatisation - calling python from VBA

2009-07-23 Thread Dushku, Aaron - Amherst, MA
I'd like a copy of that code.  Thanks for taking the time for all of us.


Sincerely,
Aaron Dushku


**
Aaron Dushku
GIS Specialist
USDA-NRCS
Amherst, Massachusetts
(413) 253-4379
Email:  aaron.dushku at ma.usda.gov


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