Floating point minimum and maximum exponent values

2013-07-16 Thread Marco

Hi all, why the maximum and minimum exp values are 1024 and -1021?:

>>> sys.float_info
sys.float_info(max=1.7976931348623157e+308, max_exp=1024, 
max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, 
min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, 
radix=2, rounds=1)


The values (in double precision) 0 and 2047 are reserved for zero, 
infinity and NaN (in combination with the fraction), so I was expecting 
-1022 and 1023...



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


Division and multiplication have a different behavior in the overflow case

2013-07-27 Thread Marco
In Python 3, when we hava a division and both the result and at least 
one operand are too large to convert to float, we get an exception:


>>> 2**1028 / 2**-2
Traceback (most recent call last):
  File "", line 1, in 
OverflowError: long int too large to convert to float

When the result is inside the limits, we get the right value:

>>> 2 ** 1025 / 2**10
3.59404027961e+305

Why the behavior is different in the case of the multiplication?

>>> 2 ** 1025 * 2**-10
Traceback (most recent call last):
  File "", line 1, in 
OverflowError: long int too large to convert to float

I think the multiplication should have the same behavior than the
division:

* `inf` or `-inf` when the operands are inside the limits,
  but the result is not
* `OverflowError`  when the result, and at least one operand,
  are out of range.
--
Marco Buttu
--
http://mail.python.org/mailman/listinfo/python-list


Built-in open() with buffering > 1

2012-08-23 Thread Marco

Please, can anyone explain me the meaning of the
"buffering > 1" in the built-in open()?
The doc says: "...and an integer > 1 to indicate the size
of a fixed-size chunk buffer."
So I thought this size was the number of bytes or chars, but
it is not:

>>> f = open('myfile', 'w', buffering=2)
>>> f.write('a')
1
>>> open('myfile').read()
''
>>> f.write('b')
1
>>> open('myfile').read()
''
>>> f.write('cdefghi\n')
8
>>> open('myfile').read()
''
>>> f.flush()
>>> open('myfile').read()
'abcdefghi\n'

Regards,
Marco
--
http://mail.python.org/mailman/listinfo/python-list


Re: Built-in open() with buffering > 1

2012-08-23 Thread Marco

On 08/24/2012 06:35 AM, Marco wrote:

Please, can anyone explain me the meaning of the
"buffering > 1" in the built-in open()?
The doc says: "...and an integer > 1 to indicate the size
of a fixed-size chunk buffer."


Sorry, I get it:

>>> f = open('myfile', 'w', buffering=2)
>>> f._CHUNK_SIZE = 5
>>> for i in range(6):
... n = f.write(str(i))
... print(i, open('myfile').read(), sep=':')
...
0:
1:
2:
3:
4:
5:012345

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


Re: Built-in open() with buffering > 1

2012-08-30 Thread Marco

On 08/26/2012 10:25 AM, Hans Mulder wrote:


The algorithm is explained at
http://docs.python.org/library/io.html#io.DEFAULT_BUFFER_SIZE


Thanks ;)


In other words: open() tries to find a suitable size by
calling os.stat(your_file).st_blksize and if that fails,
it uses io.DEFAULT_BUFFER_SIZE, which is 8192 on my box.


Yes, when the parameter `buffering` is a negative integer
that is right


Whether you call open with buffering=2 or any larger
number, does not matter: the buffer size will be the
outcome of this algorithm.


Mmm, I think it is not right, because in this case
the buffer size is not computed but it is
the value you assign to the buffering parameter.
In fact:

>>> f = open('myfile', 'w', buffering=2)
>>> f._CHUNK_SIZE = 1
>>> f.write('ab')
2
>>> open('myfile').read()

Now two bytes are in the buffer and the buffer is full.
If you write another byte, it will not be written in the
buffer, because the bytes in the queue will be transferred
into the buffer only when they are more than f._CHUNK_SIZE:

>>> f.write('c')
1
>>> open('myfile').read()

Now, if you write another byte 'd', the chunk 'cd' will
be transferred to the buffer, but because it is full,
its content 'ab' will be transferred to the disk, and
after 'cd' written to the buffer, that still full:

>>> f.write('d')
1
>>> open('myfile').read()
'ab'

So, the buffer is really of size 2

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


The opener parameter of Python 3 open() built-in

2012-09-03 Thread Marco

Does anyone have an example of utilisation?
--
http://mail.python.org/mailman/listinfo/python-list


Re: The opener parameter of Python 3 open() built-in

2012-09-03 Thread Marco

On 09/03/2012 03:05 PM, Dave Angel wrote:


Does anyone have an example of utilisation?



As of Python 3.2.3, there is no "opener" parameter in the open() function.
 http://docs.python.org/py3k/library/functions.html

I don't know of any such parameter in earlier or later versions, but I
could be wrong there.


It's new in Python 3.3:

http://docs.python.org/dev/library/functions.html#open



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


Python 3.3 and .pyo files

2012-09-21 Thread Marco
I was trying to import a pyo module in Python 3.3, but Python does not 
find it:


$ echo "print(__file__)" > foo.py
$ python3.3 -O -m foo
/home/marco/temp/foo.py
$ ls
foo.py  __pycache__
$ rm foo.py
$ mv __pycache__/foo.cpython-33.pyo foo.pyo
$ rm __pycache__ -r
$ ls
foo.pyo
# The following works in Python3.2, but not in 3.3
$ python3.3 -O -m foo
/usr/local/bin/python3.3: No module named foo

How come? Thanks in advance, Marco

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


Re: Python 3.3 and .pyo files

2012-09-21 Thread Marco

On 09/21/2012 02:55 PM, Steven D'Aprano wrote:


$ ls
>foo.pyo
># The following works in Python3.2, but not in 3.3
>$ python3.3 -O -m foo
>/usr/local/bin/python3.3: No module named foo


I can confirm that (1) it works using Python 3.2; (2) it doesn't work
using Python 3.3; and (3) it does work in Python 3.3 if you don't use the
-O option.


It doesn't work with Python 3.3.0rc2 too.

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


The type.__call__() method manages the calls to __new__ and __init__?

2012-11-20 Thread Marco

Looking at the documentation of Py3.3:

http://docs.python.org/3.3/reference/datamodel.html#object.__new__

I saw the method `__new__()` is called automatically when I create an 
istance. After the call to __new__(), if it returns an instance `self` 
then `self.__init__()` is called.


Because when I call an instance the __call__ method is called, and 
because the classes are instances of type, I thought when I call a Foo 
class this imply the call type.__call__(Foo), and so this one manages 
the Foo.__new__ and Foo.__init__ calls:


>>> class Foo:
... def __new__(cls):
... print('Foo.__new__()')
... return super().__new__(cls)
... def __init__(self):
... print('Foo.__init__(self)')
...
>>> f = type.__call__(Foo)
Foo.__new__()
Foo.__init__(self)

Is that right? Thanks in advance


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


A question about readability

2012-12-07 Thread Marco

Hi all, do you think this code:

$ more myscript.py
for line in open('data.txt'):
result = sum(int(data) for data in line.split(';'))
print(result)

that sums the elements of the lines of this file:

$ more data.txt
30;44;99;88
11;17;16;50
33;91;77;15
$ python3.3 myscript.py
261
94
216

is explicit enough? Do you prefer a clearer solution?
Thanks in advance, Marco
--
Marco
--
http://mail.python.org/mailman/listinfo/python-list


Meaning of `\newline` as escape sequence

2012-12-28 Thread Marco

Hi all, in the documentation:

http://docs.python.org/3.3/reference/lexical_analysis.html

the escape sequence `\newline` is expained as "Backslash and newline 
ignored". What does it mean?

Thanks in advance, M.
--
Marco
--
http://mail.python.org/mailman/listinfo/python-list


Difference between str.isdigit() and str.isdecimal() in Python 3

2012-05-16 Thread Marco

Hi all, because

"There should be one-- and preferably only one --obvious way to do it",

there should be a difference between the two methods in the subject, but 
I can't find it:


>>> '123'.isdecimal(), '123'.isdigit()
(True, True)
>>> print('\u0660123')
٠123
>>> '\u0660123'.isdigit(), '\u0660123'.isdecimal()
(True, True)
>>> print('\u216B')
Ⅻ
>>> '\u216B'.isdecimal(), '\u216B'.isdigit()
(False, False)

Can anyone give me some help?
Regards, Marco
--
http://mail.python.org/mailman/listinfo/python-list


Re: Difference between str.isdigit() and str.isdecimal() in Python 3

2012-05-16 Thread Marco

On 05/16/2012 06:24 PM, Ulrich Eckhardt wrote:


Marco wrote:

>>>>  '123'.isdecimal(), '123'.isdigit()
>  (True, True)
>>>>  print('\u0660123')
>  ٠123
>>>>  '\u0660123'.isdigit(), '\u0660123'.isdecimal()
>  (True, True)
>>>>  print('\u216B')
>  Ⅻ
>>>>  '\u216B'.isdecimal(), '\u216B'.isdigit()
>  (False, False)



[chr(a) for a in range(0x2) if chr(a).isdigit()]


Thanks to your list comprehension I found they are not equal:

>>> set([chr(a) for a in range(0x10) if chr(a).isdigit()]) - \
... set([chr(a) for a in range(0x10) if chr(a).isdecimal()])

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


Re: Difference between str.isdigit() and str.isdecimal() in Python 3

2012-05-17 Thread Marco

On 05/17/2012 02:15 AM, Steven D'Aprano wrote:


the Fine Manual has more detail, although I admit it isn't *entirely*
clear what it is talking about if you're not a Unicode expert:


http://docs.python.org/py3k/library/stdtypes.html#str.isdecimal


You are right, that is clear, thanks :)


Examples:

py>  c = '\u2155'
py>  print(c)
⅕
py>  c.isdecimal(), c.isdigit(), c.isnumeric()
(False, False, True)
py>  import unicodedata
py>  unicodedata.numeric(c)
0.2

py>  c = '\u00B2'
py>  print(c)
²
py>  c.isdecimal(), c.isdigit(), c.isnumeric()
(False, True, True)
py>  unicodedata.numeric(c)
2.0


Perfect explanation, thanks again, Marco


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


str.isnumeric and Cuneiforms

2012-05-17 Thread Marco

Is it normal the str.isnumeric() returns False for these Cuneiforms?

'\U00012456'
'\U00012457'
'\U00012432'
'\U00012433'

They are all in the Nl category.

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


[TWISTED] Howto Deferred

2011-07-14 Thread marco
Hello gals and guys,

I'm an experienced Python user and I'd like to begin playing with
Twisted.
I started RTFM the tutorial advised on the official site and I found it
really useful and well done.

Now I'd like to practice a bit by coding a little program that reads
strings from a serial device and redirects them remotely via TCP. For
that sake I'm trying to use deferred.

In the tutorial, a deferred class is instantiated at factory level, then
used and destroyed.

And here things get harder for me.
Now, in my test program I need to manage data which comes in a "random"
manner, and I thought about doing it in a few possible ways:

1. create a deferred at factory level and every time I read something
from the serial port add some callbacks:

class SerToTcpProtocol(Protocol):

  def dataReceived(self, data):
# deferred is already instantiated and launched
# self.factory.sendToTcp sends data to the TCP client
self.factory.deferred.addCallback(self.factory.sendToTcp, data)

2. or, either, create a deferred at protocol level every time I receive
something, then let the deferred do what I need and destroy it:

class SerToTcpProtocol(Protocol):

  def dataReceived(self, data):
d = defer.Deferred()
d.addCallback(self.factory.sendToTcp, data)
d.callback(data)

3. or again, use a deferred list:

class SerToTcpProtocol(Protocol):

  def dataReceived(self, data):
d = defer.Deferred()
d.addCallback(self.factory.sendToTcp, data)
self.factory.listDeferred.addCallback(lambda d)
d.callback(data)

Or I don't know.. hints are welcome.

Thank you in advance and sorry for my english.

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


minidom appendChild confusion

2005-09-23 Thread Marco
Hello!

