async fuction

2013-01-11 Thread aleksey
Hello.

Can someone help me to resolv error.

code:


import threading

class TimeoutError(RuntimeError):
pass

class AsyncCall(object):
def __init__(self, fnc, callback = None):
self.Callable = fnc
self.Callback = callback

def __call__(self, *args, **kwargs):
self.Thread = threading.Thread(target = self.run, name = 
self.Callable.__name__, args = args, kwargs = kwargs)
self.Thread.start()
return self

def wait(self, timeout = None):
self.Thread.join(timeout)
if self.Thread.isAlive():
raise TimeoutError()
else:
return self.Result

def run(self, *args, **kwargs):
self.Result = self.Callable(*args, **kwargs)
if self.Callback:
self.Callback(self.Result)

class AsyncMethod(object):
def __init__(self, fnc, callback=None):
self.Callable = fnc
self.Callback = callback

def __call__(self, *args, **kwargs):
return AsyncCall(self.Callable, self.Callback)(*args, **kwargs)

def Async(fnc = None, callback = None):
if fnc == None:
def AddAsyncCallback(fnc):
return AsyncMethod(fnc, callback)
return AddAsyncCallback
else:
return AsyncMethod(fnc, callback)








@Async
def fnc(pi, pp):

print "fnc-"
i=pi
while ( i < 1000 ) :
i=i+1
print "fnc+"
pass

@Async
def fnc1(pp):
print "fnc1-",pp


@Async
def fnc2():
print "fnc2-"
i=0
while ( i < 10 ) :
i=i+1
print "fnc2+"
pass

fnc(i=0,pp="123123")
fnc1()


error:

Exception in thread fnc1:
Traceback (most recent call last):
  File "C:\Python27\lib\threading.py", line 551, in __bootstrap_inner
self.run()
  File "C:\Python27\lib\threading.py", line 504, in run
self.__target(*self.__args, **self.__kwargs)
  File "C:/Users/rootiks/YandexDisk/py/myftpbackup/asynclib.py", line 26, in run
self.Result = self.Callable(*args, **kwargs)
TypeError: fnc1() takes exactly 1 argument (0 given)

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


Problems with the date of modification of files on the flash drive in windows

2010-01-22 Thread Aleksey
Hi All,

I write crossplatform program and have next problem under windows
(under linux is all OK) :

I'm trying to get the UTC modification date of files on the flash
drive under windows. In the flash card system is FAT. In the winter
time (2010 01 21) Date of modification:


>>> op.getmtime('g:\\alex\\bag\\pybag.log')
1263998652.0


In the summer time (2010 07 21) Date of the modification on the hour
(3600 sec) different from winter time:


   >>> op.getmtime('g:\\alex\\bag\\pybag.log')
   1263995052.0


Also time zone is "GMT" but real is "OMST" and localtime  is wrong. In
windows  "tm_isdst" is always "0".

For files on the HDD with NTFS modification time is right.

How I can get right GMT time for files in flash with FAT under
windows?


Winter for Windows
--

>>> time.altzone
-3600
>>> time.daylight
0
>>> time.localtime()
time.struct_time(tm_year=2010, tm_mon=1, tm_mday=21, tm_hour=3,
tm_min=32, tm_sec=42, tm_wday=3,
tm_yday=21, tm_isdst=0)
>>> time.gmtime()
time.struct_time(tm_year=2010, tm_mon=1, tm_mday=21, tm_hour=3,
tm_min=32, tm_sec=48, tm_wday=3,
tm_yday=21, tm_isdst=0)
>>> time.tzname
('GMT', '')
>>> time.timezone
0

Summer for Windows
-

>>> time.altzone
-3600
>>> time.daylight
0
>>> time.localtime()
time.struct_time(tm_year=2010, tm_mon=7, tm_mday=21, tm_hour=2,
tm_min=13, tm_sec=18, tm_wday=2,
tm_yday=202, tm_isdst=0)
>>> time.gmtime()
time.struct_time(tm_year=2010, tm_mon=7, tm_mday=21, tm_hour=2,
tm_min=13, tm_sec=26, tm_wday=2,
tm_yday=202, tm_isdst=0)
>>> time.tzname
   ('GMT', '')
   >>> time.timezone
   0


