We're looking for a middle python developer in Moscow
Hello everybody! We're looking for a well-experienced python developer who'd like to participate in educational startup in Moscow, Russia. It's going to be a contractor's job position for 6 months with possible prolongation. Here is the link: http://edumate.ru Main milestones for work completion: - The development of billing - The development of an external API for communication with partners - The design and development of additional tools for our clients Requirements: - Python, of course (you should know what does threads, GIL, yield mean, you've read PEP 8 etc.) - you've worked with Django 1.7 at least 1 year - you have an experience with NoSQL storages in production (MongoDB, Redis) - you've worked with Sphinx/Haystack - you have an experience of team work development with SVN or Git (if you active on GitHub - please, share your github account in your CV) If you're interested, please, send your CV to us: i...@edumate.ru Thank you! -- https://mail.python.org/mailman/listinfo/python-list
Re: Error while installing Python2.5.1
On Thu, 21 Jun 2007 10:03:43 +0200, <[EMAIL PROTECTED]> wrote: > checking for --without-gcc... no > checking for gcc... no > checking for cc... no > checking for cc... no > checking for cl... no > configure: error: no acceptable C compiler found in $PATH > See `config.log' for more details. It appears you have no C compiler installed. You should be able to get one of those from your distribution's repositories. If you can't, upgrade to something debian-based. /pavel -- http://mail.python.org/mailman/listinfo/python-list
Yet another Python textbook
I would like to introduce a new Python textbook aimed at high school students: http://femhub.com/textbook-python/. The textbook is open source and its public Git repository is located at Github: g...@github.com:femhub/nclab-textbook-python.git Feedback and contributions are very much welcome, every contributor becomes automatically a co-author. Best regards, Pavel -- Pavel Solin Associate Professor Applied and Computational Mathematics University of Nevada, Reno http://hpfem.org/~pavel -- http://mail.python.org/mailman/listinfo/python-list
Re: Yet another Python textbook
Hi Ian, thank you for your comments. On Mon, Nov 19, 2012 at 11:46 PM, Ian Kelly wrote: > On Sun, Nov 18, 2012 at 10:30 PM, Pavel Solin > wrote: > > I would like to introduce a new Python textbook > > aimed at high school students: > > > > http://femhub.com/textbook-python/. > > > > The textbook is open source and its public Git > > repository is located at Github: > > > > g...@github.com:femhub/nclab-textbook-python.git > > > > Feedback and contributions are very much > > welcome, every contributor becomes automatically > > a co-author. > > First impression: I'm opening up the book and reading the > introduction, and I get to section 1.6, and the very first code > example given is: > > >>> print "Hello, World!" > :) > > A fine tradition to be sure, but I have to say that I'm a little > disappointed that a new textbook on Python being written in 2012 is > focused squarely on Python 2, especially when I just read on the > previous page that Python 3 was released in 2008. Is there any work > underway get Python 3 into NCLab? > There is an ongoing discussion but we are not sure. Are there any reasons except for the print () command and division of integers? > > The issue comes up again four pages later in section 2.4, when > division is being demoed, and the text takes a page-and-a-half detour > to caution about the use of floor division for expressions like: > > >>> 33 / 6 > > If the book were teaching Python 3, then this warning would be > unnecessary, since division in Python 3 is *automatically* true > division, unless you go out of your way to invoke floor division by > using the special // operator. I think that the earliness and > frequency that these differences arise underscore the point that it > would be best if the book could simply be teaching Python 3 to start > with. > Perhaps you are right. Is there any statistics of how many Python programmers are using 2.7 vs. 3? Most of people I know use 2.7. > > Getting off that soapbox and moving along, I notice that on pages > 20-22 there are some examples by way of comparison that are written in > C, which makes me wonder what audience this textbook is really > intended for. The previous pages and references to Karel have given > me the impression that this is geared toward beginning programmers, > who most likely are not familiar with C. That's exactly right. Unfortunately, many high school teachers are using C++ to teach programming to complete beginners. This comment is for them. It can be removed, you can do it if you like. The code is on Github. > The most troublesome is the > last of these examples, which is led up to with this text: > > The asterisks in the code below are pointers, an additional > programming concept that one needs to learn and utilize here: > > This seems to suggest that the reader should stop reading here and do > a Google search on pointers, in order to understand the example. > Since this is not a textbook on C, and Python has no concept of > pointers at all, doing this would be a complete waste of the reader's > time. > > Skimming through a few more chapters, I don't see anything else that > sets my spidey sense tingling. I hope that what I've written above > gives you some things to consider, though. > Thank you once more for the comments. Pavel > > Cheers, > Ian > -- Pavel Solin Associate Professor Applied and Computational Mathematics University of Nevada, Reno http://hpfem.org/~pavel -- http://mail.python.org/mailman/listinfo/python-list
Re: Yet another Python textbook
Hi Alec, > Can you put your website—http://femhub.com/textbook-python/—on your > github—https://github.com/femhub/nclab-textbook-python? Done, thank you so much. I edited the textbook based on responses that I received. Based on several inquiries we also decided to add Python 3.2 to NCLab. New release is coming in one or two weeks. Cheers, Pavel On Wed, Nov 21, 2012 at 4:34 PM, Alec Taylor wrote: > Dear Prof. Solin, > > Can you put your website—http://femhub.com/textbook-python/—on your > github—https://github.com/femhub/nclab-textbook-python? > > I will then send you a pull request with a bootstrapped homepage with good > UX. > > All the best, > > Alec Taylor > -- Pavel Solin Associate Professor Applied and Computational Mathematics University of Nevada, Reno http://hpfem.org/~pavel -- http://mail.python.org/mailman/listinfo/python-list
Web browser Python programming in NCLab
Hello, the NCLab development team would like to invite everybody to try out Python programming in the web browser at www.nclab.com. Using NCLab is free for personal, non-commercial purposes. If you'd like to give us feedback how we are doing, please use the mailing list nclab-u...@googlegroups.com. We hope to hear from you! Best, Pavel -- Pavel Solin University of Nevada, Reno http://hpfem. <http://hpfem.math.unr.edu/people/pavel/>org/~pavel -- http://mail.python.org/mailman/listinfo/python-list
Re: Zipping a dictionary whose values are lists
zip(*d.values()) On 12 April 2012 20:28, wrote: > I using Python 3.2 and have a dictionary d = {0:[1,2], 1:[1,2,3], 2:[1,2,3,4]} > > whose values are lists I would like to zip into a list of tuples. If I > explicitly write: list(zip([1,2], [1,2,3], [1,2,3,4]) > [(1, 1, 1), (2, 2, 2)] > > I get exactly what I want. On the other hand, I have tried > list(zip(d)) > [(0,), (1,), (2,)] > list(zip(d.values())) > [([1, 2],), ([1, 2, 3],), ([1, 2, 3, 4],)] > list(zip(d[i] for i in d)) > [([1, 2],), ([1, 2, 3],), ([1, 2, 3, 4],)] > list(zip(*d)) > Traceback (most recent call last): > File "", line 1, in > list(zip(*d)) > TypeError: zip argument #1 must support iteration > > and nothing quite works. What am I doing wrong? > > Sincerely > > Thomas Philips > -- > http://mail.python.org/mailman/listinfo/python-list -- С уважением, Аносов Павел -- http://mail.python.org/mailman/listinfo/python-list
Re: how many days in one year ?
import calendar print 366 if calendar.isleap(2003) else 365 On 22 April 2012 13:37, contro opinion wrote: > i want to know how many days in one year, > import time > import datetime > d1= datetime.datetime(2003, 1, 1) > d2=datetime.datetime(2003, 21, 31) > print (d2-d1).days+1 > > i can get there are 365 days in the 2003, > > is there other way,better way to calculate ? > > -- > http://mail.python.org/mailman/listinfo/python-list > -- С уважением, Аносов Павел -- http://mail.python.org/mailman/listinfo/python-list
Re: C Python extension to export an Function
If you're familiar with C++, I recommend to have a look at Boost::Python. Sample program: #include #include void world() { std::cout << "hello world" << std::endl; } BOOST_PYTHON_MODULE( hello ) { using namespace ::boost::python; def( "world", &world ); } Usage: python -c "import hello; hello.world()" -- https://mail.python.org/mailman/listinfo/python-list
Python grammar extension via encoding (pyxl style) questions
Hi everybody! We’re building an experimental extension to Python, we’re extending Python’s comprehensions into a full-scale query language. And we’d love to use the trick that was done in pyxl, where a special encoding of the file will trigger the preprocessor to run and compile our extended language into Python and execute it. The actual encoding business is a bit tricky, but we also really really would like the whole thing to be installable via pip, otherwise only a tiny percentage of our potential users (we're targeting data scientists) would go through the pain of installing the extension. So I have some question, would really appreciate any help: - Are there any resources to get up the speed on custom encodings? The pyxl code looks fine, just not clear what some of the steps are. - We’d love to have a pip install for our module, but even pyxl didn't do it (i.e. you clone the repository, then do a local pip install, and then run a script). Is it impossible to just do it all with pip? Would it be a terrible/horrible hack? Any hints on how to do this? - Last question - we’d love to enable support for our extension in interactive python and especially in Jupyter notebooks. Would you have any advise on how to approach these? Thanks in advance! Pavel Velikhov P.S. Here's the demo site for our project,if you're interested: www.pythonql.org -- https://mail.python.org/mailman/listinfo/python-list
Announcement: PythonQL - An integrated query language for Python
Hi Folks, We have released PythonQL, a query language extension to Python (we have extended Python’s comprehensions with a full-fledged query language, drawing from the useful features of SQL, XQuery and JSONiq). Take a look at the project here: http://www.pythonql.org and lets us know what you think! The way PythonQL currently works is you mark PythonQL files with a special encoding and the system runs a preprocessor for all such files (this is similar to how pyxl project has been done). We have an interactive interpreter and Jupyter support planned. Best regards! PythonQL team -- https://mail.python.org/mailman/listinfo/python-list
Re: Announcement: PythonQL - An integrated query language for Python
On Tuesday, 1 November 2016 12:50:37 UTC+3, Peter Otten wrote: > Pavel Velikhov wrote: > > > We have released PythonQL, a query language extension to Python (we have > > extended Python’s comprehensions with a full-fledged query language, > > drawing from the useful features of SQL, XQuery and JSONiq). Take a look > > at the project here: http://www.pythonql.org and lets us know what you > > think! > > I would really like Python to get seamless integration of SQL, so I applaud > your effort! > > The demo on your web page is too noisy for my taste, so I went ahead and > installed the python3 version in a virtual env. > Great! Yes, we're hoping this will be useful to folks that like SQL and other query languages. > My first attempt failed because of a missing ply; maybe you can fix that. > Now off to the tutorial... Oops, I have tested with virtual env and ply was installing just fine, wierd. Any hints on why it didn't pick it up during the installation? > > The way PythonQL currently works is you mark PythonQL files with a > > special encoding and the system runs a preprocessor for all such files > > (this is similar to how pyxl project has been done). We have an > > interactive interpreter and Jupyter support planned. > > > > Best regards! > > PythonQL team -- https://mail.python.org/mailman/listinfo/python-list
Re: Announcement: PythonQL - An integrated query language for Python
On Tuesday, 1 November 2016 20:16:43 UTC+3, Michael Torrie wrote: > On 11/01/2016 02:56 AM, Pavel Velikhov wrote: > > Hi Folks, > > > > We have released PythonQL, a query language extension to Python (we > > have extended Python’s comprehensions with a full-fledged query > > language, drawing from the useful features of SQL, XQuery and > > JSONiq). Take a look at the project here: http://www.pythonql.org and > > lets us know what you think! > > Sounds interesting. Hope you'll accept a wee bit of criticism of the > web page, though. Why not put the examples clearly on the front page, > along with output? Took me a while to find the examples, and it wasn't > obvious what to do with the examples matrix. I'd prefer to just see > them straight away without too much digging. > > I'm glad you had a link to your github repo right on the main page. I > found your Github markdown files to be far more informative than your > web page. Hi Michael! Thanks for the feedback, will try to make the examples easier to find, definitely! Not too happy with the site layout myself... Was it obvious that you can play around with the examples - i.e. edit them and run modified versions? Will keep on improving the documentation too! -- https://mail.python.org/mailman/listinfo/python-list
Re: Announcement: PythonQL - An integrated query language for Python
On Tuesday, 1 November 2016 20:09:14 UTC+3, Peter Otten wrote: > Pavel Velikhov wrote: > > > On Tuesday, 1 November 2016 12:50:37 UTC+3, Peter Otten wrote: > >> Pavel Velikhov wrote: > >> > >> > We have released PythonQL, a query language extension to Python (we > >> > have extended Python’s comprehensions with a full-fledged query > >> > language, > >> > drawing from the useful features of SQL, XQuery and JSONiq). Take a > >> > look at the project here: http://www.pythonql.org and lets us know what > >> > you think! > >> > >> I would really like Python to get seamless integration of SQL, so I > >> applaud your effort! > >> > >> The demo on your web page is too noisy for my taste, so I went ahead and > >> installed the python3 version in a virtual env. > >> > > > > Great! Yes, we're hoping this will be useful to folks that like SQL and > > other query languages. > > > >> My first attempt failed because of a missing ply; maybe you can fix that. > >> Now off to the tutorial... > > > > Oops, I have tested with virtual env and ply was installing just fine, > > wierd. Any hints on why it didn't pick it up during the installation? > > I don't know enough about pip to make sense of it, but here's what I see: > > $ virtualenv -p python3 tmp_pyql > Running virtualenv with interpreter /usr/bin/python3 > Using base prefix '/usr' > New python executable in tmp_pyql/bin/python3 > Also creating executable in tmp_pyql/bin/python > Installing setuptools, pip...done. > $ cd tmp_pyql/ > $ . bin/activate > > > If at this point I first run > > (tmp_pyql)$ run pip install ply > > installing pythonql3 will succeed. Otherwise: > > > (tmp_pyql)$ pip install pythonql3 > Downloading/unpacking pythonql3 > Downloading pythonql3-0.9.43.tar.gz (41kB): 41kB downloaded > Running setup.py (path:/home/peter/tmp_pyql/build/pythonql3/setup.py) > egg_info for package pythonql3 > > Downloading/unpacking ply>=3.9 (from pythonql3) > Downloading ply-3.9.tar.gz (150kB): 150kB downloaded > Running setup.py (path:/home/peter/tmp_pyql/build/ply/setup.py) egg_info > for package ply > > warning: no previously-included files matching '*.pyc' found anywhere in > distribution > Installing collected packages: pythonql3, ply > Running setup.py install for pythonql3 > > Running post install task > Running setup.py install for ply > Failed to import the site module > Traceback (most recent call last): > File "/home/peter/tmp_pyql/lib/python3.4/site.py", line 703, in > > main() > File "/home/peter/tmp_pyql/lib/python3.4/site.py", line 683, in main > paths_in_sys = addsitepackages(paths_in_sys) > File "/home/peter/tmp_pyql/lib/python3.4/site.py", line 282, in > addsitepackages > addsitedir(sitedir, known_paths) > File "/home/peter/tmp_pyql/lib/python3.4/site.py", line 204, in > addsitedir > addpackage(sitedir, name, known_paths) > File "/home/peter/tmp_pyql/lib/python3.4/site.py", line 173, in > addpackage > exec(line) > File "", line 1, in > File "/home/peter/tmp_pyql/lib/python3.4/site- > packages/pythonql/codec/register.py", line 5, in > from pythonql.parser.Preprocessor import makeProgramFromString > File "/home/peter/tmp_pyql/lib/python3.4/site- > packages/pythonql/parser/Preprocessor.py", line 3, in > from pythonql.parser.PythonQLParser import Parser, Node, > print_program > File "/home/peter/tmp_pyql/lib/python3.4/site- > packages/pythonql/parser/PythonQLParser.py", line 1, in > import ply.yacc as yacc > ImportError: No module named 'ply' > Complete output from command /home/peter/tmp_pyql/bin/python3 -c "import > setuptools, > tokenize;__file__='/home/peter/tmp_pyql/build/ply/setup.py';exec(compile(getattr(tokenize, > > 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" > install --record /tmp/pip-3yg_3xh1-record/install-record.txt --single- > version-externally-managed --compile --install-headers > /home/peter/tmp_pyql/include/site/python3.4: > Failed to import the site module > > Traceback (most recent call last): > > File "/home/peter/tmp_pyql/lib/python3.4/site.py", line 703, in > > main() > > File "/home/peter/tmp_pyq
Re: errorhandler 2.0.0 Released!
Hi, can you explain, why is the attribute 'fired' class-level and not instance-level? There must be good reason for modifying class attribute from instance. Dne pondělí 6. června 2016 15:26:08 UTC+2 Chris Withers napsal(a): > Hi All, > > errorhandler is a tiny but useful logging handler for the python logging > framework. It lets you tell when logging above a certain level has > occurred, even if it's not in code you can control. > > I'm pleased to announce the release of errorhandler 2.0.0 featuring the > following: > > - Support for Python 3 > > - Documentation on Read The Docs > > - Continuous testing using Travis CI > > - Code coverage reporting through Coveralls > > The package is on PyPI and a full list of all the links to docs, issue > trackers and the like can be found here: > > https://github.com/Simplistix/errorhandler > > Any questions, please do ask on the Testing in Python list or on the > Simplistix open source mailing list... > > cheers, > > Chris > > -- > Simplistix - Content Management, Batch Processing & Python Consulting > - http://www.simplistix.co.uk -- https://mail.python.org/mailman/listinfo/python-list
Re: mod_python compilation error in VS 2008 for py2.7.1
Have you considered to use rather WSGI-based solution? (for Apache Httpd is mod_wsgi). Mod_python is totally obsolete. -- https://mail.python.org/mailman/listinfo/python-list
__all__ attribute: bug and proposal
Hi, I today uncovered subtle bug and would like to share it with you. By a mistake, I forgot to put comma into '__all__' tuple of some module. Notice missing comma after 'B'. # module foo.py __all__ = ( 'A', 'B' 'C', ) class A: pass class B: pass class C: pass If you try to import * from the module, it will raise an error, because 'B' and 'C' will be concatenated into 'BC'. >>> from foo import * AttributeError: 'module' object has no attribute 'BC' The bug won't be found until someone imports *. In order to identify problems as soon as possible, here's the proposal. Porposal: allow putting objects into __all__ directly, so possible problems will be found earlier: # module foo.py class A: pass class B: pass class C: pass __all__ = (A, B, C) Note: this currently don't work. >>> from foo import * TypeError: attribute name must be string, not 'type' -- https://mail.python.org/mailman/listinfo/python-list
Re: __all__ attribute: bug and proposal
> but what about integers or strings? Can you provide example? --- No matter if __all__ uses names or objects, I think it should be validated not only when importing '*', but always. Frankly, do you always unit-test if __all__ works? -- https://mail.python.org/mailman/listinfo/python-list
Re: python for everyday tasks
On Saturday 23 November 2013 02:01:26 Steven D'Aprano wrote: > * Python is not Java, and Java is not Python either: > > http://dirtsimple.org/2004/12/python-is-not-java.html > http://dirtsimple.org/2004/12/java-is-not-python-either.html Thanks for all those references. There's this statement in the first article: "Got a switch statement? The Python translation is a hash table, not a bunch of if-then statments. Got a bunch of if-then's that wouldn't be a switch statement in Java because strings are involved? It's still a hash table. " I can't figure out how would you translate a switch statement into hash table in general case. -- https://mail.python.org/mailman/listinfo/python-list
Caching function results
Suppose, I have some resource-intensive tasks implemented as functions in Python. Those are called repeatedly in my program. It's guranteed that a call with the same arguments always produces the same return value. I want to cache the arguments and return values and in case of repititive call immediately return the result without doing expensive calculations. I intend to do it this way: # module 'cache.py' class Cache: def save_result(self, handle, args): """Save calculated result to cache.""" <...> def load_result(self, handle, args, result): """Load previously calculated result from cache. Return None is it's unavailable.""" <...> def save_to_file(self, filename): """Save all cached data to a file.""" def __init__(self, filename=None): # Optionally loads previously cached data from a file def clear(self): <...> # module 'calculations.py' import cache _cache = cache.Cache() def big_task(arg1, arg2, arg3=None): cached_value = _cache.load_result('big_task', (arg1, arg2, arg3)) if cached_value is not None: return cached_value result = <...> _cache.save_result('big_task', (arg1, arg2, arg3), result) return result The arguments and return values are almost always: * ints * floats * tuple or lists of ints or floats I think Cache object will store data in a dictionary. I'll convert lists to tuples before storing them. I'd also like to limit the size of the cache (in MB) and get rid of old cached data. Don't know how yet. Do you like this design or maybe there's a better way with Python's included batteries? -- https://mail.python.org/mailman/listinfo/python-list
Using map()
I checked my modules with pylint and saw the following warning: W: 25,29: Used builtin function 'map' (bad-builtin) Why is the use of map() discouraged? It' such a useful thing. -- https://mail.python.org/mailman/listinfo/python-list
object().__dict__
There are some basics about Python objects I don't understand. Consider this snippet: class X: pass ... x = X() dir(x) ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__'] x.foo = 11 And now I want to make a simple object in a shorter way, without declaring X class: y = object() dir(y) ['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__'] y.foo = 12 Traceback (most recent call last): File "", line 1, in AttributeError: 'object' object has no attribute 'foo' The attribute list is different now and there's no __dict__ and the object does not accept new attributes. Please explain what's going on. -- https://mail.python.org/mailman/listinfo/python-list
GOTCHA with list comprehension
Hi, I recently found interesting GOTCHA while doing list comprehension in python 2.6: >>> values = ( True, False, 1, 2, 3, None ) >>> [ value for value in values if value if not None ] [True, 1, 2, 3] I was wondering why this list comprehension returns incorrect results and finally found a typo in the condition. The typo wasn't visible at the first look. My intention was: if value is not None But I wrote: if value if not None Is that a language feature of list comprehension that it accepts conditions like: if A if B if C if D ...? -- https://mail.python.org/mailman/listinfo/python-list
Re: GOTCHA with list comprehension
It seems this is allowed by the grammar: list_display::= "[" [expression_list | list_comprehension] "]" list_comprehension ::= expression list_for list_for::= "for" target_list "in" old_expression_list [list_iter] old_expression_list ::= old_expression [("," old_expression)+ [","]] old_expression ::= or_test | old_lambda_expr list_iter ::= list_for | list_if list_if ::= "if" old_expression [list_iter] So chaining multiple ifs is fine: [ i for i in range(10) if True if True if True if True ] Dne středa 5. srpna 2015 8:49:20 UTC+2 Pavel S napsal(a): > Hi, > > I recently found interesting GOTCHA while doing list comprehension in python > 2.6: > > >>> values = ( True, False, 1, 2, 3, None ) > >>> [ value for value in values if value if not None ] > [True, 1, 2, 3] > > I was wondering why this list comprehension returns incorrect results and > finally found a typo in the condition. The typo wasn't visible at the first > look. > > My intention was: if value is not None > But I wrote: if value if not None > > Is that a language feature of list comprehension that it accepts conditions > like: if A if B if C if D ...? -- https://mail.python.org/mailman/listinfo/python-list
Re: GOTCHA with list comprehension
$ cat /etc/redhat-release Red Hat Enterprise Linux Server release 6.5 (Santiago) $ python --version Python 2.6.6 > Incidentally, why Python 2.6? > > ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: GOTCHA with list comprehension
Hi Chris, yeah, I have to stick on the software which my employer provides to me (we're enterprise company). I'm not root on that system. I'm happy with 2.6 now, two years ago we were on older RHEL with python 2.4 and it was a real pain :) > > $ cat /etc/redhat-release > > Red Hat Enterprise Linux Server release 6.5 (Santiago) > > $ python --version > > Python 2.6.6 > > > >> Incidentally, why Python 2.6? > >> > > I guess that would be why :) > > That's probably actually a patched 2.6.6 - from what I understand of > how Red Hat works, the version number is the number of the *oldest* > part of the code, so that quite possibly has a lot of backported > fixes. When I said 2.6 was out of support, I meant from python.org; > Red Hat supports stuff for a lot longer. > > So, yeah, perfectly good reason for sticking with 2.6. For now. :) > > ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Successfully send sms with python
On Tuesday, September 22, 2015 at 1:20:07 PM UTC+2, Timon Rhynix wrote: > Hello, I have used pyserial, sms0.4 and other libraries to send sms via > huawei E1750 modem. > The code runs well and no error is thrown but the text message is not > sent/delivered to the number. > One of my code is as follows: > > import serial > import time > > class TextMessage: > def __init__(self, recipient="0123456789", message="TextMessage.content > not set."): > self.recipient = recipient > self.content = message > > def setRecipient(self, number): > self.recipient = number > > def setContent(self, message): > self.content = message > > def connectPhone(self): > conn = 'COM13' > self.ser = serial.Serial(conn, 460800, timeout=5) > time.sleep(1) > > def sendMessage(self): > self.ser.write('ATZ\r') > time.sleep(1) > self.ser.write('AT+CMGF=1\r') > time.sleep(1) > self.ser.write('''AT+CMGS="''' + self.recipient + '''"\r''') > time.sleep(1) > self.ser.write(self.content + "\r") > time.sleep(1) > self.ser.write(chr(26)) > time.sleep(1) > print "message sent!" > > def disconnectPhone(self): > self.ser.close() > > When run it, the "message sent!" is printed but no message is sent/delivered. > Please assist on what I am missing. Thank you Hi, why don't you use http://wammu.eu/python-gammu/ ? -- https://mail.python.org/mailman/listinfo/python-list
Re: Successfully send sms with python
I don't understand why all of you are telling him about '\r\n\, write(),..' instead of recommending to use take library which already has all problems resolved (python-gammu / wammu). When one will write custom templating stuff, you would also recommend him to take jinja. -- https://mail.python.org/mailman/listinfo/python-list
Re: anyone tell me why my program will not run?
On суббота, 21 ноября 2015 г. 6:30:02 MSK, Dylan Riley wrote: i am learning python and was tasked with making a program that flips a coin 100 times and then tells you the number of heads and tails. First, you have a syntax error: if result = heads: should be: if result == heads: Second, there's incorrent indentation, your while loop is almost empty (not syntax error though). -- https://mail.python.org/mailman/listinfo/python-list
Re: anyone tell me why my program will not run?
On суббота, 21 ноября 2015 г. 6:30:02 MSK, Dylan Riley wrote: Also some more notes: heads = int("1") tails = int("2") Why use this strange initialization? The usual way: heads = 1 tails = 2 gives the same result. while flips != 0: flips -= 1 There's no need to use while and flips variable: for _ in range(100): if random.randint(heads, tails) == heads: headscount += 1 else: tailscount += 1 Also, it's good to put import at the beginning. -- https://mail.python.org/mailman/listinfo/python-list
Py3K: file inheritance
In the Python 2.x was simple to create own file object: class MyFile(file): pass for example to reimplement write() or something else. How to do it in Python 3.x? -- http://mail.python.org/mailman/listinfo/python-list
Re: Py3K: file inheritance
On 21 окт, 00:42, Ian Kelly wrote: > On Thu, Oct 20, 2011 at 11:28 AM, Yosifov Pavel wrote: > > In the Python 2.x was simple to create own file object: > > > class MyFile(file): > > špass > > > for example to reimplement write() or something else. How to do it in > > Python 3.x? > > See the docs for the io module. Depending on what you want to do, you > probably need to subclass either io.FileIO or io.TextIOWrapper. Little silly example: class MyFile(file): def __init__(self, *a, **ka): super(MyFile, self).__init__(*a, **ka) self.commented = 0 def write(self, s): if s.startswith("#"): self.commented += 1 super(MyFile, self).write(s) When I tried in Python 3.x to inherit FileIO or TextIOWrapper and then to use MyFile (ex., open(name, mode, encoding), write(s)...) I get errors like 'unsupported write' or AttributeError 'readable'... Can you show me similar simple example like above but in Python 3.x? -- http://mail.python.org/mailman/listinfo/python-list
Re: Py3K: file inheritance
On 21 окт, 13:50, Ian Kelly wrote: > On Thu, Oct 20, 2011 at 10:17 PM, Yosifov Pavel wrote: > > Little silly example: > > > class MyFile(file): > > šdef __init__(self, *a, **ka): > > š šsuper(MyFile, self).__init__(*a, **ka) > > š šself.commented = 0 > > šdef write(self, s): > > š šif s.startswith("#"): > > š š šself.commented += 1 > > š š šsuper(MyFile, self).write(s) > > > When I tried in Python 3.x to inherit FileIO or TextIOWrapper and then > > to use MyFile (ex., open(name, mode, encoding), write(s)...) I get > > errors like 'unsupported write' or AttributeError 'readable'... Can > > you show me similar simple example like above but in Python 3.x? > > class MyTextIO(io.TextIOWrapper): > def __init__(self, *args, **kw): > super().__init__(*args, **kw) > self.commented = 0 > def write(self, s): > if s.startswith('#'): > self.commented += 1 > super().write(s) > > buffered = open(name, 'wb') > textio = MyTextIO(buffered, encoding='utf-8') > textio.write('line 1') > textio.write('# line 2') > textio.close() > print(textio.commented) > > HTH, > Ian Thank you very much! -- http://mail.python.org/mailman/listinfo/python-list
tkinter, livewires: slow moving on Canvas
Hi, I have two similar programs- one in Livewires and one in Tkinter. Just moving the player on a canvas. Why is that one in Tkinter so slow? What did I miss? Livewires is based on Tkinter, so it should be the same. I looked at the code of Livewires, tried something, but no success. Maybe its because of sleeping part in event handler in Tkinter, that is somehow (how?) bypassed in Livewires by magic tkinter.dooneevent(tkinter.DONT_WAIT). Neither this worked with me. Could anyone help? Both programs attached. livewires: http://www.livewires.org.uk/python/lwpackage.html -- geon Pavel Kosina from livewires import * from random import randint def makePlayer(): global player_body, player_x, player_y player_x=randint(0,640) player_y=randint(0,480) player_body=circle(player_x, player_y,5, filled=1) def movePlayer(): global finito, player_body, player_x, player_y keys=keys_pressed() if '2' in keys: player_y=player_y-krok if '4' in keys: player_x=player_x-krok if '6' in keys: player_x=player_x+krok if '8' in keys: player_y=player_y+krok if 'q' in keys: finito=1 move_to(player_body, player_x, player_y) begin_graphics() allow_moveables() makePlayer() finito=0 krok=0.1 while not finito: movePlayer() end_graphics() from Tkinter import * import random def makeCanvas(): global canvas, root root=Tk() canvas=Canvas(root, width=640, height=480, bg="white") canvas.pack() def makePlayer(): global player x1,y1=random.randint(0,640), random.randint(0,480) x2,y2=x1+10, y1+10 player=canvas.create_oval(x1,y1,x2,y2, fill="blue") root.bind("", movePlayer) def movePlayer(event): if event.char=="8": # nahoru dx,dy= 0,-step elif event.char=="6": # vpravo dx,dy= step, 0 elif event.char=="2": # dolu dx,dy= 0, step elif event.char=="4": # vlevo dx,dy= -step,0 else: return canvas.move(player, dx, dy) # --- root program step=1 makeCanvas() makePlayer() mainloop() -- http://mail.python.org/mailman/listinfo/python-list
Re: Help with autotools
Jonh Wendell wrote: > Hi all! I need help with autotools stuff.. > > My directory structure: > > project_name > src > main.py > others.py > data > ui.glade > images.png > po > translations.po > > I'd like to do something like that: > > The files in "src" should be installed at: $prefix/lib/project_name > The files in "data" should be installed at: $prefix/share/project_name > > At $prefix/bin should be create a slink: > $prefix/bin/project_name -> $prefix/lib/project_name/main.py > > The listen (http://listengnome.free.fr/) does this, but it doesn't use > autotools... > > Can anyone help me? > I've tried a lot to make this work, but with no success... > > Thanks, > -- > Jonh Wendell Santana > Analista de Sistemas > > http://www.bani.com.br > MSN: [EMAIL PROTECTED] > GTalk/Jabber: [EMAIL PROTECTED] > Linux User #114432 Hi, in listen project Makefile is shown how to make this thing. URL for SVN view of Makefile for listen 0.4.3 is: http://svn.sourceforge.net/viewvc/listengnome/releases/0.4.x/0.4.3/Makefile?view=markup Cheers, Pavel -- http://mail.python.org/mailman/listinfo/python-list
Re: How to comment code?
I think that doc strings are the most important way in which you should be commenting on your code. Once the code works, you can elimainate most inline comments, leaving only doc string for everything and a few comments on some particularly confusing parts. Other than that, comments usually only clutter Python code. But remember that you are the person who understands your code best, so don't be too fanatical about deleting inline comments. -- http://mail.python.org/mailman/listinfo/python-list
Re: how to write special value to a config file with ConfigParser
Hi, I have tried this: import ConfigParser fp = file("test.conf","w+") cp = ConfigParser.ConfigParser() cp.add_section("Section1") cp.set("Section1","Value1",12345) cp.write(fp) and works fine for me. -- http://mail.python.org/mailman/listinfo/python-list
typedef (type alias) and ctypes
I try to create type aliases, like typedef in C (a specially aliases to ctypes objects). This case: >>> some_type = c_ulong >>> oth_type = c_ulong works in all cases but not with type qualification: >>> t1 = c_ulong # reference to c_ulong, nothing else :( >>> t2 = c_ulong >>> x = t1() >>> y = t2() >>> type(x)==type(y) True This trivial way for typedef doesn't allow to determine real type and it's absolutely right :) >>> t1 = type('t1',(c_ulong,),{}) >>> t2 = type('t2',(c_ulong,),{}) >>> x = t1() >>> y = t2() >>> type(x)==type(y) False The problem: 1st way work in complex using of ctypes (interfacing with some DLLs), but doesn't allow to determine real type! 2st way allows to determine real type, but "access violation reading 0x000C'" occurs in some DLL calls! Question: what "warts", errors are in 2nd way (may be reason of access violation)? -- Pavel -- http://mail.python.org/mailman/listinfo/python-list
tkinter 3.0 multiple keyboard events together
Is it possible to catch in an event more that one key from keyboard? In my code, I can handle always the only one, the first I press, the others are omitted. Say, I press both "4" and "8" and only "4" is catched. def movePlayer(event): print (event.keysym) Thank you. -- geon Pavel Kosina -- http://mail.python.org/mailman/listinfo/python-list
Re: tkinter 3.0 multiple keyboard events together
janislaw napsal(a): On 26 Gru, 05:52, Pavel Kosina wrote: Is it possible to catch in an event more that one key from keyboard? In my code, I can handle always the only one, the first I press, the others are omitted. Say, I press both "4" and "8" and only "4" is catched. def movePlayer(event): print (event.keysym) Each keypress triggers another event. Fortunately there are two types of events: reaction to press and release. The logic to write to recognize those as simultaneous clicks is up to you :) Might you give me a little bit more? Just a link to a site where this is explained and showed would be OK. I really did my best but everything is bad. geon -- http://mail.python.org/mailman/listinfo/python-list
Re: tkinter 3.0 multiple keyboard events together
janislaw napsal(a): Use google to find the appropriate site, or browse this site, there are plenty of examples. You may want to examine the code I wrote to you to catch the idea: #-- import Tkinter import pprint tk = Tkinter.Tk() f = Tkinter.Frame(tk, width=100, height=100) msg = Tkinter.StringVar() msg.set('Hello') l = Tkinter.Label(f, textvariable=msg) l.pack() f.pack() keys = set() def keyPressHandler(event): keys.add(event.char) display() def keyReleaseHandler(event): keys.remove(event.char) display() def display(): msg.set(str(keys)) f.bind_all('', keyPressHandler) f.bind_all('', keyReleaseHandler) f.mainloop() Is this really the most simple solution how to do this in Tkinter? Is there nothing cleaner "build inside"? This solution has disadvantage that after you release one key, that the function keyPressHandler stopped to be called by the other pressed keys. I would have not to build moving the player in KeyPresshandler, but on another new function that would have to read periodically "keys" and to act afterwards I really tried to search for words mentioned in this subject, but nothing, not even this solution, was found by me. PK -- http://mail.python.org/mailman/listinfo/python-list
Re: tkinter 3.0 multiple keyboard events together
janislaw napsal(a): Um, I could be only guessing what are you meant to do, unless you describe your problem in more detailed way. I.e. describe the desired behaviour, show code which you have, and describe the current behaviour. well, I am working on a tutorial for youngster (thats why i need to stay the code as easy as possible). In this game you are hunted by robots. I could use key"7" on numeric keypad for left-up moving but seems to me, that "4"+"8" is much more standard for them. This solution has disadvantage that after you release one key, that the function keyPressHandler stopped to be called by the other pressed keys. I would have not to build moving the player in KeyPresshandler, but on another new function that would have to read periodically "keys" and to act afterwards Hmmm. Maybe you'd like to hook into Tkinter event loop, i.e. by custom events if you don't like periodical polling. No, that would be even more difficult. I already have a code that use your idea: from Tkinter import * root = Tk() pressedKeys=set() def onKeyPress(event): pressedKeys.add(event.keysym) def onKeyRelease(event): pressedKeys.remove(event.keysym) def move(): print list(pressedKeys) root.after(100,move) root.bind("", onKeyPress) root.bind("", onKeyRelease) root.after(100,move) root.mainloop() well, I thought that gui´s have such a problem already built-in - so that i am not pressed to code it. You know, its not exactly about me - if I do it for myself, for my program, that there is no problem, but I need to explained it to begginners . And I do not want, as might be more usual, do my module, that would cover this "insanity" (look at it with 13-years old boy eyes ;-) ). Do you like to say me, that this is not a standard "function" neither in tkinter, not say gtk or the others, too? I would expect something like this: def onKeyTouch(event): print (event.keysymAll) root.bind("", onKeyTouch) and all the pressed keys are printed all the functions OnKeyPress, OnKeyRelease, move, even set pressedKeys are in onKeyTouch P. -- http://mail.python.org/mailman/listinfo/python-list
idle 3.0 unicode
As for unicode in Python 2.5 everything works fine in program running either in IDLE or under Command line: # -*- coding: utf-8 -*- print u"ěščřžýáíé" In 3.0 there is an error. The same program, moved to 3.0 syntax, in IDLE editor : # -*- coding: utf-8 -*- print ("ěščřžýáíé") prints: ěščřžýáĂĂ© The same program running in Command line from PSPad editor works fine. Is it mistake of my misunderstanding or of IDLE? - The same program without coding declaration (but saved in utf8) : print ("ěščřžýáíé") even immediately destroyed my IDLE window without any error message. -- geon Pavel Kosina -- http://mail.python.org/mailman/listinfo/python-list
Re: idle 3.0 unicode
小楼 napsal(a), dne 1.1.2009 10:32: #coding="utf-8" #中国 print('a') saved in utf8 alt+x,destroyed IDLE... me too,why? Are you sure? Run (F5) with print('a') is OK here. Maybe you have redefined key bindings in IDLE ... -- geon Pavel Kosina -- http://mail.python.org/mailman/listinfo/python-list
patch
Týká se to Pythonu okrajově, přesto to dávám sem. Myslím, že by se mohlo hodit i ostatním, kdo chtějí zkoušet pod Windows nové věci z development verze: Jak opatchovat (pythonýrský) soubor pod Windows? Co jsem udělal: 1/ stáhl jsem http://gnuwin32.sourceforge.net/packages/patch.htm 2/ nainstaloval pod amin, přidal cestu do ..bin .. do PATH 3/ stáhl http://bugs.python.org/file12561/conv.diff a uložil do Lib/idlelib 4/ cmd: patch < conf.diff hází chybu: C:\prg\Python30\Lib\idlelib>patch < conv.diff patching file IOBinding.py Assertion failed: hunk, file ../patch-2.5.9-src/patch.c, line 354 This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information. Patchuje někdo tady pod Windows? Díky -- geon Pavel Kosina -- http://mail.python.org/mailman/listinfo/python-list
patch
sorry, pls forget ... -- geon Pavel Kosina -- http://mail.python.org/mailman/listinfo/python-list
Overriding methods per-object
I've got an object which has a method, __nonzero__ The problem is, that method is attached to that object not that class > a = GeneralTypeOfObject() > a.__nonzero__ = lambda: False > a.__nonzero__() False But: > bool(a) True What to do? -- http://mail.python.org/mailman/listinfo/python-list
Re: Overriding methods per-object
> The docs don't say you can do that: Thanks, hadn't noticed that. > Should you be able to? I'd say so. In my case, I need a class that can encapsulate any object, add a few methods to it, and spit something back that works just like the object, but also has those extra methods. I can't just add the methods, because it has to work on e.g. lists. So I'll have to end up defining all the possible methods on that class (and that's still not best because I can't use hasattr to test if, for example, addition is allowed on that object). On the other hand, I see how this severely restricts the possibly optimizations that can be made in the interpreter. -- http://mail.python.org/mailman/listinfo/python-list
Re: Overriding methods per-object
On Apr 18, 4:01 pm, Piet van Oostrum wrote: > But you can give each object its own class and then put the special > methods in that class: > > >>> def create_special_object(bases, *args): > > ... if not isinstance(bases, tuple): > ... bases = bases, > ... cls = type("SpecialClass", bases, {}) > ... return cls(*args) > ...>>> a = create_special_object(list, [1,2,3]) > >>> a > [1, 2, 3] > >>> a.__class__ > > > > >>> a.__class__.__nonzero__ = lambda self: False > >>> bool(a) > False > >>> a.__nonzero__() > False > >>> a.append(4) > >>> a > [1, 2, 3, 4] > > -- > Piet van Oostrum > URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4] > Private email: p...@vanoostrum.org I think this is the solution I like best. > FYI this works as you expect if GeneralTypeOfObject is an old-style > class (i.e. does not inherit from object). If this feature is so more > important than all those that come with new-style classes, you have > control over the involved classes and don't care about Python 3.x > (where old-style classes are gone), you may choose to downgrade > GeneralTypeOfObject to old-style. It is important, but new-style classes are important too. And I care quite a bit about Python 3.x, thus the fact that I prefer the above solution. -- http://mail.python.org/mailman/listinfo/python-list
Re: Overriding methods per-object
On Apr 18, 9:43 pm, Aaron Brady wrote: > On Apr 17, 9:41 pm, Steven D'Aprano > cybersource.com.au> wrote: > > On Fri, 17 Apr 2009 18:22:49 -0700, Pavel Panchekha wrote: > > > I've got an object which has a method, __nonzero__ The problem is, that > > > method is attached to that object not that class > > > >> a = GeneralTypeOfObject() > > >> a.__nonzero__ = lambda: False > > >> a.__nonzero__() > > > False > > > > But: > > > >> bool(a) > > > True > > > > What to do? > > > (1) Don't do that. > > > (2) If you *really* have to do that, you can tell the class to look at > > the instance: > > > class GeneralTypeOfObject(object): > > def __nonzero__(self): > > try: > > return self.__dict__['__nonzero__'] > > except KeyError: > > return something > > snip > > I think you need to call the return, unlike you would in > '__getattr__'. > return self.__dict__['__nonzero__']( ) > > You're free to use a different name for the method too. > return self.custom_bool( ) > > And you don't even have to implement an ABC. Er... /have/ to, that > is. I got it working. Thanks! -- http://mail.python.org/mailman/listinfo/python-list
Unhandled exceptions checking
Does somebody know existent tool for checking unhandled exceptions? Like in Java when method throws exception but in code using this method, try...catch is missed. May be something like PyChecker? -- /Pavel -- http://mail.python.org/mailman/listinfo/python-list
Re: Unhandled exceptions checking
On 24 май, 12:58, bukzor <[EMAIL PROTECTED]> wrote: > On May 23, 6:31 pm, Yosifov Pavel <[EMAIL PROTECTED]> wrote: > > > Does somebody know existent tool for checking unhandled exceptions? > > Like in Java when method throws exception but in code using this > > method, try...catch is missed. May be something like PyChecker? > > > -- > > /Pavel > > I know that pychecker doesn't do that. The set of handled exceptions > is probably much smaller than handled. Almost every line of code can > throw ten different exceptions (at least my code). I'm not sure what > specifically the output you'd like to see would look like. OK. I understand point of view of all responders. It seems I surmised why it's very difficult to do in tool like PyChecker: not only functions/methods but usual Python expression can mask potential exception source. And this tool must know how to parse every expression deep (to object protocol methods, for example). But it's possible in principle... And you are right: the output of this tool will be very verbal :-) Thanks to all -- http://mail.python.org/mailman/listinfo/python-list
ThreadPoolingMixIn
Hi, everybody! I wrote a useful class ThreadPoolingMixIn which can be used to create fast thread-based servers. This mix-in works much faster than ThreadingMixIn because it doesn't create a new thread on each request. Is it worth including in SocketServer.py? from __future__ import with_statement from SocketServer import ThreadingMixIn import threading import Queue class ThreadPoolingMixIn(ThreadingMixIn): """Mix-in class to handle requests in a thread pool. The pool grows and thrinks depending on load. For instance, a threading UDP server class is created as follows: class ThreadPoolingUDPServer(ThreadPoolingMixIn, UDPServer): pass """ __author__ = 'Pavel Uvarov <[EMAIL PROTECTED]>' def init_thread_pool(self, min_workers = 5, max_workers = 100, min_spare_workers = 5): """Initialize thread pool.""" self.q = Queue.Queue() self.min_workers = min_workers self.max_workers = max_workers self.min_spare_workers = min_spare_workers self.num_workers = 0 self.num_busy_workers = 0 self.workers_mutex = threading.Lock() self.start_workers(self.min_workers) def start_workers(self, n): """Start n workers.""" for i in xrange(n): t = threading.Thread(target = self.worker) t.setDaemon(True) t.start() def worker(self): """A function of a working thread. It gets a request from queue (blocking if there are no requests) and processes it. After processing it checks how many spare workers are there now and if this value is greater than self.min_spare_workers then the worker exits. Otherwise it loops infinitely. """ with self.workers_mutex: self.num_workers += 1 while True: (request, client_address) = self.q.get() with self.workers_mutex: self.num_busy_workers += 1 self.process_request_thread(request, client_address) self.q.task_done() with self.workers_mutex: self.num_busy_workers -= 1 if self.num_workers - self.num_busy_workers > \ self.min_spare_workers: self.num_workers -= 1 return def process_request(self, request, client_address): """Puts a request into queue. If the queue size is too large, it adds extra worker. """ self.q.put((request, client_address)) with self.workers_mutex: if self.q.qsize() > 3 and self.num_workers < self.max_workers: self.start_workers(1) def join(self): """Wait for all busy threads""" self.q.join() -- http://mail.python.org/mailman/listinfo/python-list
Re: ThreadPoolingMixIn
On May 31, 9:13 pm, Rhamphoryncus <[EMAIL PROTECTED]> wrote: > On May 30, 2:40 pm, [EMAIL PROTECTED] wrote: > > > Hi, everybody! > > > I wrote a useful class ThreadPoolingMixIn which can be used to create > > fast thread-based servers. This mix-in works much faster than > > ThreadingMixIn because it doesn't create a new thread on each request. > > Do you have any benchmarks demonstrating the performance difference/ > To benchmark this I used a simple tcp server which writes a small (16k) string to the client and closes the connection. I started 100 remote clients and got 500 replies/s for ThreadingMixIn and more than 1500 replies/s for ThreadPoolingMixIn. I tested it on FreeBSD 6.2 amd64. I'm very curious about the exactness of the number 500 for ThreadingMixIn. It seems to be the same for various packet sizes. I suspect there is some OS limit on thread creating rate. Below I include a bugfixed ThreadPoolingMixIn and the benchmarking utility. The utility can be used to start clients on localhost, though the reply rate will be slower (around 1000 replies/s). To start benchmarking server with localhost clients use: python ./TestServer.py --server=threading --n-clients=100 or python ./TestServer.py --server=threadpooling --n-clients=100 #--- ThreadPoolingMixIn.py from __future__ import with_statement from SocketServer import ThreadingMixIn import threading import Queue class ThreadPoolingMixIn(ThreadingMixIn): """Mix-in class to handle requests in a thread pool. The pool grows and thrinks depending on load. For instance, a threadpooling TCP server class is created as follows: class ThreadPoolingUDPServer(ThreadPoolingMixIn, TCPServer): pass """ __author__ = 'Pavel Uvarov <[EMAIL PROTECTED]>' def init_thread_pool(self, min_workers = 5, max_workers = 100, min_spare_workers = 5): """Initialize thread pool.""" self.q = Queue.Queue() self.min_workers = min_workers self.max_workers = max_workers self.min_spare_workers = min_spare_workers self.num_workers = 0 self.num_busy_workers = 0 self.workers_mutex = threading.Lock() self.start_workers(self.min_workers) def start_workers(self, n): """Start n workers.""" for i in xrange(n): t = threading.Thread(target = self.worker) t.setDaemon(True) t.start() def worker(self): """A function of a working thread. It gets a request from queue (blocking if there are no requests) and processes it. After processing it checks how many spare workers are there now and if this value is greater than self.min_spare_workers then the worker exits. Otherwise it loops infinitely. """ with self.workers_mutex: self.num_workers += 1 while True: (request, client_address) = self.q.get() with self.workers_mutex: self.num_busy_workers += 1 self.process_request_thread(request, client_address) self.q.task_done() with self.workers_mutex: self.num_busy_workers -= 1 if (self.num_workers > self.min_workers and self.num_workers - self.num_busy_workers > self.min_spare_workers): self.num_workers -= 1 return def process_request(self, request, client_address): """Puts a request into queue. If the queue size is too large, it adds extra worker. """ self.q.put((request, client_address)) with self.workers_mutex: if self.q.qsize() > 3 and self.num_workers < self.max_workers: self.start_workers(1) def join(self): """Wait for all busy threads""" self.q.join() #--- TestServer.py from __future__ import with_statement from SocketServer import * import socket import sys import threading import time import os from ThreadPoolingMixIn import * class ThreadPoolingTCPServer(ThreadPoolingMixIn, TCPServer): pass class TestServer(ThreadingTCPServer): allow_reuse_address = True request_queue_size = 128 def __init__(self, server_address, RequestHandlerClass, packet_size): TCPServer.__init__(self, server_address, RequestHandlerClass) self.packet_size = packet_size self.sum_t = 0 self.total_num_requests = 0 self.num_requests = 0 self.t0 = time.time() self.lock = threading.Lock() def reset_stats(self): with self.lock: self.total_num_requests += self.num_requests
Re: ThreadPoolingMixIn
On Jun 2, 7:09 pm, [EMAIL PROTECTED] wrote: > On May 31, 9:13 pm, Rhamphoryncus <[EMAIL PROTECTED]> wrote: > > > On May 30, 2:40 pm, [EMAIL PROTECTED] wrote: > > > > Hi, everybody! > > > > I wrote a useful class ThreadPoolingMixIn which can be used to create > > > fast thread-based servers. This mix-in works much faster than > > > ThreadingMixIn because it doesn't create a new thread on each request. > > > Do you have any benchmarks demonstrating the performance difference/ > > To benchmark this I used a simple tcp server which writes a small > (16k) > string to the client and closes the connection. > > I started 100 remote clients and got 500 replies/s for ThreadingMixIn > and more than 1500 replies/s for ThreadPoolingMixIn. I tested it on > FreeBSD 6.2 amd64. > > I'm very curious about the exactness of the number 500 for > ThreadingMixIn. It seems to be the same for various packet sizes. > I suspect there is some OS limit on thread creating rate. > > Below I include a bugfixed ThreadPoolingMixIn and the benchmarking > utility. The utility can be used to start clients on localhost, though > the reply rate will be slower (around 1000 replies/s). > > To start benchmarking server with localhost clients use: > python ./TestServer.py --server=threading --n-clients=100 > or > python ./TestServer.py --server=threadpooling --n-clients=100 I've just tested it on a linux box and got a 240 replies/s vs 2000 replies/s, that is 8x performance improvement. -- http://mail.python.org/mailman/listinfo/python-list
Re: ThreadPoolingMixIn
On Jun 2, 7:15 pm, Michael Ströder <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: > > To benchmark this I used a simple tcp server which writes a small > > (16k) > > string to the client and closes the connection. > > Just a general note: When benchmarking such a network service it would > be valuable to see benchmark results for several data sizes. I'd expect > better numbers for a ThreadPoolingMixIn when there are more requests > with smaller data size. > > Ciao, Michael. Here are benchmarks for FreeBSD 6.2, amd64 packet_size x y 0499.57 1114.54 1024499.29 1130.02 3072500.09 1119.14 7168498.20 .76 15360499.29 1086.73 31744500.04 1036.46 64512499.43939.60 130048499.28737.44 261120498.04499.03 523264307.54312.04 1047552173.57185.32 2096128 93.61 94.39 x = ThreadingMixIn replies/s y = ThreadPoolingMixIn replies/s -- http://mail.python.org/mailman/listinfo/python-list
Re: ThreadPoolingMixIn
On Jun 3, 1:19 am, [EMAIL PROTECTED] wrote: > On Jun 2, 12:41 pm, [EMAIL PROTECTED] wrote: > > > > > On Jun 2, 7:15 pm, Michael Ströder <[EMAIL PROTECTED]> wrote: > > > Here are benchmarks for FreeBSD 6.2, amd64 > > > packet_size x y > > 0499.57 1114.54 > >1024499.29 1130.02 > >3072500.09 1119.14 > >7168498.20 .76 > > 15360499.29 1086.73 > > 31744500.04 1036.46 > > 64512499.43939.60 > > 130048499.28737.44 > > 261120498.04499.03 > > 523264307.54312.04 > > 1047552173.57185.32 > > 2096128 93.61 94.39 > > > x = ThreadingMixIn replies/s > > y = ThreadPoolingMixIn replies/s > > Well, I'd say you've got yourself a winner. Performance (at least on > FreeBSD) seems as good or better for your ThreadPoolingMixin than > ThreadingMixin. Is this with the default values of min=5 and max=5 > worker threads? No, I initialized thread pool with min_threads=2, max_threads=200 and min_spare_threads=20. For Linux (2.6.22, amd64) I got even more dramatic improvement: packet_sizex y 0 249.97 2014.63 1024 249.98 1782.83 3072 240.09 1859.00 7168 249.98 1900.61 15360 249.98 1787.30 31744 238.96 1808.17 64512 249.85 1561.47 130048 237.26 1213.26 261120 249.98841.96 523264 249.97595.40 1047552 236.40351.96 2096128 216.26218.15 x = ThreadingMixIn replies/s y = ThreadPoolingMixIn replies/s -- http://mail.python.org/mailman/listinfo/python-list
iterator clone
Whats is the way to clone "independent" iterator? I can't use tee(), because I don't know how many "independent" iterators I need. copy and deepcopy doesn't work... --pavel -- http://mail.python.org/mailman/listinfo/python-list
Re: iterator clone
On 13 июл, 14:12, Peter Otten <[EMAIL PROTECTED]> wrote: > Yosifov Pavel wrote: > > Whats is the way to clone "independent" iterator? I can't use tee(), > > because I don't know how many "independent" iterators I need. copy and > > deepcopy doesn't work... > > There is no general way. For "short" sequences you can store the items in a > list which is also the worst-case behaviour of tee(). > > What are you trying to do? > > Peter I try to generate iterators (iterator of iterators). Peter, you are right! Thank you. For example, it's possible to use something like this: def cloneiter( it ): """return (clonable,clone)""" return tee(it) and usage: clonable,seq1 = cloneiter(seq) ...iter over seq1... then clone again: clonable,seq2 = cloneiter(clonable) ...iter over seq2... Or in class: class ReIter: def __init__( self, it ): self._it = it def __iter__( self ): self._it,ret = tee(self._it) return ret and usage: ri = ReIter(seq) ...iter over ri... ...again iter over ri... ...and again... But I think (I'm sure!) it's deficiency of Python iterators! They are not very good... --Pavel -- http://mail.python.org/mailman/listinfo/python-list
Re: iterator clone
> Well, I think Python's iterators, especially the generators, are beautiful. > More importantly, I think there is no general way to make iterators > copyable, regardless of the programming language. The problem is that most > of the useful ones depend on external state. > > Peter Hmm, but tee() de facto do it (clone iterator) and ignore side-effects of iterator ("external" state). And tee() create independent **internal** state of iterator (current position). But **external** state - is headache of programmer. So, iterator/generator have to be method for copy itself (the tee() implementation) or be "re- startable". Why not? Concrete problem was to generate iterators (iterator of slices). It was solved with ReIter. --Best regards, --pavel -- http://mail.python.org/mailman/listinfo/python-list
Re: iterator clone
> `tee()` doesn't copy the iterator or its internal state but just caches > it's results, so you can iterate over them again. That makes only sense > if you expect to use the two iterators in a way they don't get much out of > sync. If your usage pattern is "consume iterator 1 fully, and then > re-iterate with iterator 2" `tee()` has no advantage over building a list > of all results of the original iterator and iterate over that twice. > `tee()` would be building this list anyway. It's interesting and a concrete answer. Thanks a lot. > Because it's often not possible without generating a list with all > results, and the advantage of a low memory footprint is lost. > > Ciao, > Marc 'BlackJack' Rintsch Seems like "monada". But I think is possible to determine when there is a bounded external state (side-effects) or not, may be is needed some new class-protocol for it... or something else. Or another way: iterators may be re-iterable always, but if programmer need to point to the extra- (external) state, he has to raise some a special exception in __iter)) method... OK, it's only fantasies about language design :-) --pavel -- http://mail.python.org/mailman/listinfo/python-list
Re: iterator clone
On 14 июл, 23:36, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > On 13 juil, 12:05, Yosifov Pavel <[EMAIL PROTECTED]> wrote: > (snip) > > > defcloneiter( it ): > > """return (clonable,clone)""" > > return tee(it) > > This might as well be written as > > cloneiter = tee > > Or yet better, just remove the above code and s/cloneiter/tee/g in the > remaining... Yes, sure. It was only for illustration. BUT: Marc Rintsch is right: cloning of iterators in this manner is bad, more good is to use one, single list(my_iter) instead of (see http://aquagnu.blogspot.com/2008/07/self-repair-iterator-in-python.html). Thanks to all! --pavel -- http://mail.python.org/mailman/listinfo/python-list
Re: iterator clone
Kay, can you show example of such generator? ReIter, for example, work with usual generators. But for "big" iterator, I think is no any good solutions. IMHO we can discern 2 types of iterators: re-startable (based on internal Python objects) and not re-startable (with an external state, side- effects)... Best regards, Pavel -- http://mail.python.org/mailman/listinfo/python-list
Re: iterator clone
On 16 июл, 11:32, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote: > On Tue, 15 Jul 2008 19:54:30 -0700, Yosifov Pavel wrote: > > Kay, can you show example of such generator? ReIter, for example, work > > with usual generators. > > > But for "big" iterator, I think is no any good solutions. IMHO we can > > discern 2 types of iterators: re-startable (based on internal Python > > objects) and not re-startable (with an external state, side- > > effects)... > > Has nothing to do with internal vs. external. > Examples: ``itertools.count(1)``, ``itertools.cycle(iterable)``, or > > def fib(): > a, b = 0, 1 > while True: > yield a > a, b = b, a + b > > Ciao, > Marc 'BlackJack' Rintsch Yes. So, I'm disconcerted: what Python "means" about iterators. Why iterator's are not clonable in default?.. ``itertools.count(it) `` "suppose" ``it`` will be restarted after count? So ``count(it)`` "suppose" ``it`` is restarable and therefore clonable (why not?!). Generator is only function, so can be restarted/cloned. Generator keeps "position" and can't be iterated again. But this position can be reseted (main rule: if generator function has not side-effects/ external state, see below)! Iterator, I think, is more general method (for naturally serial access). For example, you can read values from some device (RS323 or other) and represent it in program like iterator. In this case, ``__iter__()`` make some preparation, ``next()`` read next value from RS232. In this case, iterator can't be restarted and really cloned. It has external state (state of device) and can't to return it at start. But when series of values are generated (are born) in the program: no problem to have the feature of clone/restart iterator. And is very interesting to research Icon iterators. Is possible to create something in Python such theirs, for non-deterministic solving purpose... :-) --pavel -- http://mail.python.org/mailman/listinfo/python-list
Overriding module's class
Is it possible to override a class in the module or the module itself that is imported across the project to add new methods to it? For example I've got module 'a' with class A from a import A but I don't want to add a method to that A class not just in this unit, but across the project, so everywhere I'll import class A - it would be a modified one. Thank you. -- http://mail.python.org/mailman/listinfo/python-list
Windows and Linux Tips
Hi Friends, For Windows and Linux Tips, Please Visit: www.windowsandlinuxtips.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Inheriting dictionary
I want a dictionary that will transparently "inherit" from a parent dictionary. So, for example: """ a = InheritDict({1: "one", 2: "two", 4: "four"}) b = InheritDict({3: "three", 4: "foobar"}, inherit_from=a) a[1] # "one" a[4] # "four" b[1] # "one" b[3] # "three" b[4] # "foobar" """ I've written something like this in Python already, but I'm wondering if something like this already exists, preferably written in C, for speed. -- http://mail.python.org/mailman/listinfo/python-list
Re: Inheriting dictionary
On Aug 18, 4:23 pm, "Jan Kaliszewski" wrote: > 18-08-2009 o 21:44:55 Pavel Panchekha wrote: > > > > > I want a dictionary that will transparently "inherit" from a parent > > dictionary. So, for example: > > > """ > > a = InheritDict({1: "one", 2: "two", 4: "four"}) > > b = InheritDict({3: "three", 4: "foobar"}, inherit_from=a) > > > a[1] # "one" > > a[4] # "four" > > b[1] # "one" > > b[3] # "three" > > b[4] # "foobar" > > """ > > > I've written something like this in Python already, but I'm wondering > > if something like this already exists, preferably written in C, for > > speed. > > AFAIN -- no. But you can easily implement it in Python with rather > small loss of speed... > > class InheritDict(dict): > > class NoParent(object): > def __getitem__(self, key): > raise KeyError('There is no %r key in the hierarchy' % key) > def __nonzero__(self): > return False > > noparent = NoParent() > > def __init__(self, *args, **kwargs): > parent = kwargs.pop('inherit_from', self.noparent) > dict.__init__(self, *args, **kwargs) > self.parent = parent > > def __getitem__(self, key): > try: > return dict.__getitem__(self, key) > except KeyError: > return self.parent[key] > > Did you do it in similar way? (just curiosity) :-) > > Regards, > *j > > -- > Jan Kaliszewski (zuo) I implemented it in a similar way (instead of a try block, an if block, which works a tiny bit faster; also have a multiple-parents case, but its rare, and I could do that in Python without much loss of speed). Pity that there's no C version; this InheritDict is kind of the core of my application (in a very basic test, I have 329901 calls to __getitem__). Oh well; I'll see if I can optimize the __getattr__ function with minor tweaking. Thanks for your help. -- http://mail.python.org/mailman/listinfo/python-list
Re: Inheriting dictionary
On Aug 18, 5:11 pm, "Jan Kaliszewski" wrote: > 18-08-2009 o 22:27:41 Nat Williams wrote: > > > > > On Tue, Aug 18, 2009 at 2:44 PM, Pavel Panchekha > > wrote: > > >> I want a dictionary that will transparently "inherit" from a parent > >> dictionary. So, for example: > > >> """ > >> a = InheritDict({1: "one", 2: "two", 4: "four"}) > >> b = InheritDict({3: "three", 4: "foobar"}, inherit_from=a) > > >> a[1] # "one" > >> a[4] # "four" > >> b[1] # "one" > >> b[3] # "three" > >> b[4] # "foobar" > >> """ > > >> I've written something like this in Python already, but I'm wondering > >> if something like this already exists, preferably written in C, for > >> speed. > > > Why complicate this with a custom object? Just use regular dicts and > > make b a copy of a. > > > a = {1: 'one', 2: 'two', 4: 'four'} > > b = dict(a) > > b[3] = 'three' > > b[4] = 'foobar' > > Because, as I understand Pavel's intent, it has to work dynamically > (e.g. changes in 'a' reflect in behaviour of 'b'), and obviously not > only for such trivial examples like above. > > *j > > -- > Jan Kaliszewski (zuo) That is indeed the point. -- http://mail.python.org/mailman/listinfo/python-list
Distutils evil voodoo: install into a package
Before you flame me, I know that what I'm trying to do is beyond evil. But I nonetheless want to do it. Feel free to rant if you must. :) I have a package that I want to install into another package. For example, I have the packages pya and pyb. pya is guaranteed to be installed before pyb is, so that's not an issue. pya is installed as the module `pya`. I want to install pyb into the module `pya.pyb`. Is there any way to do this beyond figuring out what the path is and installing the entire pyb directory as data_files? -- http://mail.python.org/mailman/listinfo/python-list
Re: Distutils evil voodoo: install into a package
> This is what whe world has created namespace-packages for. At least if > you can live with the namespace "pya" being otherwise empty. That seems like a good solution. Thanks! -- http://mail.python.org/mailman/listinfo/python-list
urllib.request with proxy and HTTPS
Hello, I'm trying to make an HTTPS request with urllib. OS: Gentoo Python: 3.6.1 openssl: 1.0.2l This is my test code: = CODE BLOCK BEGIN = import ssl import urllib.request from lxml import etree PROXY = 'proxy.vpn.local:' URL = "https://google.com"; proxy = urllib.request.ProxyHandler({'http': PROXY}) #context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_1) context = ssl.SSLContext() context.verify_mode = ssl.CERT_REQUIRED context.check_hostname = True secure_handler = urllib.request.HTTPSHandler(context = context) opener = urllib.request.build_opener(proxy, secure_handler) opener.addheaders = [('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:54.0) Gecko/20100101 Firefox/54.0')] response = opener.open(URL) tree = etree.parse(response, parser=etree.HTMLParser()) print(tree.docinfo.doctype) = CODE BLOCK END = My first problem is that CERTIFICATE_VERIFY_FAILED error happens. I've found that something similar happens in macOS since Python installs its own set of trusted CA. But this isn't macOS and I can fetch HTTPS normally with curl and other tools. = TRACE BLOCK BEGIN = Traceback (most recent call last): File "/usr/lib64/python3.6/urllib/request.py", line 1318, in do_open encode_chunked=req.has_header('Transfer-encoding')) File "/usr/lib64/python3.6/http/client.py", line 1239, in request self._send_request(method, url, body, headers, encode_chunked) File "/usr/lib64/python3.6/http/client.py", line 1285, in _send_request self.endheaders(body, encode_chunked=encode_chunked) File "/usr/lib64/python3.6/http/client.py", line 1234, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "/usr/lib64/python3.6/http/client.py", line 1026, in _send_output self.send(msg) File "/usr/lib64/python3.6/http/client.py", line 964, in send self.connect() File "/usr/lib64/python3.6/http/client.py", line 1400, in connect server_hostname=server_hostname) File "/usr/lib64/python3.6/ssl.py", line 401, in wrap_socket _context=self, _session=session) File "/usr/lib64/python3.6/ssl.py", line 808, in __init__ self.do_handshake() File "/usr/lib64/python3.6/ssl.py", line 1061, in do_handshake self._sslobj.do_handshake() File "/usr/lib64/python3.6/ssl.py", line 683, in do_handshake self._sslobj.do_handshake() ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "./https_test.py", line 21, in response = opener.open(URL) File "/usr/lib64/python3.6/urllib/request.py", line 526, in open response = self._open(req, data) File "/usr/lib64/python3.6/urllib/request.py", line 544, in _open '_open', req) File "/usr/lib64/python3.6/urllib/request.py", line 504, in _call_chain result = func(*args) File "/usr/lib64/python3.6/urllib/request.py", line 1361, in https_open context=self._context, check_hostname=self._check_hostname) File "/usr/lib64/python3.6/urllib/request.py", line 1320, in do_open raise URLError(err) urllib.error.URLError: certificate verify failed (_ssl.c:749)> = TRACE BLOCK END = Second problem is that for HTTP requests proxy is used, but for HTTPS it makes a direct connection (verified with tcpdump). I've read at docs.python.org that previous versions of Python couldn't handle HTTPS with proxy but that shortcoming seems to have gone now. Please help :) -- https://mail.python.org/mailman/listinfo/python-list