Can anyone explain why the following code does not work?
(I'm using python2.4.)


Cheers,  Marco

--

# the following code does _not_ work.
# intended: put child-nodes as children to another node

from xml.dom.minidom import Document
doc = Document()
node1 = doc.createElement('one')
el1 = doc.createElement('e1')
el2 = doc.createElement('e1')
node1.appendChild(el1)
node1.appendChild(el2)
assert 2 == len(node1.childNodes) # ok

doc2 = Document()
node2 = doc2.createElement('two')

for el in node1.childNodes:
node2.appendChild(el)

assert 2 == len(node2.childNodes), "node2 has an unexpected number of
children"

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


Re: minidom appendChild confusion

2005-09-23 Thread Marco
That's it.  Thank you very much!

Marco

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


python problems with dos lineendings in python-scripts

2005-10-21 Thread marco
hi folks,

i can not run any python scripts with dos lineendings under cygwin's python.
if i run such a scripts i get stupid syntax error messages from python.

what can i do to run these scripts without changing the lineending of these 
scripts.

regards marco


pgpz3U7zraEvK.pgp
Description: PGP signature
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: noob question

2005-06-28 Thread Marco
Steven D'Aprano <[EMAIL PROTECTED]> wrote in message news:<[EMAIL PROTECTED]>...

> Can anyone think of some good, easy to understand examples of where
> Python's name/object model differs from the variable/value model?

a = b = [ 1 ]

a and b are _not_ two variables, each with [ 1 ] as value, but just two
names for the same object (a list). In other variable-based languages,
the above is equivalent to:

a = [ 1 ]
b = [ 1 ]

in Python it is not.

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


Re: check if object is number

2005-02-12 Thread marco
Steven Bethard wrote:
Is there a good way to determine if an object is a numeric type? 
.
.
.
Ideas?
Maybe this can help?
def isnumber(x):
try:
return(x == x-0)
except:
return False
print '1:\t', isnumber(1)
print '1.25:\t', isnumber(1.25)
print '1.0 / 7:\t', isnumber(1.0 / 7)
print '1+0j:\t', isnumber((1+0j))
print '"spam":\t', isnumber("spam")
output:
1:  True
1.25:   True
1.0/7:  True
1+0j:   True
"spam":   False
Ooops! While checking other posts I realized that this is almost the 
same as Dan Bishop's solution.

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


Re: A completely silly question

2004-12-17 Thread marco
Amir Dekel wrote:
What I need from the program is to wait for a single character input, 
something like while(getchar()) in C. All those Python modules don't 
make much sence to me...
Take a look at Alan Gauld's "Learning to Program"
(http://www.freenetpages.co.uk/hp/alan.gauld/)
in the section "Event Driven programming"
Hope it helps
Marco
--
http://mail.python.org/mailman/listinfo/python-list


Temp dir creation

2005-03-23 Thread marco

I'm a bit puzzled by the following behaviour
(at least in the way I read
   http://docs.python.org/lib/module-tempfile.html )

> python
Python 2.4 (#1, Mar 21 2005, 23:04:52) 
[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tempfile
>>> tfile = tempfile.mkstemp('','','.')
>>> tfile
(3, '/home/local/9qF7kf')

OK, it works as expected, but...

>>> tdir = tempfile.mkdtemp('','','.')
>>> tdir
'./HFXmX2'

I thought this should be '/home/local/HFXmX2'?
According to the doc page above "mkdtemp() returns
the absolute pathname of the new directory", which
isn't what I get:

>>> os.path.abspath(tdir)
'/home/local/HFXmX2'

Workaround I'm using:

>>> import os
>>> tdir = tempfile.mkdtemp('','',os.path.abspath('.'))
>>> tdir
'/home/local/5r8gFi'
>>> 

Comments?

Cheers!

--
[EMAIL PROTECTED]
Gunnm: Broken Angel  http://amv.reimeika.ca
http://reimeika.ca/  http://photo.reimeika.ca
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: New Python regex Doc (was: Python documentation moronicities)

2005-04-20 Thread marco

Re: http://xahlee.org/perl-python/python_re-write/lib/module-re.html

Bill Mill <[EMAIL PROTECTED]> writes:

> Alright, I feel like I'm feeding the trolls just by posting in this
> thread. Just so that nobody else has to read the "revised" docs, no it
> doesn't:

I find that Lee's version complements the official docs quite nicely.

> 1) He didn't really change anything besides the intro page and
> deleting the matching vs. searching page and the examples page. He
> also put a couple of  breaks into the doc.

Official doc:

findall(pattern, string[, flags])

Return a list of all non-overlapping matches of pattern in string. If
one or more groups are present in the pattern, return a list of groups;
this will be a list of tuples if the pattern has more than one
group. Empty matches are included in the result unless they touch the
beginning of another match. New in version 1.5.2. Changed in version
2.4: Added the optional flags argument.

Revised doc:

findall(pattern, string[, flags])

Return a list of all non-overlapping matches of pattern in string. For
example:

re.findall(r'@+', 'what   @@@do  @@you @think')
# returns ['@@@', '@@', '@']

If one or more groups are present in the pattern, return a list of
groups; this will be a list of tuples if the pattern has more than one
group. For example:

re.findall(r'( +)(@+)', 'what   @@@do  @@you @think')
# returns [('   ', '@@@'), ('  ', '@@'), (' ', '@')]

Empty matches are included in the result unless they touch the
beginning of another match. For example:

re.findall(r'\b', 'what   @@@do  @@you @think')
# returns ['', '', '', '', '', '', '', '']

need another example here showing what is meant by "unless they touch the
beginning of another match."


Personally I find the latter much clearer (even in its incomplete state).

> 3) adding "MAY NEED AN EXAMPLE HERE" instead of actually putting one in

Well, you could suggest one to him.

Cheers,

--
[EMAIL PROTECTED]
Gunnm: Broken Angel  http://amv.reimeika.ca
http://reimeika.ca/  http://photo.reimeika.ca
-- 
http://mail.python.org/mailman/listinfo/python-list


Required arguments in argparse: at least one of a group

2013-03-23 Thread Marco
Is there the possibility using the argparse module to group two or more 
arguments in order to have at least one of them required? For instance, 
I would like to have not an error only in the following cases:


  python finder.py --file myfile --dir mydir
  python finder.py --pattern mypattern --dir mydir
  python finder.py --file myfile --pattern mypattern --dir mydir

where --dir is required, and --file _or_ --parser have to be specified. 
In other words, I want the parser prints an error message just in this case:


  python finder.py --dir mydir

Thanks in advance, Marco
--
Marco
--
http://mail.python.org/mailman/listinfo/python-list


Re: Required arguments in argparse: at least one of a group

2013-03-24 Thread Marco

On 03/23/2013 05:27 PM, Rob Day wrote:


I don't know about argparse, but if you use docopt
(http://docopt.org/) then this is easy to do with something like:

"""Usage:

finder.py --file  --dir 
finder.py --pattern  --dir 
finder.py --file  --pattern  --dir 
"""


Thanks Rob, but I was looking for a solution in the stdlib

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


python setup.py install and dependencies

2013-04-14 Thread Marco

I have a some confusion about the package installation process.
Let's say I have manually installed Python 3.3, so I don't have 
distribute and pip. Now I want to install the bpython shell, so I 
download the source code and after I try to do "python3.3 setup.py 
install".
I did so, and all it'is ok, but I don't have the dependencies 
(pygments), so when I run bpython, Python complains with an ImportError: 
No module named 'pygments'.


So I installed distribute, and after I removed and reinstalled bpython, 
and now "python3.3 setup.py install" has installed pygments too.


So my question is: if I want "python3.3 setup.py install" installs the 
dependencies of a package I need to have distribute?

And pip installs the dependencies because of it uses distribute?

Thanks in advance,
--
Marco
--
http://mail.python.org/mailman/listinfo/python-list


PyOpengl text rendering with autocad font

2005-12-15 Thread Marco
I must use text in Opengl, Python
This text is for architecture design then I need to use AutoCad fonts
(extension .shx).
I have two troubles:

I don't find a guide, a good program, something for use any kind of
fonts in PyOpengl. The nehe tutorial has some bugs in translation to
Python.
I need to scale rotate the text.

I don't know anything about handling fonts, I must use this .shx but I
don't know. In which way can I take this file and put them in my
programs?

In two afternoons in Google I don't find anything.

If somebody wants to help me :-)
Thanks.
Marco Bonifazi



http://www.marcobonifazi.com

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


generating a wm5 executable

2007-06-06 Thread Marco
Hi,
I'm quite a newbee on Python and have started developing apps on
PythonCE.

My .pyc files are running fine under windows mobile 5 but I'm a bit
stuck on how to generate single file executable packages under such
platform, nor I've found a lot of reference about it - seems to be
quite a niche.

Can I run py2exe runs under wm5?

Any other suggestion?

Thanks in advance

Marco

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


Hey! A VERY c00l feature of X and mplayer(could bind to PyQt4)

2007-03-04 Thread Marco
I found a Very c00l feature, that you can write your own MPlayer GUI.

The core is Xembed (http://www.freedesktop.org/wiki/Standards/xembed-spec)
And Of course MPlayer support it as its para: -wid

I hope you like it!

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


How to create pid.lock via python?

2007-03-05 Thread Marco
Hello,

I write a program control a device via RS232, I hope to add some code
to let the program canNOT be used by other people when one people
using.

Can you tell me how to create pid.lock file in python?
Thank you!!


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


how to set different TCP_KEEPALIVE parameters for different socket ?

2007-03-12 Thread Marco
Hi,

I can use sd.setsockopt() to enable the socket use TCP keepalive
feature, but how to set a different parameters(interval, probes and
time) for it?
Sure, I can modify /proc/sys/net/ipv4/tcp_* , but need root password
and will affect other program.

Thank you!


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


Re: how to set different TCP_KEEPALIVE parameters for different socket ?

2007-03-12 Thread Marco
Oh, I see,

sd.getsockopt(SOL_TCP, TCP_*)

On 3/13/07, Marco <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I can use sd.setsockopt() to enable the socket use TCP keepalive
> feature, but how to set a different parameters(interval, probes and
> time) for it?
> Sure, I can modify /proc/sys/net/ipv4/tcp_* , but need root password
> and will affect other program.
>
> Thank you!
>
>
> --
> LinuX Power
>


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


How to set SA_RESTART flag in Python

2007-03-20 Thread Marco
Hi,
 I want to set SA_RESTART flag to restart a system-call. How to do
this in Python?

Thank you!


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


about second parameter of signal handler func.

2007-03-26 Thread Marco
Hi,

In C, a signal handler function has only one parameter, that is signal number.
But in Python(import signal), a signal handler function has two
parameters, the first is signal number, the second is "frame"?

What is "frame", please?

Thank you!


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


frame of Qt program

2007-04-02 Thread Marco
Hi,
 I want to write a PyQt4 program, which like a graph-ssh. One side
user choose server moder, and other side(s) user choose client mode.
Both of them have a GUI, they can connect via socket.

 I have study PyQt4 for a while and learn a little socket(from W.R
Stevens Unix Networking Programming). But I donot know how to integer
both of them.

 What the frame of this kind of program looked like? Or is there some example?

Thank you !!


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


communication between two classes which in different threading

2007-04-05 Thread Marco
Hello,

 I write two class, which one is Gui(PyQt4) other is socket server. I
wanna: if socket recv() something, Gui will show it in a label.
 But it seems that I cannot call the func from other threading. How to
reslove it?

Thank you!


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


Re: communication between two classes which in different threading

2007-04-05 Thread Marco
Yes, use signal/slot could reslove this question!

On 4/6/07, Marco <[EMAIL PROTECTED]> wrote:
> Hello,
>
>  I write two class, which one is Gui(PyQt4) other is socket server. I
> wanna: if socket recv() something, Gui will show it in a label.
>  But it seems that I cannot call the func from other threading. How to
> reslove it?
>
> Thank you!
>
>
> --
> LinuX Power
>


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


[no subject]

2007-04-09 Thread Marco
Hi,

I have a problem to install wxPython on my MacBook (Pythonversion 2.5).
If would install the wxPython (python setup.py install), then I got  
this error:

Traceback (most recent call last):
File "/Users/marco/Desktop/flexo1/wxpython/wxPython-src-2.8.3.0/ 
wxPython/setup.py", line 49, in 
copy_file('config.py', 'wx/build', update=1, verbose=1)
File "/Users/marco/python//lib/python2.5/distutils/file_util.py",  
line 119, in copy_file
"can't copy '%s': doesn't exist or not a regular file" % src
distutils.errors.DistutilsFileError: can't copy 'config.py': doesn't  
exist or not a regular file

Where is the problem and how could i fix it?

Thx for your answers.
marco
-- 
http://mail.python.org/mailman/listinfo/python-list


install wxPython

2007-04-09 Thread Marco
Hi,

I have a problem to install wxPython on my MacBook (Pythonversion 2.5).
If would install the wxPython (python setup.py install), then I got  
this error:

Traceback (most recent call last):
File "/Users/marco/Desktop/flexo1/wxpython/wxPython-src-2.8.3.0/ 
wxPython/setup.py", line 49, in 
copy_file('config.py', 'wx/build', update=1, verbose=1)
File "/Users/marco/python//lib/python2.5/distutils/file_util.py",  
line 119, in copy_file
"can't copy '%s': doesn't exist or not a regular file" % src
distutils.errors.DistutilsFileError: can't copy 'config.py': doesn't  
exist or not a regular file

Where is the problem and how could i fix it?

Thx for your answers.
marco

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


Re: Free Python Ebook

2007-09-16 Thread Marco
Hi George,

 > Please tell me from which website I will get the free Python Ebook.
which one do you mean?

I only know this one:
http://diveintopython.org

Bye,
Marco

PS: Sorry, hit the wrong button the first time ;-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: vim - what's a smarttab?

2007-09-24 Thread Marco
Hi "7stud",

> Alternatively, what is a smarttab?

in VIM type :help smarttab and you'll see the following:

'smarttab' 'sta'boolean (default off)
 global
 {not in Vi}
When on, a  in front of a line inserts blanks according to
'shiftwidth'.  'tabstop' or 'softtabstop' is used in other places.  A
 will delete a 'shiftwidth' worth of space at the start of the
line.
When off, a  always inserts blanks according to 'tabstop' or
'softtabstop'.  'shiftwidth' is only used for shifting text left or
right |shift-left-right|.
What gets inserted (a Tab or spaces) depends on the 'expandtab'
option.  Also see |ins-expandtab|.  When 'expandtab' is not set, the
number of spaces is minimized by using s.
NOTE: This option is reset when 'compatible' is set.

If you'd like to use VIM for Python make sure you have the following 
settings for the best result:
http://www.vex.net/~x/python_and_vim.html

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


IOError: [Errno 4] Interrupted system call

2007-02-05 Thread Marco
Hello,every one, I meet a question:

in my old script, I usually use os.popen2() to get info from standard
unix(LinuX) program like ps,ifconfig...

Now, I write a OO-based programme, I still use os.popen2( check
whether mplayer still working via ps command ), but some things I got
the following message:

Traceback (most recent call last):
  File "./mkt.py", line 351, in loop_timeout
self.process(self.event.get_next())
  File "./mkt.py", line 361, in process
self.player.play(command[1])
  File "./mkt.py", line 107, in play
if self.is_playing():
  File "./mkt.py", line 78, in is_playing
info = rfd.readlines()
IOError: [Errno 4] Interrupted system call

why? Thank you!

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


IOError: [Errno 4] Interrupted system call

2007-02-06 Thread Marco
Hello,every one, I meet a question:

in my old script, I usually use os.popen2() to get info from standard
unix(LinuX) program like ps,ifconfig...

Now, I write a OO-based programme, I still use os.popen2( check
whether mplayer still working via ps command ), but some things I got
the following message:

Traceback (most recent call last):
  File "./mkt.py", line 351, in loop_timeout
self.process(self.event.get_next())
  File "./mkt.py", line 361, in process
self.player.play(command[1])
  File "./mkt.py", line 107, in play
if self.is_playing():
  File "./mkt.py", line 78, in is_playing
info = rfd.readlines()
IOError: [Errno 4] Interrupted system call

why? Thank you!


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


mplayer bug or python bug?

2007-02-07 Thread Marco
The following code is my test program for control mplayer.
in movies/ there are about 20 movies, the code plays them in circle,
but mplayer will crash silently after a circle, the "sliently" means I
can handle popen2 without except, but no movie.

I have no idea about it...
Can you help me?

class SimplePlayer( myobject ):
def __init__(self):
self.debug('simple player init ready')
self.is_open = False
self.wfd = None
self.rfd = None

def play(self, file):
if self.is_open:
self.wfd.write('loadfile %s\n' %(file))
self.wfd.flush()
else:
self.wfd, self.rfd = os.popen2('mplayer -loop 0 -slave
-quiet -ao null %s 2> /dev/null' %(file))
self.is_open = True


##
if __name__ == '__main__':
player = SimplePlayer()
all = os.listdir('movies/')
print all
while True:
for current in all:
print current
player.play('movies/' + current)
time.sleep(3)

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


Linux-Signal VS QT

2007-02-08 Thread Marco
Can I use LinuX signal as a tool for commuction with a QT(PyQt4) programme?

The follow code didNOT work...


from PyQt4 import QtCore,QtGui
import signal
import sys
import os

try:
import psyco
psyco.full()
except:
pass

class Main(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)

self.frame = QtGui.QFrame(self)
self.label = QtGui.QLabel(str(os.getpid()), self.frame)
signal.signal(15, self.sig_handler)
print signal.getsignal(15)

def sig_handler(self, signum, sframe):
print 'received!'
self.label.setText('haha')
self.show()


#   main routine#

if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)

main = Main()
main.show()
app.exec_()


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


How to write a programe that include both pipe(low speed system call) and signal

2007-02-14 Thread Marco
Hi,
I have know that signal will interrupt some kind low speed system
call like pipe. But how to design a program that both support signal
and pipe?

I have a mplayer.py to play movie via os.popen2() and mplayer
slave mode. And there is a mplayer_ctl.py send signal to mplayer.py to
trigger function from mplayer.py.  Sometimes os.popen2() is reading or
writing when user run mplayer_ctl.py the bad things raise...

Is there some better way to design the programe?  Thank you


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


select which lib for iNet?

2007-02-25 Thread Marco
Hello everyone,

I wanta know why (Py)Qt has its own lib on socket/ sql/ openGL,etc ?

Is the lib better than standard lib?

Thank you!


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


a question in python curses modules

2008-02-14 Thread Marco
  Hi, I wanna write a simple curses program, but somethings confuse
me, my code here:

#!/usr/bin/python

import os
import sys
import time
import curses

class CursesObject( object ):

def __init__(self):

self.STDSCR = curses.initscr()
curses.noecho()
curses.cbreak()
self.STDSCR.keypad(1)

def __del__(self):
self.STDSCR.keypad(0)
curses.nocbreak()
curses.echo()
curses.endwin()

c1 = CursesObject()
time.sleep(1)


I donot know what happen, but in __del__ function, curses become None??!!

Thank you VERY much!
--
LinuX Power
-- 
http://mail.python.org/mailman/listinfo/python-list


python and rcp, portbind communication

2008-12-27 Thread Marco
Hello python list,

i want to write a program in python which can communicate with
rpcservices. At the first stage it shall talk with
portbind on port 111 and figure out the running rpc services. Is there
already a implementation for python to do so?

Best regards,
 marco
--
http://mail.python.org/mailman/listinfo/python-list


import overwrites __name__

2009-05-08 Thread Marco
Hi,

There happened something that I do not understand. Actually I don't even 
know how it can be possible.

I import a module and then the name space of the importing module seems do 
be overwritten.

my_name = __name__
print my_name
print len(dir())
from x import y as z
print __name__
print len(dir())
print my_name

->
__main__
119
x
117
unhandled NameError "name 'my_name' is not defined"

The module x is from me, and I am not aware of doing anything cruel there. 
What evil thing can be done in its sub module that can result in that 
strange thing?

br
Marco


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


Re: import overwrites __name__

2009-05-08 Thread Marco
Terry Reedy wrote:

> Marco wrote:
>> Hi,
>> 
>> There happened something that I do not understand. Actually I don't even
>> know how it can be possible.
>> 
>> I import a module and then the name space of the importing module seems
>> do be overwritten.
>> 
>> my_name = __name__
>> print my_name
>> print len(dir())
>> from x import y as z
>> print __name__
>> print len(dir())
>> print my_name
>> 
>> ->
>> __main__
>> 119
>> x
>> 117
>> unhandled NameError "name 'my_name' is not defined"
>> 
>> The module x is from me, and I am not aware of doing anything cruel
>> there. What evil thing can be done in its sub module that can result in
>> that strange thing?
> 
> see responses to "unicode bit me' thread

Therry,

What shall I find there?



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


Re: import overwrites __name__

2009-05-08 Thread Marco
Marco wrote:

> Hi,
> 
> There happened something that I do not understand. Actually I don't even
> know how it can be possible.
> 
> I import a module and then the name space of the importing module seems do
> be overwritten.
> 
> my_name = __name__
> print my_name
> print len(dir())
> from x import y as z
> print __name__
> print len(dir())
> print my_name
> 
> ->
> __main__
> 119
> x
> 117
> unhandled NameError "name 'my_name' is not defined"
> 
> The module x is from me, and I am not aware of doing anything cruel there.
> What evil thing can be done in its sub module that can result in that
> strange thing?


I just discovered the .pyc file for the main module is not created. When I 
delete the old pyc and import the main module from the python shell it works 
fine. But when I start it with "python main_module.py" the .pyc is not 
created and it happens as shown above.

So what leads to the behavior that the .pyc is not created?

br
Marco


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


Re: import overwrites __name__

2009-05-08 Thread Marco
Stephen Hansen wrote:

>>
>>  So what leads to the behavior that the .pyc is not created?
>>
> 
> PYC's are only generated on import, they're never made for the main
> script.
> 
> I personally like to keep my 'main script' very small. Largely just
> something which imports other modules and sends them along on their
> business.

Thanks Stephen,

I just learned that. But still I don't know why there is a different 
(correct) behavior when the main module is compiled.

As a work-around I put this on top of my main script:

import py_compile
py_compile.compile(__file__)

Still I appreciate if someone could explain me why.

br
Marco 


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


Inconsistency on getting arguments

2013-06-25 Thread Marco Perniciaro
Hi,
I've been working with Python for a long time.
Yet, I came across an issue which I cannot explain.

Recently I have a new PC (Windows 7).
Previously I could call a Python script with or without the "python" word at 
the beginning.
Now the behavior is different if I use or not use the "python" prefix!

I only have Python 2.7 installed and the path in in my environment variable.
I create a simple file called "example.py" which contains two lines:

  import sys
  print sys.argv

This is the output result:

  C:\Users\mapr>example.py a b c
  ['C:\\Users\\mapr\\example.py']

  C:\Users\mapr>python example.py a b c
  ['example.py', 'a', 'b', 'c']

Can someone please explain?

Regards,
Marco


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


Re: Beginners question

2012-08-30 Thread Marco Nawijn
On Thursday, August 30, 2012 1:54:08 PM UTC+2, (unknown) wrote:
> Hello
> 
> 
> 
> I'm slowly teaching myself python so apologies if this is a dumb question.
> 
> but something has confused me with the os.stat() function:
> 
> 
> 
> >>> s = os.stat(".")
> 
> >>> print s
> 
> posix.stat_result(st_mode=16877, st_ino=2278764L, st_dev=2053L, st_nlink=2, 
> st_u
> 
> id=1000, st_gid=100, st_size=4096L, st_atime=1346327745, st_mtime=1346327754, 
> st
> 
> _ctime=1346327754)
> 
> 
> 
> What sort of object is posix.stat_result? Its not a dictionary or list or a 
> 
> class object as far as I can tell. Thanks for any help.
> 
> 
> 
> B2003

Hi,

So let's try to figure this out. First of all, we can ask Python what object it 
is.

>>> s = os.stat('.')
>>> type(s)
posix.stat_result

So it seems to be a custom type. However types can inherit from builtins like
list, tuple and dict, so maybe it still is a dict or a tuple. Let's ask Python 
again:

>>> isinstance(s, dict)
False
>>> isinstance(s, (tuple, list))
False

Ok. So it is neither a list (tuple) nor a dict. So without reverting to the 
source code, it is probably save to say that the result is a custom class where 
the attributes can be accessed by the dot '.' notation. This is confirmed when 
you do:

>>> dir(s)
..
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 'n_fields',
 'n_sequence_fields',
 'n_unnamed_fields',
 'st_atime',
 'st_blksize',
 'st_blocks',
 'st_ctime',
 'st_dev',
 'st_gid',
 'st_ino',
 'st_mode',
 'st_mtime',
 'st_nlink',
 'st_rdev',
 'st_size',
 'st_uid']

For example:

>>> print s.st_size
4096

In case of Linux I think that the result of os.stat(..) is a wrapping of a C 
struct (a class with only attributes and no methods).

A small additional remark. Besides being a real dict or list (by means of 
inheritance), custom class can also implement the interface (__getitem__ etc.). 
If you want to know if an object implements this interface you could use the 
types defined in the 'abc' and 'collections' standard modules. So instead of 
checking if a type is a dict like this:

>>> isinstance(s, dict)

you could also check if it implements the dict interface:

>>> isinstance(s, collections.MutableMapping) # or similar

Regards,

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


Re: class object's attribute is also the instance's attribute?

2012-08-30 Thread Marco Nawijn
On Thursday, August 30, 2012 12:55:25 PM UTC+2, 陈伟 wrote:
> when i write code like this:
> 
> 
> 
> class A(object):
> 
>  
> 
> d = 'it is a doc.'
> 
> 
> 
> 
> 
> t = A()
> 
> 
> 
> print t.__class__.d
> 
> print t.d
> 
> 
> 
> the output is same.
> 
> 
> 
> so it means class object's attribute is also the instance's attribute. is it 
> right? i can not understand it.

I think the best way is to think of it as a global attribute restricted to the 
class A. Note that you even don't need an instance to get the value of the 
attribute. You can directly get it from the class itself.

>>> class A(object): d = 'my attribute'
>>> A.d
'my attribute'
>>> aobj = A()
>>> aobj.d
'my attribute'

Note that if you change 'd' it will change for all instances!
>>> bobj = A()
>>> bobj.d
'my attribute'

>>> A.d = 'oops...attribute changed'
>>> aobj.d
'oops...attribute changed'

>>> bobj.d
'oops...attribute changed'

If you want attributes to be local to the instance, you have to define them in 
the __init__ section of the class like this:

class A(object):

   def __init__(self):
d = 'my attribute'

>>> aobj = A()
>>> bobj = A()

>>> aobj.d
'my attribute'

>>> bobj.d
'my attribute'

>>> aobj.d = 'oops...attribute changed'

>>> aobj.d
'oops...attribute changed'

>>> bobj.d
'my attribute'

Regards,

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


Re: Beginners question

2012-08-30 Thread Marco Nawijn
On Thursday, August 30, 2012 3:15:03 PM UTC+2, Ulrich Eckhardt wrote:
> Am 30.08.2012 13:54, schrieb boltar2003@boltar.world:
> 
>  s = os.stat(".")
> 
>  print s
> 
> > posix.stat_result(st_mode=16877, st_ino=2278764L, st_dev=2053L, st_nlink=2, 
> > st_u
> 
> > id=1000, st_gid=100, st_size=4096L, st_atime=1346327745, 
> > st_mtime=1346327754, st
> 
> > _ctime=1346327754)
> 
> >
> 
> > What sort of object is posix.stat_result?
> 
> 
> 
> Use the type() function to find out. I guess that this is a named tuple, 
> 
> which is a tuple where the attributes are not indexed but have a name, 
> 
> see the documentation for the namedtuple() function from the collections 
> 
> library.
> 
> 
> 
> Uli

It is not a namedtuple. Because a namedtuple "is" a tuple and therefore 
isinstance(s, tuple) would have returned True.

>>> from collections import namedtuple
>>> Point = namedtuple('Point', 'x y')
>>> p = Point(10,2)
>>> isinstance(p, tuple)
True
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: class object's attribute is also the instance's attribute?

2012-08-30 Thread Marco Nawijn
On Thursday, August 30, 2012 3:25:52 PM UTC+2, Hans Mulder wrote:
> On 30/08/12 14:34:51, Marco Nawijn wrote:
> 
> 
> 
> > Note that if you change 'd' it will change for all instances!
> 
> 
> 
> That depends on how you change it.
> 
> 
> 
> >>>> bobj = A()
> 
> >>>> bobj.d
> 
> > 'my attribute'
> 
> > 
> 
> >>>> A.d = 'oops...attribute changed'
> 
> 
> 
> Here you change the attribute on the class.
> 
> That will affect all instances:
> 
> 
> 
> >>>> aobj.d
> 
> > 'oops...attribute changed'
> 
> > 
> 
> >>>> bobj.d
> 
> > 'oops...attribute changed'
> 
> 
> 
> You can also set the attribute on an instance:
> 
> 
> 
> >>> bobj.d = 'For bobj only'
> 
> >>> bobj.d
> 
> 'For bobj only'
> 
> >>>> aobj.d
> 
> > 'oops...attribute changed'
> 
> 
> 
> So, if you specifically change it on one instance, thenit won't
> 
> change on other instances of the same class.
> 
> 
> 
> > If you want attributes to be local to the instance, you have
> 
> > to define them in the __init__ section of the class like this:
> 
> 
> 
> That's a good idea, but it's not required.  You can set them
> 
> later, as shown above.
> 
> 
> 
> 
> 
> > class A(object):
> 
> > 
> 
> >def __init__(self):
> 
> > d = 'my attribute'
> 
> 
> 
> That will just set the global variable d.
> 
> You want to set the instance attribute:
> 
> 
> 
> self.d = 'my attribute'
> 
> 
> 
> >>>> aobj = A()
> 
> >>>> bobj = A()
> 
> > 
> 
> >>>> aobj.d
> 
> > 'my attribute'
> 
> 
> 
> Note that aobj.d will not find the global variable d,
> 
> if neither the instance, nor the class nor any of the
> 
> base classes have that attribute.
> 
> 
> 
> I don't know where this 'my attribute' comes from, but
> 
> it's not the instance attribute you tried to set in the
> 
> __init__ method.  Maybe your class A still has a class
> 
> attribute with that value from an earlier experiment.
> 
> 
> 
> 
> 
> Hope this helps,
> 
> 
> 
> -- HansM

Learned my lesson today. Don't assume you know something. Test it first ;). I 
have done quite some programming in Python, but did not know that class 
attributes are still local to the instances. It is also a little surprising I 
must say. I always considered them like static variables in C++ (not that I am 
an expert in C++). 

I knew of course that you don't have to define a local attribute in the 
__init__ method of a class, but I consider it good style and since the OP is a 
self claimed newbie I left out the other option. 

The missing "self" in the code below was a typo
class A(object): 
 
def __init__(self): 
d = 'my attribute'   # should be self.d 

Regards,

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


Re: class object's attribute is also the instance's attribute?

2012-08-30 Thread Marco Nawijn
On Thursday, August 30, 2012 4:30:59 PM UTC+2, Dave Angel wrote:
> On 08/30/2012 10:11 AM, Marco Nawijn wrote:
> 
> > On Thursday, August 30, 2012 3:25:52 PM UTC+2, Hans Mulder wrote:
> 
> >> 
> 
> >>
> 
> > Learned my lesson today. Don't assume you know something. Test it first ;). 
> > I have done quite some programming in Python, but did not know that class 
> > attributes are still local to the instances. 
> 
> 
> 
> They're not.  They're just visible to the instances, except where the
> 
> instance has an instance attribute of the same name.  Don't be confused
> 
> by dir(), which shows both instance and class attributes.
> 
> 
> 
> Please show me an example where you think you observe each instance
> 
> getting a copy of the class attribute.  There's probably some other
> 
> explanation.

I don't have an example. It was just what I thought would happen. Consider the 
following. In a class declaration like this:

class A(object):
attr_1 = 10

def __init__(self):
   self.attr_2 = 20

If I instantiated it twice:

obj_1 = A()
obj_2 = A()

For both obj_1 and obj_2 attr_1 equals 10. What I thought would happen after 
the following statement:

obj_1.attr_1 = 12

is that obj_2.attr_1 also equals 12. This is what surprised me a little, that's 
all. 

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


Re: Looking for an IPC solution

2012-08-31 Thread Marco Nawijn
On Friday, August 31, 2012 9:22:00 PM UTC+2, Laszlo Nagy wrote:
> There are just so many IPC modules out there. I'm looking for a solution 
> 
> for developing a new a multi-tier application. The core application will 
> 
> be running on a single computer, so the IPC should be using shared 
> 
> memory (or mmap) and have very short response times. But there will be a 
> 
> tier that will hold application state for clients, and there will be 
> 
> lots of clients. So that tier needs to go to different computers. E.g. 
> 
> the same IPC should also be accessed over TCP/IP. Most messages will be 
> 
> simple data structures, nothing complicated. The ability to run on PyPy 
> 
> would, and also to run on both Windows and Linux would be a plus.
> 
> 
> 
> I have seen a stand alone cross platform IPC server before that could 
> 
> serve "channels", and send/receive messages using these channels. But I 
> 
> don't remember its name and now I cannot find it. Can somebody please help?
> 
> 
> 
> Thanks,
> 
> 
> 
> Laszlo

Hi,

Are you aware and have you considered zeromq (www.zeromq.org)? It does not 
provide a messaging system, but you could use things like simple strings (json) 
or more complicated things like Protobuf. 

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


Re: python CAD libraries?

2012-09-11 Thread Marco Nawijn
On Monday, September 10, 2012 11:10:55 PM UTC+2, Jayden wrote:
> Are there any python CAD libraries that can
> 
> 
> 
> (1) build simple 3D primitives solids such as spheres, cylinders and so on
> 
> (2) perform bool operations on 3D solids
> 
> (3) better if it has some transformations such has scaling, sweeping, and 
> lofting
> 
> 
> 
> Please recommend some good ones for me? Thanks a lot!!

Hi Jayden,

In my opinion, the best you can get is OpenCascade (OCC) (www.opencascade.org) 
in combination with the python bindings (www.pythonocc.org). OCC is a hugh C++ 
CAD library. It not only deals with the simple geometric stuff, but it can be 
used to build CAD programs similar to SolidEdge or SolidWorks. It does however 
come with quite a steep learning curve. When using PythonOCC, the learning 
curve becomes a little less steep. 

Also note that in my opinion, Blender cannot be considered as a CAD 
environment. Ofcourse it is very powerful, but I think it is more targeted 
towards animation and visually pleasing applications, not mechanical 
engineering.

Regards,

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


Re: pythonOCC examples doesn't work?

2012-09-11 Thread Marco Nawijn
On Wednesday, September 12, 2012 6:02:14 AM UTC+2, Jayden wrote:
> I installed 
> 
> (1) pythonxy2.7.2.3 (with python2.7) and 
> 
> (2) pythonOCC-0.5-all-in-one.win32.py26 
> 
> on windows 7 64 bit computer. 
> 
> 
> 
> I try run pythonOCC examples in its example folder, such as the helloworld.py 
> and got errors as follows: 
> 
> 
> 
> ImportantError: DLL load failed: The specified module could not be found. 
> 
> 
> 
> The error come from the line of code: 
> 
> 
> 
> from OCC.BrepPrimAPI import * 
> 
> 
> 
> How to fix the error? Thanks a lot!!

Hi Jayden,

It has been some time ago that I used PythonOCC and I used it on Linux, so I 
cannot be of much help here. It sounds like you have to tell Windows where to 
look for the installed libraries (environment variables?). Anyhow, I recommend 
to post the question to the PythonOCC mailinglist. They are quite responsive.

One last suggestion. OCC itself comes with a small utility called DRAWEXE. It 
is a tcl/tk program that can be used to play around with a lot of the 
functionality provided by OCC.

Good luck!

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


Re: What's the tidy/elegant way to protect this against null/empty parameters?

2012-10-16 Thread Marco Nawijn
On Monday, October 15, 2012 1:33:02 PM UTC+2, (unknown) wrote:
> I want to fix an error in some code I have installed, however I don't
> 
> really want to just bodge it.
> 
> 
> 
> The function producing the error is:-
> 
> 
> 
> def get_text(self, idx):   # override !
> 
> node = self.items[idx]
> 
> 
> 
> a= [
> 
> ", ".join(node.tags),
> 
> node.comment,
> 
> node.folderName,
> 
> cd2rd(node.date),
> 
> node.name,
> 
> '[' + self.rating_stars[node.rating] + ']'
> 
> ] [self.select]
> 
> 
> 
> return a
> 
> 
> 
> 
> 
> The error occurs when node[] (or at least its members) turn out to be
> 
> empty, you get a Traceback that ends with:-
> 
> 
> 
>   File "/usr/lib/jbrout/jbrout/listview.py", line 608, in draw_cell 
> layout.set_text(self.get_text(thumbnail_num))
> 
>   File "/usr/lib/jbrout/jbrout.py", line 325, in get_text ", 
> ".join(node.tags),
> 
>   TypeError: sequence item 0: expected string, NoneType found
> 
> 
> 
> Now its *probably* something higher up the tree causing the problem
> 
> (it's only one particular image in 20 thousand or so that breaks
> 
> things) but I really want to just get things working.  So, what's the
> 
> neatest way to protect the get_text() method from empty data?
> 
> 
> 
> 
> 
> -- 
> 
> Chris Green
Hi,

Instead of protecting against empty data, you could just catch the exception, 
issue a warning and return a default "error" node which is valid. So something 
like (not tested):

def get_text(self, idx):   # override ! 
node = self.items[idx] 

error_a = "A valid, but erroneous representation of a"

try:
a= [ 
", ".join(node.tags), 
node.comment, 
node.folderName, 
cd2rd(node.date), 
node.name, 
'[' + self.rating_stars[node.rating] + ']' 
] [self.select] 
except TypeError:
  print 'Oops, something went wrong'
  a = error_a
# You should always have a valid a here (or another exception has  
occured)
return a 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Accessing variables in __init__.py

2012-10-16 Thread Marco Nawijn
On Tuesday, October 16, 2012 10:48:17 AM UTC+2, Gaudha wrote:
> my_package/
> 
>   __init__.py
> 
>   my_module1.py
> 
>   my_module2.py
> 
>   variables.py
> 
> 
> 
> I want to define common variables in __init__.py and use the namespace in 
> my_module1.py or my_module2.py. Defining it is not a problem. How can call it 
> from my modules?
> 
> 
> 
> If I define them in a module (say, variables.py), I can call them by 
> importing variables.py in other modules. How can it be done if I define it in 
> __init__.py?
> 
> 
> 
> It may be a silly query as I am newbie in Python. But, I would be grateful to 
> get help.

Hi,

If you store the variables in __init__.py, you can import them from the 
package. So in your case suppose __init__.py contains:
a = 10
b = {1 :"Hello", 2: "World" }

Than if you import my_package, you can access the variables as follows 
(interactive IPython session):

In [1]: import my_package

In [2]: my_pack
my_package   my_package/  

In [2]: my_package.
my_package.a  my_package.b  

In [2]: my_package.a
Out[2]: 10

In [3]: my_package.b
Out[3]: {1: 'Hello', 2: 'World'}

In [4]: 

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


Re: Providing a Python wrapper to a C++ type.

2012-10-16 Thread Marco Nawijn
On Tuesday, October 16, 2012 10:11:52 AM UTC+2, aaron.l...@gmail.com wrote:
> Hi,
> 
> 
> 
> I have a C++ module where I have a defined, working type. How would I make a 
> wrapper for this type to be able to be used in Python? I am familiar(-ish) 
> with the C-API for functions but I can't see concretely how one would include 
> an interface to a type.
> 
> 
> 
> Is it this? http://docs.python.org/release/2.7.3/extending/newtypes.html
> 
> 
> 
> Regards,
> 
> Aaron

Hi Aaron,

There are a few ways of doing this. At least three come to my mind:
1. Wrap the C++ type yourself by using handcrafted code implemented with the 
Python C API
2. Use SWIG to wrap the C++ code and (semi) automatically create the wrapper 
(http://www.swig.org/)
3. Use BOOST Python to wrap the C++ code 
(http://www.boost.org/doc/libs/1_51_0/libs/python/doc/index.html)

I would highly discourage (1) unless you are very brave and curious. Ofcourse 
it is a nice excercise, but if you want something to work quickly I would 
recommend to use either (2) or (3).

I have used both SWIG and BOOST Python and either of them worked pretty well 
for me. In the end I selected BOOST Python, because I was only interested in 
the Python wrapping (SWIG could generate many other wrappers as well).

Regards,

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


Re: Is there a way to create kernel log messages via Python?

2012-10-16 Thread Marco Nawijn
On Tuesday, October 16, 2012 5:43:28 AM UTC+2, J wrote:
> Hi...
> 
> 
> 
> I have a bit of code that does the following:
> 
> 
> 
> uses the syslog module to inject a LOG_INFO message into the syslog on
> 
> my linux machine
> 
> runs a suspend/resume cycle
> 
> uses the syslog module to inkect a LOG_INFO message marking the end of test.
> 
> 
> 
> Then I parse everything between the start and stop markers for certain
> 
> items that the Linux kernel logs during a suspend and resume cycle.
> 
> 
> 
> But my  "resume complete" timing is not as accurate as it could be.
> 
> The problem with doing it this way is that while I can find definite
> 
> kernel messages that mark various points in the suspend/resume cycle,
> 
> the final message when the kernel is done resuming is NOT the point I
> 
> actually want to mark.
> 
> 
> 
> Instead, the end point I want is the time of the ending marker itself,
> 
> as this happens after certain other things are done such as resumption
> 
> of networking services.
> 
> 
> 
> Here's the problem.  I can't just use syslog timestamps.  The reason
> 
> is that the syslog timestamps are only indicative of when messages are
> 
> written to syslog via syslogd.  The kernel timestamps are different.
> 
> For example, the following bits of log are taken from the time the
> 
> test starts until the end of the "going to sleep" kernel messages.
> 
> First, note that there's a 5 second difference between the START
> 
> marker and the first kernel message.  Next, look at the kernel
> 
> timestamps.  The total real time to suspend starts at 421320.380947
> 
> and ends at 421322.386355, around 2 seconds later, where the log
> 
> messages themselves all state that the events occurred at the same
> 
> time.
> 
> 
> 
> Oct 15 10:24:19 klaatu sleep_test: ---SLEEP TEST START 1350296656---
> 
> Oct 15 10:25:24 klaatu kernel: [421320.380947] PM: Syncing filesystems ... 
> done.
> 
> Oct 15 10:25:24 klaatu kernel: [421320.391282] PM: Preparing system
> 
> for mem sleep
> 
> [SNIP]
> 
> Oct 15 10:25:24 klaatu kernel: [421322.282943] Broke affinity for irq 23
> 
> Oct 15 10:25:24 klaatu kernel: [421322.386355] CPU 7 is now offline
> 
> 
> 
> So, what I REALLY want is to inject my start/stop markers into klogd
> 
> rather than syslogd.  This will, I hope, give my markers kernel
> 
> timestamps rather than syslog timestamps which are not as accurate.
> 
> 
> 
> So does anyone know of a way to do this?  Unfortunately, I've tried
> 
> some searching but google doesn't like the term klog, and most of the
> 
> hits involved injecting code or other things that are not related at
> 
> all.
> 
> 
> 
> Or, if there's a better way to get accurate timestamps, what would that be?

Hi,

I cannot be of too much help here, but is there a userspace/kernel C-interface 
that could be used. If so, you might be able to use the Python ctypes module to 
create messages at the correct moments

Regards,
Marco
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Which book is the best?

2012-10-16 Thread Marco Nawijn
On Tuesday, October 16, 2012 6:41:29 AM UTC+2, David Hutto wrote:
> On Tue, Oct 16, 2012 at 12:27 AM, 老爷  wrote:
> 
> > I have strong c++ development experience.  But now I want to study the
> 
> > python to do some windows setting task, such as editing file, changing the
> 
> > system setting, doing some network processing.  Please help me which book is
> 
> > the best?
> 
> >
> 
> >
> 
> Definitely command line apps/command line usage.
> 
> 
> 
>  I could recommend google searches, but use the calls to the OS, and
> 
> you can accomplish a good bit of things.
> 
> 
> 
> 
> 
> -- 
> 
> Best Regards,
> 
> David Hutto
> 
> CEO: http://www.hitwebdevelopment.com

Hi,

Although I agree with the fact the working in the interactive interpreter (may 
I recommend IPython for this) is definitely an efficient way of exploring the 
Python world, I also liked alot the Python Essential Reference (4th edition). 
Since you already understand how to program, the Python essential reference 
quickly guides you through Python language and the standard library.

Regards,

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


Re: numpy - 2D matrix/array - initialization like in Matlab...

2012-10-16 Thread Marco Nawijn
On Tuesday, October 16, 2012 12:43:09 AM UTC+2, someone wrote:
> On 10/15/2012 11:26 PM, MRAB wrote:
> 
> > On 2012-10-15 22:09, someone wrote:
> 
> >>
> 
> >> See this:
> 
> >>
> 
> >> ==
> 
> >> In [5]: Dx = numpy.matrix('1 0 0; 0 0.5 -0.5; 0 -0.5 1.5')
> 
> >>
> 
> >> In [6]: Dx
> 
> >> Out[6]:
> 
> >> matrix([[ 1. ,  0. ,  0. ],
> 
> >>   [ 0. ,  0.5, -0.5],
> 
> >>   [ 0. , -0.5,  1.5]])
> 
> >> ==
> 
> >>
> 
> >>
> 
> >>
> 
> >> Ok... So now test = 33 and instead of the value 1.5 I want to use the
> 
> >> value of "test" and put it directly into the matrix (or array):
> 
> >>
> 
> >> ==
> 
> >> In [7]: test=33
> 
> >>
> 
> >> In [8]: Dx = numpy.matrix('1 0 0; 0 0.5 -0.5; 0 -0.5 test')
> 
> >> ---
> 
> >>
> 
> >> NameError Traceback (most recent call
> 
> >> last)
> 
> >> /home/user/something/ in ()
> 
> >> > 1 Dx = numpy.matrix('1 0 0; 0 0.5 -0.5; 0 -0.5 test')
> 
> >>
> 
> >> /usr/lib/python2.7/dist-packages/numpy/matrixlib/defmatrix.pyc in
> 
> >> __new__(subtype, data, dtype, copy)
> 
> >>   252
> 
> >>   253 if isinstance(data, str):
> 
> >> --> 254 data = _convert_from_string(data)
> 
> >>   255
> 
> >>   256 # now convert data to an array
> 
> >> .. etc...
> 
> >> ==
> 
> >>
> 
> >>
> 
> >>
> 
> >> So obviously it doesn't understand that I want this:
> 
> >>
> 
> >> ==
> 
> >> In [21]: Dx[2,2]=test
> 
> >>
> 
> >> In [22]: Dx
> 
> >> Out[22]:
> 
> >> matrix([[  1. ,   0. ,   0. ],
> 
> >>   [  0. ,  33. ,  -0.5],
> 
> >>   [  0. ,  -0.5,  33. ]])
> 
> >> ==
> 
> >>
> 
> >> Without having to manually change all the individual places using my
> 
> >> variables (test is actually many variables, not just one but I think you
> 
> >> should understand the problem now).
> 
> >>
> 
> >>
> 
> >> How to initialize my array directly using variables ?
> 
> >>
> 
> >> It could also be that I wanted:
> 
> >>
> 
> >> test11 = 1
> 
> >> test12 = 1.5
> 
> >> test13 = 2
> 
> >> test21 = 0
> 
> >> test22 = 5
> 
> >>
> 
> >> Dx = numpy.matrix('test11 test12 test13; test21 test22 -0.5; 0 -0.5 1.5')
> 
> >>
> 
> >> Etc... for many variables...
> 
> >>
> 
> >> Appreciate ANY help, thank you very much!
> 
> >>
> 
> > What it prints should give you a hint:
> 
> >
> 
> >  >>> Dx = numpy.matrix([[test11, test12, test13], [test21, test22,
> 
> > -0.5], [0, -0.5, 1.5]])
> 
> >  >>> Dx
> 
> > matrix([[ 1. ,  1.5,  2. ],
> 
> >  [ 0. ,  5. , -0.5],
> 
> >  [ 0. , -0.5,  1.5]])
> 
> 
> 
> Uh, great - thank you very much!
> 
> 
> 
> As you maybe see, I'm only a python newbie so I'm not so good at 
> 
> understanding the error messages and reading the source code yet.
> 
> 
> 
> Thank you very much for the solution to the problem! It's highly 
> 
> appreciated. Thanks.

Hi,

Also note that you don't need to initialize the array with a string. You could 
directly do it like this:

>>> a = numpy.array(((1,2,3), (2,3,4), (4,5,6)))

Other things that might be interesting for you are:

# List comprehension (standard python) to convert strings to floats
>>> vals = [ float(s) for s in "1.0 2.3 1.2".split() ]
produces [1.0, 2.3, 1.2]
>>> vals = [ float(s) for s in ("1.0", "2.3", "1.2") ]
produces again [1.0, 2.3, 1.2]

Also lookup the documentation for numpy.reshape. With this you could provide a 
single list of for example 9 numbers and reshape it into a 3x3 array.

Python and Numpy are so cool!!

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


Re: Python on Windows

2012-10-16 Thread Marco Nawijn
On Tuesday, October 16, 2012 1:29:23 PM UTC+2, graham wrote:
> Downloaded and installed Python 2.7.3 for windows (an XP machine).
> 
> 
> 
> Entered the Python interactive interpreter/command line and typed the 
> 
> following:
>
> 
> 
>   >>>import feedparser
> 
> 
> 
> and I get the error message "No module named feedparser".
> 
> 
> 
> There is a feedparser.py file lurking around - so I suppose Python 
> 
> cannot find it.
> 
> 
> 
> Anyone: What to do?
> 
> 
> 
> 
> 
> 
> 
> GC
Hi,

feedparser.py is not a Python standard library. So, if it feedparser is located 
in a non-standard folder you have at least the following two options:
1. Update the PYTHONPATH environment variable such that it includes the path 
the installation location of feedparser.py

2. Add the path to feedparser.py directly in the script that uses it. Something 
like the following:
import sys
sys.path.append("path to feedparser.py")

import feedparser

Regards,

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


Re: Providing a Python wrapper to a C++ type.

2012-10-16 Thread Marco Nawijn
On Tuesday, October 16, 2012 1:39:44 PM UTC+2, Stefan Behnel wrote:
> Marco Nawijn, 16.10.2012 12:17:
> 
> > On Tuesday, October 16, 2012 10:11:52 AM UTC+2, aaron.l...@gmail.com wrote:
> 
> >> I have a C++ module where I have a defined, working type. How would I
> 
> >> make a wrapper for this type to be able to be used in Python? I am
> 
> >> familiar(-ish) with the C-API for functions but I can't see concretely how
> 
> >> one would include an interface to a type.
> 
> >>
> 
> >> Is it this? http://docs.python.org/release/2.7.3/extending/newtypes.html
> 
> > 
> 
> > There are a few ways of doing this. At least three come to my mind:
> 
> > 1. Wrap the C++ type yourself by using handcrafted code implemented with 
> > the Python C API
> 
> > 2. Use SWIG to wrap the C++ code and (semi) automatically create the 
> > wrapper (http://www.swig.org/)
> 
> > 3. Use BOOST Python to wrap the C++ code 
> > (http://www.boost.org/doc/libs/1_51_0/libs/python/doc/index.html)
> 
> > 
> 
> > I would highly discourage (1) unless you are very brave and curious. 
> > Ofcourse it is a nice excercise, but if you want something to work quickly 
> > I would recommend to use either (2) or (3).
> 
> > 
> 
> > I have used both SWIG and BOOST Python and either of them worked pretty 
> > well for me. In the end I selected BOOST Python, because I was only 
> > interested in the Python wrapping (SWIG could generate many other wrappers 
> > as well).
> 
> 
> 
> There's also Cython, which provides a very flexible way (unlike SWIG) of
> 
> doing these things easily (unlike C++ with Boost).
> 
> 
> 
> http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html
> 
> 
> 
> I agree with discouraging 1) in specific.
> 
> 
> 
> Stefan

Hi Stefan,

I never worked with Cython (but I know it is very powerful and interesting) but 
in my mind there are slight differences in usage scenario between e.g. Boost 
Python and Cython. For me the idea of Cython is that your main code is in 
Python, but you want to improve the performance of specific parts of the code. 
In that case, Cython is the way to go. In case of Boost Python, the scenario 
for me is that you have a main program/library in C++, but you want to be able 
use the functionality from Python. 

Do you agree with this view?

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


Re: Printing a text over an image

2012-11-15 Thread Marco Nawijn
On Wednesday, November 7, 2012 5:52:36 PM UTC+1, Martha Morrigan wrote:
> Hi guys,
> 
> 
> 
> Using python, wxpython and sqlite in a windows system, Im trying to
> 
> print some certificates/diplomas/cards with a image at background with
> 
> the name of person/text over it.
> 
> 
> 
> I know the basic steps to print the text using win32print from Pywin32
> 
> but...:
> 
> 
> 
> 1) I dont know how to add an image and set it to background.
> 
> 
> 
>while .
> 
> 
> 
> .
> 
> 
> 
> # Query sqlite rows and collumn name and set the self.text for
> 
> each certificate
> 
> 
> 
> .
> 
> 
> 
> # Now send to printer
> 
> 
> 
> DC = win32ui.CreateDC()
> 
> DC.CreatePrinterDC(win32print.GetDefaultPrinter())
> 
> 
> 
> DC.SetMapMode(win32con.MM_TWIPS)
> 
> 
> 
> DC.StartDoc("Certificates Job")
> 
> 
> 
> DC.StartPage()
> 
> 
> 
> ux = 1000
> 
> uy = -1000
> 
> lx = 5500
> 
> ly = -55000
> 
> 
> 
> DC.DrawText(self.text, (ux, uy, lx, ly),win32con.DT_LEFT)
> 
> 
> 
> DC.EndPage()
> 
> DC.EndDoc()
> 
> 
> 
> This printer-code is inside a while loop calling each people name from
> 
> a sqlite database per check condition.
> 
> 
> 
> 
> 
> 2) All the names of database was printed at same page... how i command
> 
> the printer to spit out 1 page per name from the database?
> 
> 
> 
> 
> 
> 3) Any more simple approach or module to deals with printers (paper
> 
> and/or pdf) will be welcome.
> 
> 
> 
> Thanks in advance,
> 
> 
> 
> Martha

Hi Martha,

Since you are on windows, why don't you use MS/Word directly from Python. It 
has all the abstractions you need (documents, pages, tables, figures, printing 
etc.). Working with MS/Word through the win32 bindings is really simple.
I have done this a while ago for some automatic report generation. The basic 
routine is to start recording your actions with the MS/Word macro recorder, do 
the things you want to do, stop recording, look at the VB code and guess the 
equivalent Python code. This is not as bad as it sounds. It normally is really 
straightforward.

I am on Linux at the moment, so I cannot present any code examples. Feel free 
to try and post some example code if you get stuck.

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


Re: Beginner Tutorials

2013-01-18 Thread marco . kretz
Am Freitag, 18. Januar 2013 15:47:52 UTC+1 schrieb Rik:
> Hi, I've developed a website for beginners to Python. I'd appreciate any 
> comments or criticism. It's still under development, and should be finished 
> in the next few months. Oh, and it's free to use.
> 
> 
> 
> www.usingpython.com

Very nice and clean structure, I like it!

But I would recommend to place the menu right below the header ;)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dynamically creating classes from text

2012-01-24 Thread Marco Nawijn
On Jan 24, 6:22 pm, T H  wrote:
> I’m new to python, sorry if my question is a bit naive, I was
> wondering if it is possible to parse some text (ie. from a text file
> or say html) and then dynamically create a class?
>
> for example lets say the contents of the text file is:
>
>      functionName: bark  arg1: numberBarks
>      functionName: run    arg1: howFast  arg2: howLong
>
> and then from the text dynamically create a class like the one below
> (including the functions and its implementation)
>
> class Dog:
>         def bark(self, numberBarks, myArg):
>                 print(‘numberBarks: ‘ + numberBarks)
>                 print(‘myArg’ + myArg)
>                 return
>         def run(self, howFast, howLong, myArg):
>                 print(‘howFast: ‘ + howFast)
>                 print(‘howLong’ + howLong)
>                 print(‘myArg’ + myArg)
>                 return
>
> I know my question is a bit far fetched. Anyhow if it is possible how
> is it done?
> Thanks so much for any help!

Hi,

You could also take a look at the 'type' builtin. It can be used to
dynamically create a class. It allows you to specify the class name,
the bases and a dictionary containing the attributes. A simple
example:

>> aClass = type('A', (object, ), { 'a' : 10, 'b' : 'Hello World' })
>> aobj = aClass()
>> aobj.__class__.__name__
'A'
>> aobj.a
10

The methods might be a little more difficult to attach to the class,
but I don't fully understand your problem. In particular, where does
the implementation of your methods come from?

Marco

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


Re: str.isnumeric and Cuneiforms

2012-05-18 Thread Marco Buttu

On 05/18/2012 02:50 AM, Steven D'Aprano wrote:


Is it normal the str.isnumeric() returns False for these Cuneiforms?
>
>  '\U00012456'
>  '\U00012457'
>  '\U00012432'
>  '\U00012433'
>
>  They are all in the Nl category.



Are you sure about that? Do you have a reference?


I I was just playing with Unicode on Python 3.3a:

>>> from unicodedata import category, name
>>> from sys import maxunicode

>>> nl = [chr(c) for c in range(maxunicode + 1) \
... if category(chr(c)).startswith('Nl')]

>>> numerics = [chr(c) for c in range(maxunicode + 1) \
... if chr(c).isnumeric()]

>>> for c in set(nl) - set(numerics):
... print(hex(ord(c)), category(c), unicodedata.name(c))
...
0x12432 Nl CUNEIFORM NUMERIC SIGN SHAR2 TIMES GAL PLUS DISH
0x12433 Nl CUNEIFORM NUMERIC SIGN SHAR2 TIMES GAL PLUS MIN
0x12456 Nl CUNEIFORM NUMERIC SIGN NIGIDAMIN
0x12457 Nl CUNEIFORM NUMERIC SIGN NIGIDAESH

So they are in the Nl category but are not "numerics", and that sounds 
strange because other Cuneiforms are "numerics":


>>> '\U00012455'.isnumeric(), '\U00012456'.isnumeric()
(True, False)


It seems to me that they are not:


py>  c = '\U00012456'
py>  import unicodedata
py>  unicodedata.numeric(c)
Traceback (most recent call last):
   File "", line 1, in
ValueError: not a numeric character


Exactly, as I wrote above, is that right?

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


Re: str.isnumeric and Cuneiforms

2012-05-18 Thread Marco Buttu

On 05/18/2012 11:24 AM, jmfauth wrote:


Is it normal the str.isnumeric() returns False for these Cuneiforms?
>
>  '\U00012456'
>  '\U00012457'
>  '\U00012432'
>  '\U00012433'
>
>  They are all in the Nl category.

Indeed there are, but Unicode (ver. 5.0.0) does not assign numeric
values to these code points.


help(unicodedata) says Python 3.3a refers  to Unicode 6.0.0

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


Re: str.isnumeric and Cuneiforms

2012-05-18 Thread Marco Buttu

On 05/17/2012 09:32 PM, Marco wrote:


Is it normal the str.isnumeric() returns False for these Cuneiforms?

'\U00012456'
'\U00012457'
'\U00012432'
'\U00012433'

They are all in the Nl category.

Marco


It's ok, I found that they don't have a number assigned in the 
ftp://ftp.unicode.org/Public/UNIDATA/UnicodeData.txt database.

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


Re: str.isnumeric and Cuneiforms

2012-05-18 Thread Marco Buttu

On 05/18/2012 05:42 PM, jmfauth wrote:


Non official but really practical:

http://www.fileformat.info/info/unicode/index.htm


Very well ordered, thanks

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


Re: Passing array from java to python

2011-06-03 Thread Marco Nawijn
On Jun 2, 11:54 am, loial  wrote:
> I need to pass some sort of array or hashmap from Java and read the
> data in a python script (which will be called by the java class). Is
> there any neater way  to do this other than just passing strings?

I recently had to deal with the same problem, some bi-directional
communication between Java and Python. Several options were discussed
between me and my fellow programmer. In the end we settled for XML-
rpc. It works remarkably well in our case. We use it to pass test and
simulation data to GUI code. XML-rpc is very well supported in python.
Basic types (lists, dicts etc.) are encoded automatically. If the
arrays are very large, I would probably use an intermediate database
(e.g. Hdf5) for storage and then use some sort of messaging to inform
the Java code of any changes.

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


Re: losing-end-of-row values when manipulating CSV input

2011-07-13 Thread Marco Nawijn
On Jul 13, 10:22 pm, Neil Berg  wrote:
> Hello all,
>
> I am having an issue with my attempts to accurately filter some data from a 
> CSV file I am importing.  I have attached both a sample of the CSV data and 
> my script.  
>
> The attached CSV file contains two rows and 27 columns of data.  The first 
> column is the station ID "BLS", the second column is the sensor number "4", 
> the third column is the date, and the remaining 24 columns are hourly 
> temperature readings.
>
> In my attached script, I read in row[3:] to extract just the temperatures, do 
> a sanity check to make sure there are 24 values, remove any missing or "m" 
> values, and then append the non-missing values into the "hour_list".  
>
> Strangely the the first seven rows appear to be empty after reading into the 
> CSV file, so that's what I had to incorporate the if len(temps) == 24 
> statement.  
>
> But the real issue is that for days with no missing values, for example the 
> second row of data, the length of the hour_list should be 24.  My script, 
> however, is returning 23.  I think this is because the end-of-row-values have 
> a trailing "\". This must mark these numbers as non-digits and are lost in my 
> "isdig" filter line.  I've tried several ways to remove this trailing "\", 
> but to no success.
>
> Do you have any suggestions on how to fix this issue?
>
> Many thanks in advance,
>
> Neil Berg
>
>  csv_test.py
> 1KViewDownload
>
>  csv_sample.csv
> < 1KViewDownload

Dear Neil,

Don't know if this is a double post (previous post seems to be gone),
but val = val.rstrip('\\') should fix your problem. Note the double
backslash.

Kind regards,

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


Re: losing-end-of-row values when manipulating CSV input

2011-07-13 Thread Marco Nawijn
On Jul 13, 10:22 pm, Neil Berg  wrote:
> Hello all,
>
> I am having an issue with my attempts to accurately filter some data from a 
> CSV file I am importing.  I have attached both a sample of the CSV data and 
> my script.  
>
> The attached CSV file contains two rows and 27 columns of data.  The first 
> column is the station ID "BLS", the second column is the sensor number "4", 
> the third column is the date, and the remaining 24 columns are hourly 
> temperature readings.
>
> In my attached script, I read in row[3:] to extract just the temperatures, do 
> a sanity check to make sure there are 24 values, remove any missing or "m" 
> values, and then append the non-missing values into the "hour_list".  
>
> Strangely the the first seven rows appear to be empty after reading into the 
> CSV file, so that's what I had to incorporate the if len(temps) == 24 
> statement.  
>
> But the real issue is that for days with no missing values, for example the 
> second row of data, the length of the hour_list should be 24.  My script, 
> however, is returning 23.  I think this is because the end-of-row-values have 
> a trailing "\". This must mark these numbers as non-digits and are lost in my 
> "isdig" filter line.  I've tried several ways to remove this trailing "\", 
> but to no success.
>
> Do you have any suggestions on how to fix this issue?
>
> Many thanks in advance,
>
> Neil Berg
>
>  csv_test.py
> 1KViewDownload
>
>  csv_sample.csv
> < 1KViewDownload

Hello Neil,

I just had a quick look at your script. To remove the trailing "\" you
can use val = val.rstrip('\\') in your script. Note the double
backslash.

The script now returns 24 items in the hour_list.

Good luck!

Marco


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


Re: Tree structure

2011-07-26 Thread Marco Nawijn
On Jul 26, 6:53 am, Bevan Jenkins  wrote:
> Hello,
>
> I am trying to create a tree structure for use with a PyQt QTreeView.
> But first I need to get my head around how to create the tree
> structure.  I have a dictionary (for testing purposes) but I will
> later use a table via sqlalchemy.
>
> The use case is hydrology, so I  would like to have a hydrologically
> connected river tree, in which you can browse upstream from the sea
> (making choices) or downstream from any named hydrological feature.
> Each key flows into its value pair. myrivers =
> {"river":"flows_into"}.  An example is below:
>
> myrivers = {"little stream":"sea",
>     "mountain stream":"lake",
>     "lake":"big river",
>     "cold spring":"big river",
>     "big river":"sea"
>     "sea":""}
>
> I would like the tree to look like (if the formatting works). so
> you can browse downstream from each named river but also upstream from
> the sea picking which direction to go.
>
> little stream
>     sea
> mountain stream
>     lake
>         big river
>             sea
> lake
>     big river
>         sea
> cold spring
>     big river
>         sea
> big river
>     sea
> sea
>     little stream
>     big river
>         lake
>             mountain stream
>         cold spring
>
> <>
> So every key is a parent.  For all keys that have a value (not ""),
> the value is the child and is then used as a parent to get the next
> child until the sea and a value of "" is reached.  For the sea this is
> reversed, that you find all rivers that flow into the sea and then all
> rivers that flow into them.
>
> Any thoughts about how to acomplish this will be much appreciated,
> Bevan

Hello Bevan,

Is it an option to use XML as an in-memory representation. It
naturally provides the
interface you need, like traversing from parent to children and back.
In addition, you
get querying capabilities like XPATH for free. In python I recommend
lxml.

Your task than is to fill the tree based on the information in the
database. The ORM functionality
from sqlalchemy will be of help here. In addition, you somehow have to
populate the QT tree with the
data from your in-memory XML representation. I have no experience with
QT so I cannot help you there.

Regards,

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


Re: debugging segfaults in pythen PyQt (QWebview)

2011-03-04 Thread Marco Bizzarri
This applies to debugging a spinning Zope server, but I think you can adapt
the suggestions to your core dump:

http://www.upfrontsystems.co.za/Members/jean/zope-notes/debug-spinning-zope

Regards
Marco

On Thu, Mar 3, 2011 at 10:40 AM, Gelonida  wrote:

> Hi,
>
> I have a QWebview application, which segfaults rather often,
> but not all the time.
>
> I assume it is some kind of race condition when loading a certain web
> page with quite some built in AJax.
>
>
>
> How can I debug it?
>
> The application crashes under Windows and under Linux.
>
>
> I enabled already core dumps and am able to
>
> start
>
> gdb python.exe core
>
> I guess the command "bt"
> will be able to give m a backtrace of  the C program
>
>
> Is there any way to obtain the related  backtrace of the python script?
>
>
> I'm at a complete loss of what I am doing wrong in my script and would
> hope to get at least some indication.
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Marco Bizzarri
http://code.google.com/p/qt-asterisk/
http://notenotturne.blogspot.com/
http://iliveinpisa.blogspot.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


In List Query -> None Case Sensitive?

2011-03-31 Thread Wehe, Marco
Hi,

I am doing a search through a list of files but the text the casing doesn't 
match. My list is all upper case but the real files are all different. Is there 
a smooth way of searching through the list without going full on regular 
expressions?

path = "V:\\Jinsy\\incoming\\assets"

media=["LIHOU ISLAND.MOV", "MVI_1449.MOV"]

def FindMedia(path):
result = []

for root, dirs, files in os.walk(path):
for iFile in files:

if iFile in media:
filePath = 
os.path.join(root, iFile)

result.append(filePath)

return result


for filePath in FindMedia(path):
log(filePath)



This is the real file name that I can't find:
Lihou Island.mov

Thanks a lot,
Marco
[sky]
Marco Wehe
Visual Effects TD
VFX/3D
BSkyB, Sky 2, Grant Way, Isleworth, Middlesex TW7 5QD
t: +44 (0) 20 7805 8035
f: +44 (0) 20 7805 6577
e: marco.w...@bskyb.com<mailto:smarco.w...@bskyb.com>



Information in this email including any attachments may be privileged, 
confidential and is intended exclusively for the addressee. The views expressed 
may not be official policy, but the personal views of the originator. If you 
have received it in error, please notify the sender by return e-mail and delete 
it from your system. You should not reproduce, distribute, store, retransmit, 
use or disclose its contents to anyone. Please note we reserve the right to 
monitor all e-mail communication through our internal and external networks. 
SKY and the SKY marks are trade marks of British Sky Broadcasting Group plc and 
are used under licence. British Sky Broadcasting Limited (Registration No. 
2906991), Sky Interactive Limited (Registration No. 3554332), Sky-In-Home 
Service Limited (Registration No. 2067075) and Sky Subscribers Services Limited 
(Registration No. 2340150) are direct or indirect subsidiaries of British Sky 
Broadcasting Group plc (Registration No. 2247735). All of the companies 
mentioned in this paragraph are incorporated in England and Wales and share the 
same registered office at Grant Way, Isleworth, Middlesex TW7 5QD.

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


Suggestions on programming in Python an email simple client

2018-02-13 Thread Maroso Marco
Hi,

what i'm trying to do is develop my own email client, but a simple one.

I just want it to connect to a specific email account and read the subject line 
of messages coming from a certain email address.

I then want it to be able to execute the command i wrote on the subject.

It would be nice if i could specify two parameters on the subject line

first : password;
second : command;

The first parameter is the password and the second is the command to be 
executed in the local pc where this program is running.

The program will have to show only email coming from a specific account
and if the mail is unread then it should read the subject line and find 
parameters.
The first parameter must match my hardcoded password, and the second parameter 
must be executed on the local pc (example ... c:\windows\notepad.exe)

It must check for new emails at a given timing (example ...every 30 seconds )
 (the timing can be hardcoded or specified in a separate file)

This is it.

Any suggestions?

I have started doing this (but i never programmed in python before):


import imaplib
import os
import email
import email.header

plusmail = "emailaddresstobechec...@gmail.com"
googlepass = "mypassword"
owner = "validsen...@gmail.com"

M = imaplib.IMAP4_SSL('imap.gmail.com')
M.login(plusmail, googlepass)
M.select()

status, response = M.search(None, '(UNSEEN)')
unread_msg_nums = response[0].split()

# Print the count of all unread messages
print (len(unread_msg_nums))

# Print all unread messages from a certain sender of interest
status, response = M.search(None, '(UNSEEN)', '(FROM "%s")' % (owner))
unread_msg_nums = response[0].split()
da = []
for e_id in unread_msg_nums:
_, response = imap.fetch(e_id, '(BODY.PEEK[HEADER.FIELDS (From Subject)] 
RFC822.SIZE)')
da.append(response[0][1])
# print (da)

# Mark them as seen
for e_id in unread_msg_nums:
imap.store(e_id, '+FLAGS', '\Seen')



typ, data = M.search(None, 'From',(owner))
for num in data[0].split():
typ, data = M.fetch(num, '(RFC822)')
print ('Message %s\n%s\n' % (num, data[0][1]))
M.close()
M.logout()

I know there are duplicates but i need to figure out how to build this 
correctly 


Thanks in advance for any help given.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Suggestions on programming in Python an email simple client

2018-02-13 Thread Maroso Marco
Il giorno martedì 13 febbraio 2018 21:18:37 UTC+1, Chris Angelico ha scritto:
> On Wed, Feb 14, 2018 at 7:05 AM, Maroso Marco  wrote:
> > Hi,
> >
> > what i'm trying to do is develop my own email client, but a simple one.
> >
> > I just want it to connect to a specific email account and read the subject 
> > line of messages coming from a certain email address.
> >
> > I then want it to be able to execute the command i wrote on the subject.
> 
> Be aware that "from" addresses can easily be forged. I suggest
> whitelisting a set of valid commands and executing only those. If you
> want a REAL way to manage your home system remotely, I would recommend
> SSH instead.
> 
> > It would be nice if i could specify two parameters on the subject line
> >
> > first : password;
> > second : command;
> >
> > The first parameter is the password and the second is the command to be 
> > executed in the local pc where this program is running.
> 
> That's an improvement, but since the password is being transmitted in
> clear text, it won't prevent attacks. It does mean you won't try to
> execute the subject lines of random spam, though, so accidents will be
> reduced (or eliminated).
> 
> > The program will have to show only email coming from a specific account
> > and if the mail is unread then it should read the subject line and find 
> > parameters.
> > The first parameter must match my hardcoded password, and the second 
> > parameter must be executed on the local pc (example ... 
> > c:\windows\notepad.exe)
> >
> > It must check for new emails at a given timing (example ...every 30 seconds 
> > )
> >  (the timing can be hardcoded or specified in a separate file)
> >
> > This is it.
> >
> > Any suggestions?
> 
> Seems perfectly possible.
> 
> > I have started doing this (but i never programmed in python before):
> >
> >
> > import imaplib
> > import os
> > import email
> > import email.header
> >
> > plusmail = "emailaddresstobechec...@gmail.com"
> > googlepass = "mypassword"
> > owner = "validsen...@gmail.com"
> >
> > M = imaplib.IMAP4_SSL('imap.gmail.com')
> > M.login(plusmail, googlepass)
> > M.select()
> >
> > status, response = M.search(None, '(UNSEEN)')
> > unread_msg_nums = response[0].split()
> >
> > # Print the count of all unread messages
> > print (len(unread_msg_nums))
> >
> > # Print all unread messages from a certain sender of interest
> > status, response = M.search(None, '(UNSEEN)', '(FROM "%s")' % (owner))
> > unread_msg_nums = response[0].split()
> > da = []
> > for e_id in unread_msg_nums:
> > _, response = imap.fetch(e_id, '(BODY.PEEK[HEADER.FIELDS (From 
> > Subject)] RFC822.SIZE)')
> > da.append(response[0][1])
> > # print (da)
> >
> > # Mark them as seen
> > for e_id in unread_msg_nums:
> > imap.store(e_id, '+FLAGS', '\Seen')
> >
> >
> >
> > typ, data = M.search(None, 'From',(owner))
> > for num in data[0].split():
> > typ, data = M.fetch(num, '(RFC822)')
> > print ('Message %s\n%s\n' % (num, data[0][1]))
> > M.close()
> > M.logout()
> >
> > I know there are duplicates but i need to figure out how to build this 
> > correctly
> 
> Seems like a reasonable start. So what's the code doing wrong, or
> failing to do? Is it failing with an exception? Is it running
> successfully, but not doing something you want it to do?
> 
> ChrisA