>From Linux for summer:
-

   >>> time.altzone
   -25200
   >>> time.daylight
   1
   >>> time.localtime()
   time.struct_time(tm_year=2010, tm_mon=7, tm_mday=23, tm_hour=12,
tm_min=7, tm_sec=24, tm_wday=4,
   tm_yday=204, tm_isdst=1)
   >>> time.gmtime()
   time.struct_time(tm_year=2010, tm_mon=7, tm_mday=23, tm_hour=5,
tm_min=7, tm_sec=36, tm_wday=4,
   tm_yday=204, tm_isdst=0)
   >>> time.timezone
   -21600
   >>> time.tzname
   ('OMST', 'OMSST')


>From Linux for winter:


   >>> time.altzone
   -25200
   >>> time.daylight
   1
   >>> time.localtime()
   time.struct_time(tm_year=2010, tm_mon=1, tm_mday=23, tm_hour=11,
tm_min=8, tm_sec=26, tm_wday=5,
   tm_yday=23, tm_isdst=0)
   >>> time.gmtime()
   time.struct_time(tm_year=2010, tm_mon=1, tm_mday=23, tm_hour=5,
tm_min=8, tm_sec=39, tm_wday=5,
   tm_yday=23, tm_isdst=0)
   >>> time.timezone
   -21600
   >>> time.tzname
('OMST', 'OMSST')
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Copying a file with a question mark in it's name in Windows

2010-08-12 Thread Aleksey
On 12 авг, 18:49, drodrig  wrote:
> A python script I use to backup files on a Windows 2003 server
> occasionally fails to retrieve the size of a file with a question mark
> in the name. The exception I get is "OSError #123 The filename,
> directory name, or volume label syntax is incorrect". I realize that
> technically a question mark in the name of a file on Windows is
> illegal, but nevertheless these files exist on the file system. It
> seems that they are created by Office 2007 Word, for the most part.


If "?" is a placeholder for an unprintable character you can try view
real file name in IDLE:

  import glob
  print glob.glob(u'e:/full/path/to/file?')

In path to file you must instead question use wild "?".
Will be printed all like files.

-
Under Windows I too have similar problem: windows sometimes (from any
programs - e.g. Firefox) save files with wrong names, but later
do not manipulate with it.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Optimising literals away

2010-08-31 Thread Aleksey
On Aug 30, 10:38 pm, Tobias Weber  wrote:
> Hi,
> whenever I type an "object literal" I'm unsure what optimisation will do
> to it.
>
> def m(arg):
>   if arg & set([1,2,3]):
>     return 4
>
> Is the set created every time the method is called? What about a
> frozenset? Or tuple vs list? After how many calls per second does it pay
> to save it at the module level? Would anybody else find this ugly?
>
> Also I never profiled the regular expression cache...
>
> --
>   Tobias Weber

I test time creation of any types ang get next result:

dictionary = 393 000 * 10
frozenset = 267 000 * 10
list = 519 000 * 10
set = 268 000 * 10
tuple = 5 935 500 * 10
global assign = 5 882 700 * 10

All results multiple by 10 becouse i do 10 creations in one loop and
count loops per second.

As you see create global variable is more faster (20 times) then
create list and from it create set! Assigments is ~ 5 882 000*10,>>>
set creation is 268 000*10