Hi Chris and thanks for replying.

I came up with the following code (thanks for the security suggestions-i'm just 
testing) :

My code actually works fine allthough i have a couple of problems :

1 - once parced the messages and executed the command in the subject line, it 
keeps looping even if i told to only execute on unread messages
2 - it loops continuosly instead of waiting the correct wait time
3 - i had to add a delete statement so that the program stops looping at least 
on processing the commands.

Can you help me sort out the issue? Maybe it's an indent problem!


Here is the code : 

import imaplib
import os
import email
import email.header
import time

def mailcontroller():

# Set user, pass and allowed mail for giving commands
plusmail = "emailaddresstobechec...@gmail.com"
googlepass = "thesecretpassword"
captain = "theallowedemailaddr...@gmail.com"

# Set vars for IMAP access
M = 

Re: Suggestions on programming in Python an email simple client

2018-02-15 Thread Maroso Marco
Il giorno martedì 13 febbraio 2018 21:06:19 UTC+1, Maroso Marco ha scritto:
> Hi,
> 
> what i'm trying to do is develop my own email client, but a simple one.
> 
> I just want it to connect to a specific email account and read the subject 
> line of messages coming from a certain email address.
> 
> I then want it to be able to execute the command i wrote on the subject.
> 
> It would be nice if i could specify two parameters on the subject line
> 
> first : password;
> second : command;
> 
> The first parameter is the password and the second is the command to be 
> executed in the local pc where this program is running.
> 
> The program will have to show only email coming from a specific account
> and if the mail is unread then it should read the subject line and find 
> parameters.
> The first parameter must match my hardcoded password, and the second 
> parameter must be executed on the local pc (example ... 
> c:\windows\notepad.exe)
> 
> It must check for new emails at a given timing (example ...every 30 seconds )
>  (the timing can be hardcoded or specified in a separate file)
> 
> This is it.
> 
> Any suggestions?
> 
> I have started doing this (but i never programmed in python before):
> 
> 
> import imaplib
> import os
> import email
> import email.header
> 
> plusmail = "emailaddresstobechec...@gmail.com"
> googlepass = "mypassword"
> owner = "validsen...@gmail.com"
> 
> M = imaplib.IMAP4_SSL('imap.gmail.com')
> M.login(plusmail, googlepass)
> M.select()
> 
> status, response = M.search(None, '(UNSEEN)')
> unread_msg_nums = response[0].split()
> 
> # Print the count of all unread messages
> print (len(unread_msg_nums))
> 
> # Print all unread messages from a certain sender of interest
> status, response = M.search(None, '(UNSEEN)', '(FROM "%s")' % (owner))
> unread_msg_nums = response[0].split()
> da = []
> for e_id in unread_msg_nums:
> _, response = imap.fetch(e_id, '(BODY.PEEK[HEADER.FIELDS (From Subject)] 
> RFC822.SIZE)')
> da.append(response[0][1])
> # print (da)
> 
> # Mark them as seen
> for e_id in unread_msg_nums:
> imap.store(e_id, '+FLAGS', '\Seen')
> 
> 
> 
> typ, data = M.search(None, 'From',(owner))
> for num in data[0].split():
> typ, data = M.fetch(num, '(RFC822)')
> print ('Message %s\n%s\n' % (num, data[0][1]))
> M.close()
> M.logout()
> 
> I know there are duplicates but i need to figure out how to build this 
> correctly 
> 
> 
> Thanks in advance for any help given.

OK, i came up to this solution, but now i tried to compile and run as a 
service, but it doesnt seem to work, not fetching emails and reading comand.

# A SCRIPT FROM MININGFELLOWS By CrazyLion
import imaplib
import os
import email
import email.header
import time
import subprocess

def mailcontroller():

# Set user, pass and allowed mail for giving commands
plusmail = "mailmailaddress.com"
googlepass = "thepassword"
captain = "autorizedm...@gmail.com"

# Set vars for IMAP access
M = imaplib.IMAP4_SSL('imap.gmail.com')
M.login(plusmail, googlepass)
M.select()

# Set search on UNSEEN messages
status, response = M.search(None, '(UNSEEN)')
unread_msg_nums = response[0].split()


# Mark as read
for e_id in unread_msg_nums:
M.store(e_id, '+FLAGS', '\Seen')

# cycle messages sent from autorized email address
typ, data = M.search(None, 'From',(captain))

for num in data[0].split():
typ, data = M.fetch(num, '(RFC822)')
msg = email.message_from_string(data[0][1])
decode = email.header.decode_header(msg['Subject'])[0]
subject = unicode(decode[0])
comando = subject

if googlepass in subject:

# print 'Message %s: %s' % (num, subject)
# Split subject line
googlepass,comando = subject.split(";")
# Execute command
#os.system(comando)
# Execute command with alternate method
subprocess.call(comando)
# Delete email
M.store(num, '+FLAGS', '\\Deleted')