My test system is Ubuntu 10.04, Dell Inspiron 1525, Core2Duo, T8300,
2Gb , Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) [GCC 4.4.3].
I make tests with XPyLIB.timetest module. (XPyLIB hosted at
sourceforge - http://sourceforge.net/apps/trac/xpylib/wiki/CookBook/TimeTest)

Assign global (pre declared by "global") function is next (N - is a
times of repeating):

gset = set((1,2,3))
def t_set_assign_global(ntimes = N, funcloop=u'funcloop',
excludecall=u'excludecall'):
"""Set assigment from global : global=(1,2,3); loop a = global 10
times in while.

@UID@ e710b888-bacd-4248-9ff7-1f7a348e1c8f
@author@ Mazhugin Aleksey
@score_common@ 1
"""
a = 0
global gset
while ntimes > 0:
a = gset
a = gset
a = gset
a = gset
a = gset
a = gset
a = gset
a = gset
a = gset
a = gset
ntimes -= 1



Set function is next:

def t_set_create(ntimes = N, funcloop=u'funcloop',
excludecall=u'excludecall'):
"""Set creation : t=(1,2,3); loop a = set(t) 10 times in while.

@UID@ a021a756-f9a5-44ec-b9e6-e5532b56c09f
@author@ Mazhugin Aleksey
@score_common@ 1
"""
a = 0
t = (1,2,3)
while ntimes > 0:
a = set(t)
a = set(t)
a = set(t)
a = set(t)
a = set(t)
a = set(t)
a = set(t)
a = set(t)
a = set(t)
a = set(t)
ntimes -= 1



Also i test regular expression compiled pattern vs non-compiled:

compiled = 343 000*2
not compiled = 164 000*2

Functions is next:

patt5 = u'*.tmp,*.pyc,*.pyo,*.bak,*.log'
path1 = u'/home/user/project/src/file.ext'
path2 = u'/home/user/project/logs/debug.log'

def t_rematch(ntimes=10, funcloop=u'funcloop',
excludecall='excludecall'):
"""
Compiled.

@UID@ 665f4014-9c11-4668-baae-e49230027bd4
@author@ Mazhugin Aleksey
@score_common@ 1
"""
ci = patt5.replace(u'\\',u'').replace(u'|',u'\
\|').replace(u'.',u'\\.').replace(u'*',u'.*'). \
replace(u'?',u'.?').replace(u'$',u'\\$').replace(u'^',u'\
\^').replace(u'{',u'\\{'). \
replace(u'(',u'\\(').replace(u'[',u'\\[').replace(u'+',u'\\
+').split(u',')
repat = u'|'.join([u'('+i+u'$)' for i in ci])
rec = re.compile(repat)
r = 0
while ntimes:
r = rec.match(path1) is not None
r = rec.match(path2) is not None
ntimes -= 1

def t_rematch_string(ntimes=10, funcloop=u'funcloop',
excludecall='excludecall'):
"""
Not compiled.

@UID@ 80fa1ca3-5d51-4f6e-8ac2-4ccafe4c1160
@author@ Mazhugin Aleksey
@score_common@ 1
"""
ci = patt5.replace(u'\\',u'').replace(u'|',u'\
\|').replace(u'.',u'\\.').replace(u'*',u'.*'). \
replace(u'?',u'.?').replace(u'$',u'\\$').replace(u'^',u'\
\^').replace(u'{',u'\\{'). \
replace(u'(',u'\\(').replace(u'[',u'\\[').replace(u'+',u'\\
+').split(u',')
repat = u'|'.join([u'('+i+u'$)' for i in ci])
#rec = re.compile(repat)
r = 0
while ntimes:
r = re.match(repat, path1) is not None
r = re.match(repat, path2) is not None
ntimes -= 1
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: parsing string into dict

2010-09-02 Thread Aleksey
On Sep 2, 12:46 am, Tim Arnold  wrote:
> Hi,
> I have a set of strings that are *basically* comma separated, but with
> the exception that if a comma occur insides curly braces it is not a
> delimiter.  Here's an example:
>
> [code=one, caption={My Analysis for \textbf{t}, Version 1}, continued]
>
> I'd like to parse that into a dictionary (note that 'continued' gets
> the value 'true'):
> {'code':'one', 'caption':'{My Analysis for \textbf{t}, Version
> 1}','continued':'true'}
>
> I know and love pyparsing, but for this particular code I need to rely
> only on the standard library (I'm running 2.7). Here's what I've got,
> and it works. I wonder if there's a simpler way?
> thanks,
> --Tim Arnold
>
> The 'line' is like my example above but it comes in without the ending
> bracket, so I append one on the 6th line.
>


You can use regular expression (also you not need adding ending
bracket):

import re
patt = re.compile(ur'\[code=(?P\w+),\scaption=(?P\{.+\})
(?P,\scontinued)?\]?')
def parse_options(s):
g=patt.match(s).groupdict()
return {'caption' : g['CAPTION'], 'code' : g['CODE'], 'continued' :
g['CONTINUED'] and True or False}


Test is next:


>>> s=u'[code=one, caption={My Analysis for \textbf{t}, Version 1}, continued]'
>>> s1=u'[code=one, caption={My Analysis for \textbf{t}, Version 1}]'
>>> s2=u'[code=one, caption={My Analysis for \textbf{t}, Version 1}, continued'
>>> s3=u'[code=one, caption={My Analysis for \textbf{t}, Version 1}'

>>> parse_options(s)
{'caption': u'{My Analysis for \textbf{t}, Version 1}', 'code':
u'one', 'continued': True}
>>> parse_options(s1)
{'caption': u'{My Analysis for \textbf{t}, Version 1}', 'code':
u'one', 'continued': False}
>>> parse_options(s2)
{'caption': u'{My Analysis for \textbf{t}, Version 1}', 'code':
u'one', 'continued': True}
>>> parse_options(s3)
{'caption': u'{My Analysis for \textbf{t}, Version 1}', 'code':
u'one', 'continued': False}
>>>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: parsing string into dict

2010-09-02 Thread Aleksey
On Sep 2, 12:46 am, Tim Arnold  wrote:
> Hi,
> I have a set of strings that are *basically* comma separated, but with
> the exception that if a comma occur insides curly braces it is not a
> delimiter.  Here's an example:
>
> [code=one, caption={My Analysis for \textbf{t}, Version 1}, continued]
>
> I'd like to parse that into a dictionary (note that 'continued' gets
> the value 'true'):
> {'code':'one', 'caption':'{My Analysis for \textbf{t}, Version
> 1}','continued':'true'}
>
> I know and love pyparsing, but for this particular code I need to rely
> only on the standard library (I'm running 2.7). Here's what I've got,
> and it works. I wonder if there's a simpler way?
> thanks,
> --Tim Arnold
>
> The 'line' is like my example above but it comes in without the ending
> bracket, so I append one on the 6th line.
>


You can use regular expression (also you not need adding ending
bracket):

import re
patt = re.compile(ur'\[code=(?P\w+),\scaption=(?P\{.+\})
(?P,\scontinued)?\]?')
def parse_options(s):
g=patt.match(s).groupdict()
return {'caption' : g['CAPTION'], 'code' : g['CODE'], 'continued' :
g['CONTINUED'] and True or False}


Test is next:


>>> s=u'[code=one, caption={My Analysis for \textbf{t}, Version 1}, continued]'
>>> s1=u'[code=one, caption={My Analysis for \textbf{t}, Version 1}]'
>>> s2=u'[code=one, caption={My Analysis for \textbf{t}, Version 1}, continued'
>>> s3=u'[code=one, caption={My Analysis for \textbf{t}, Version 1}'

>>> parse_options(s)
{'caption': u'{My Analysis for \textbf{t}, Version 1}', 'code':
u'one', 'continued': True}
>>> parse_options(s1)
{'caption': u'{My Analysis for \textbf{t}, Version 1}', 'code':
u'one', 'continued': False}
>>> parse_options(s2)
{'caption': u'{My Analysis for \textbf{t}, Version 1}', 'code':
u'one', 'continued': True}
>>> parse_options(s3)
{'caption': u'{My Analysis for \textbf{t}, Version 1}', 'code':
u'one', 'continued': False}
>>>
-- 
http://mail.python.org/mailman/listinfo/python-list


ANNOUNCE: PYBAG v0.4.0

2010-10-23 Thread Aleksey
This is first PYBAG announce on comp.lang.python.

PYBAG implements a portable bag and is intended for fast
synchronization and backup. It lets you use a portable digital storage
device to carry your electronic documents similar to the way you can
use a bag to carry paper documents. You can synchronize the bag with
your original files easily. If a synchronization conflict occurs, it
will be reported. You can specify rules for automatic conflict
resolution. With PYBAG, you can backup files and synchronize any
changes made to the original files with the bag. The synchronization
process will only copy changed files. The program is cross-platform
and independent from the OS and filesystem. You may easily synchronize
files between Windows and Linux, for example. Symbolic links are
supported on all systems (if the OS or filesystem does not support
symlinks, then they are emulated). This program has a GUI and a
command line interface.

Home page: http://pybag.sourceforge.net
-- 
http://mail.python.org/mailman/listinfo/python-list