M.close()
M.logout()

# Read ini file for timer settings
timing = open('timing.ini', 'r').read()
# Convert timer value from string to int
time.sleep(int(timing))

while True:
mailcontroller()




Any suggestions on how to compile it to run as a service.
I've tried to compile it in 32 and 64 bit version.
32 bit version runs ok, the 64 shuts after some seconds.

How to run comands like "net stop spooler" or "notepad" or "c:\file.bat" but as 
administrator (i mean with admin rights)

Tried to create a service based on my compiled version, it is in the processes
but it does not do anything.

Thanks really.




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


How to transform this as a service

2018-02-20 Thread Maroso Marco
Hi everyone, i need this program to run as a service.

This program basically check an email account (gmail) and looks if there is
mail from a specific email account, then reads the subject line and splits the 
text found assigning the left part (before the ; ) to the password, and the 
right part to the comand to be executed.
It should loop continuosly waiting a specific time given in a file called 
timing.ini (seconds)

My program seems to have a problem and i don't understand which! Firstly it 
worked and now just went to run it again and it doesnt. I have different 
versions of python installed on my pc.

Can someone please tell me how to transform it in a service to run on Windows?

I found some solutions but i don't understand how to implement it correctly
This is my code, any help is really apreciated, i'm loosing my nights on this 
and can't get it working.




import imaplib
import os
import email
import email.header
import time
import subprocess

def mailcontroller():

# Set user, pass and allowed mail for giving commands
plusmail = "anemailaddr...@gmail.com"
googlepass = "thepassword"
captain = "autorizedemailacco...@gmail.com"

# Set vars for IMAP access
M = imaplib.IMAP4_SSL('imap.gmail.com')
M.login(plusmail, googlepass)
M.select()

# Set search on UNSEEN messages
status, response = M.search(None, '(UNSEEN)')
unread_msg_nums = response[0].split()


# Mark as read
for e_id in unread_msg_nums:
M.store(e_id, '+FLAGS', '\Seen')

# cycle messages sent from autorized email address
typ, data = M.search(None, 'From',(captain))

for num in data[0].split():
typ, data = M.fetch(num, '(RFC822)')
msg = email.message_from_string(data[0][1])
decode = email.header.decode_header(msg['Subject'])[0]
subject = unicode(decode[0])
comando = subject

if googlepass in subject:

# print 'Message %s: %s' % (num, subject)
# Split subject line
googlepass,comando = subject.split(";")
# Execute command
#os.system(comando)
# Execute command with alternate method
subprocess.call(comando)
# Delete email
M.store(num, '+FLAGS', '\\Deleted')

M.close()
M.logout()

# Read ini file for timer settings
timing = open('timing.ini', 'r').read()
# Convert timer value from string to int
time.sleep(int(timing))

while True:
mailcontroller()










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


Re: How to transform this as a service

2018-02-20 Thread Maroso Marco
Il giorno martedì 20 febbraio 2018 23:42:27 UTC+1, Maroso Marco ha scritto:
> Hi everyone, i need this program to run as a service.
> 
> This program basically check an email account (gmail) and looks if there is
> mail from a specific email account, then reads the subject line and splits 
> the text found assigning the left part (before the ; ) to the password, and 
> the right part to the comand to be executed.
> It should loop continuosly waiting a specific time given in a file called 
> timing.ini (seconds)
> 
> My program seems to have a problem and i don't understand which! Firstly it 
> worked and now just went to run it again and it doesnt. I have different 
> versions of python installed on my pc.
> 
> Can someone please tell me how to transform it in a service to run on Windows?
> 
> I found some solutions but i don't understand how to implement it correctly
> This is my code, any help is really apreciated, i'm loosing my nights on this 
> and can't get it working.
> 
> 
> 
> 
> import imaplib
> import os
> import email
> import email.header
> import time
> import subprocess
> 
> def mailcontroller():
> 
> # Set user, pass and allowed mail for giving commands
> plusmail = "anemailaddr...@gmail.com"
> googlepass = "thepassword"
> captain = "autorizedemailacco...@gmail.com"
> 
> # Set vars for IMAP access
> M = imaplib.IMAP4_SSL('imap.gmail.com')
> M.login(plusmail, googlepass)
> M.select()
> 
> # Set search on UNSEEN messages
> status, response = M.search(None, '(UNSEEN)')
> unread_msg_nums = response[0].split()
> 
> 
> # Mark as read
> for e_id in unread_msg_nums:
> M.store(e_id, '+FLAGS', '\Seen')
> 
> # cycle messages sent from autorized email address
> typ, data = M.search(None, 'From',(captain))
> 
> for num in data[0].split():
> typ, data = M.fetch(num, '(RFC822)')
> msg = email.message_from_string(data[0][1])
> decode = email.header.decode_header(msg['Subject'])[0]
> subject = unicode(decode[0])
> comando = subject
> 
> if googlepass in subject:
> 
> # print 'Message %s: %s' % (num, subject)
> # Split subject line
> googlepass,comando = subject.split(";")
> # Execute command
> #os.system(comando)
> # Execute command with alternate method
> subprocess.call(comando)
> # Delete email
> M.store(num, '+FLAGS', '\\Deleted')
> 
> M.close()
> M.logout()
> 
> # Read ini file for timer settings
> timing = open('timing.ini', 'r').read()
> # Convert timer value from string to int
> time.sleep(int(timing))
> 
> while True:
> mailcontroller()

I would like to implement my code in something like this described here : 
http://www.chrisumbel.com/article/windows_services_in_python
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Is there are good DRY fix for this painful design pattern?

2018-02-26 Thread marco . nawijn
On Monday, February 26, 2018 at 3:44:14 PM UTC+1, Steven D'Aprano wrote:
> I have a class with a large number of parameters (about ten) assigned in 
> `__init__`. The class then has a number of methods which accept 
> *optional* arguments with the same names as the constructor/initialiser 
> parameters. If those arguments are None, the defaults are taken from the 
> instance attributes.
> 
> An example might be something like this:
> 
> 
> class Foo:
> def __init__(self, bashful, doc, dopey, grumpy, 
>happy, sleepy, sneezy):
> self.bashful = bashful  # etc
> 
> def spam(self, bashful=None, doc=None, dopey=None, 
>grumpy=None, happy=None, sleepy=None,
>sneezy=None):
> if bashful is None:
> bashful = self.bashful
> if doc is None:
> doc = self.doc
> if dopey is None:
> dopey = self.dopey
> if grumpy is None:
> grumpy = self.grumpy
> if happy is None:
> happy = self.happy
> if sleepy is None:
> sleepy = self.sleepy
> if sneezy is None:
> sneezy = self.sneezy
> # now do the real work...
> 
> def eggs(self, bashful=None, # etc... 
>):
> if bashful is None:
> bashful = self.bashful
> # and so on
>  
> 
> There's a lot of tedious boilerplate repetition in this, and to add 
> insult to injury the class is still under active development with an 
> unstable API, so every time I change one of the parameters, or add a new 
> one, I have to change it in over a dozen places.
> 
> Is there a good fix for this to reduce the amount of boilerplate?
> 
> 
> Thanks,
> 
> 
> 
> -- 
> Steve

What about something along these lines:

from inspect import getargspec, getargvalues


class Demo(object):

def __init__(self, a=None, b=10, c='oops'):
spec = getargspec(self.__init__)
for key, value in zip(spec.args[1:], spec.defaults):
setattr(self, key, value)

def foo(self, *args, **kwargs):
assert len(args) == 0

for k, v in kwargs.items():
try:
getattr(self, k)
except AttributeError:
print('*** error: attribute {} does not exist.'.format(k))

setattr(self, k, v)


d = Demo()
print(d.a)
d.foo(a=3.14)
print(d.a)

d.foo(q='this will raise')

So, use the inspect module to detect the valid arguments
from the class initializer. Then use **kwargs in every 
class method. It would be nice if the full method signature
can be kept, but I have not yet figured out if this is
possible. 

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


Re: Suggestion for Linux Distro (from PSA: Linux vulnerability)

2022-04-18 Thread Marco Sulla
On Sat, 16 Apr 2022 at 17:14, Peter J. Holzer  wrote:
>
> On 2022-04-16 16:49:17 +0200, Marco Sulla wrote:
> > Furthermore, you didn't answer my simple question: why does the
> > security update package contain metadata about Debian patches, if the
> > Ubuntu security team did not benefit from Debian security patches but
> > only from internal work?
>
> It DOES NOT contain metadata about Debian patches. You are
> misinterpreting the name "debian". The directory has this name because
> the tools (dpkg, quilt, etc.) were originally written by the Debian team
> for the Debian distribution. Ubuntu uses the same tools. They didn't
> bother to rename the directory (why should they?), so the directory is
> still called "debian" on Ubuntu (and yes I know this because I've built
> numerous .deb packages on Ubuntu systems).

Ah ok, now I understand. Sorry for the confusion.
-- 
https://mail.python.org/mailman/listinfo/python-list


tail

2022-04-23 Thread Marco Sulla
What about introducing a method for text streams that reads the lines
from the bottom? Java has also a ReversedLinesFileReader with Apache
Commons IO.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Receive a signal when waking or suspending?

2022-04-23 Thread Marco Sulla
I don't know in Python, but maybe you can create a script that writes
on a named pipe and read it from Python?
https://askubuntu.com/questions/226278/run-script-on-wakeup
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: tail

2022-04-23 Thread Marco Sulla
On Sat, 23 Apr 2022 at 20:59, Chris Angelico  wrote:
>
> On Sun, 24 Apr 2022 at 04:37, Marco Sulla  
> wrote:
> >
> > What about introducing a method for text streams that reads the lines
> > from the bottom? Java has also a ReversedLinesFileReader with Apache
> > Commons IO.
>
> It's fundamentally difficult to get precise. In general, there are
> three steps to reading the last N lines of a file:
>
> 1) Find out the size of the file (currently, if it's being grown)
> 2) Seek to the end of the file, minus some threshold that you hope
> will contain a number of lines
> 3) Read from there to the end of the file, split it into lines, and
> keep the last N
>
> Reading the preceding N lines is basically a matter of repeating the
> same exercise, but instead of "end of the file", use the byte position
> of the line you last read.
>
> The problem is, seeking around in a file is done by bytes, not
> characters. So if you know for sure that you can resynchronize
> (possible with UTF-8, not possible with some other encodings), then
> you can do this, but it's probably best to build it yourself (opening
> the file in binary mode).

Well, indeed I have an implementation that does more or less what you
described for utf8 only. The only difference is that I just started
from the end of file -1. I'm just wondering if this will be useful in
the stdlib. I think it's not too difficult to generalise for every
encoding.

> This is quite inefficient in general.

Why inefficient? I think that readlines() will be much slower, not
only more time consuming.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: tail

2022-04-23 Thread Marco Sulla
On Sat, 23 Apr 2022 at 23:00, Chris Angelico  wrote:
> > > This is quite inefficient in general.
> >
> > Why inefficient? I think that readlines() will be much slower, not
> > only more time consuming.
>
> It depends on which is more costly: reading the whole file (cost
> depends on size of file) or reading chunks and splitting into lines
> (cost depends on how well you guess at chunk size). If the lines are
> all *precisely* the same number of bytes each, you can pick a chunk
> size and step backwards with near-perfect efficiency (it's still
> likely to be less efficient than reading a file forwards, on most file
> systems, but it'll be close); but if you have to guess, adjust, and
> keep going, then you lose efficiency there.

Emh, why chunks? My function simply reads byte per byte and compares it to
b"\n". When it find it, it stops and do a readline():

def tail(filepath):
"""
@author Marco Sulla
@date May 31, 2016
"""

try:
filepath.is_file
fp = str(filepath)
except AttributeError:
fp = filepath

with open(fp, "rb") as f:
size = os.stat(fp).st_size
start_pos = 0 if size - 1 < 0 else size - 1

if start_pos != 0:
f.seek(start_pos)
char = f.read(1)

if char == b"\n":
start_pos -= 1
f.seek(start_pos)

if start_pos == 0:
f.seek(start_pos)
else:
for pos in range(start_pos, -1, -1):
f.seek(pos)

char = f.read(1)

if char == b"\n":
break

return f.readline()

This is only for one line and in utf8, but it can be generalised.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: tail

2022-04-24 Thread Marco Sulla
On Sat, 23 Apr 2022 at 23:18, Chris Angelico  wrote:

> Ah. Well, then, THAT is why it's inefficient: you're seeking back one
> single byte at a time, then reading forwards. That is NOT going to
> play nicely with file systems or buffers.
>
> Compare reading line by line over the file with readlines() and you'll
> see how abysmal this is.
>
> If you really only need one line (which isn't what your original post
> suggested), I would recommend starting with a chunk that is likely to
> include a full line, and expanding the chunk until you have that
> newline. Much more efficient than one byte at a time.
>

Well, I would like to have a sort of tail, so to generalise to more than 1
line. But I think that once you have a good algorithm for one line, you can
repeat it N times.

I understand that you can read a chunk instead of a single byte, so when
the newline is found you can return all the cached chunks concatenated. But
will this make the search of the start of the line faster? I suppose you
have always to read byte by byte (or more, if you're using urf16 etc) and
see if there's a newline.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: tail

2022-04-24 Thread Marco Sulla
On Sun, 24 Apr 2022 at 00:19, Cameron Simpson  wrote:

> An approach I think you both may have missed: mmap the file and use
> mmap.rfind(b'\n') to locate line delimiters.
> https://docs.python.org/3/library/mmap.html#mmap.mmap.rfind
>

Ah, I played very little with mmap, I didn't know about this. So I suppose
you can locate the newline and at that point read the line without using
chunks?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: tail

2022-04-24 Thread Marco Sulla
On Sun, 24 Apr 2022 at 11:21, Roel Schroeven  wrote:

> dn schreef op 24/04/2022 om 0:04:
> > Disagreeing with @Chris in the sense that I use tail very frequently,
> > and usually in the context of server logs - but I'm talking about the
> > Linux implementation, not Python code!
> If I understand Marco correctly, what he want is to read the lines from
> bottom to top, i.e. tac instead of tail, despite his subject.
> I use tail very frequently too, but tac is something I almost never use.
>

Well, the inverse reader is only a secondary suggestion. I suppose a tail
is much more useful.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: tail

2022-05-01 Thread Marco Sulla
Something like this is OK?

import os

def tail(f):
chunk_size = 100
size = os.stat(f.fileno()).st_size

positions = iter(range(size, -1, -chunk_size))
next(positions)

chunk_line_pos = -1
pos = 0

for pos in positions:
f.seek(pos)
chars = f.read(chunk_size)
chunk_line_pos = chars.rfind(b"\n")

if chunk_line_pos != -1:
break

if chunk_line_pos == -1:
nbytes = pos
pos = 0
f.seek(pos)
chars = f.read(nbytes)
chunk_line_pos = chars.rfind(b"\n")

if chunk_line_pos == -1:
line_pos = pos
else:
line_pos = pos + chunk_line_pos + 1

f.seek(line_pos)

return f.readline()

This is simply for one line and for utf8.
-- 
https://mail.python.org/mailman/listinfo/python-list


  1   2   3   4   5   6   7   8   >