import locale and print range on same line
Can someone tell me why next code doesn't work?
import locale; locale.setlocale(locale.LC_ALL, ""); for i in range(1,20,4):
print(locale.format("%2f", i, 1))
It gives an error: SyntaxError: invalid syntax --> indicating 'for'
However I need to put the code on one single line.
When I separate them like below it works fine.
import locale
locale.setlocale(locale.LC_ALL, "")
for i in range(1,20,4):
print(locale.format("%2f", i, 1))
--
https://mail.python.org/mailman/listinfo/python-list
locale and for loop on same line
Can someone tell me why this doesn't work?
import locale; locale.setlocale(locale.LC_ALL, ""); for i in range(1,20,4):
print(locale.format("%2f", i, 1))
It gives an error: SyntaxError: invalid syntax (highlighting the word 'for')
I need this code on one and the same line.
However when I separate them it works fine:
import locale
locale.setlocale(locale.LC_ALL, "")
for i in range(1,20,4):
print(locale.format("%2f", i, 1))
--
https://mail.python.org/mailman/listinfo/python-list
Re: one more question on regex
2016-01-22 23:47 GMT+01:00 mg : > Il Fri, 22 Jan 2016 21:10:44 +0100, Vlastimil Brom ha scritto: > >> [...] > > You explanation of re.findall() results is correct. My point is that the > documentation states: > > re.findall(pattern, string, flags=0) > Return all non-overlapping matches of pattern in string, as a list of > strings > > and this is not what re.findall does. IMHO it should be more reasonable > to get back the whole matches, since this seems to me the most useful > information for the user. In any case I'll go with finditer, that returns > in match object all the infos that anyone can look for. > -- > https://mail.python.org/mailman/listinfo/python-list Hi, I don't know the reasoning for this special behaviour of findall, but it seems to be documented explicitly: https://docs.python.org/3/library/re.html#re.findall "... 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. finditer is clearly much more robust for general usage. I only use findall for quick one-line tests (and there one has to account for this specificities - either by using non capturing groups or enclosing the whole pattern in a "main" group and use the first items in the resulting tuples. vbr -- https://mail.python.org/mailman/listinfo/python-list
Re: import locale and print range on same line
[email protected]: > Can someone tell me why next code doesn't work? > > import locale; locale.setlocale(locale.LC_ALL, ""); for i in > range(1,20,4): print(locale.format("%2f", i, 1)) > > It gives an error: SyntaxError: invalid syntax --> indicating 'for' > > However I need to put the code on one single line. > When I separate them like below it works fine. > > import locale > locale.setlocale(locale.LC_ALL, "") > for i in range(1,20,4): >print(locale.format("%2f", i, 1)) The answer is in Python's syntax definition. Not everything is allowed on a single line. See https://docs.python.org/3/reference/grammar.html> Only small_stmt's can be separated by semicolons. A for statement is a compound_stmt, which is not a small_stmt. Marko -- https://mail.python.org/mailman/listinfo/python-list
Re: import locale and print range on same line
2016-01-23 11:36 GMT+01:00 Marko Rauhamaa : > [email protected]: > >> Can someone tell me why next code doesn't work? >> >> import locale; locale.setlocale(locale.LC_ALL, ""); for i in >> range(1,20,4): print(locale.format("%2f", i, 1)) >> >> It gives an error: SyntaxError: invalid syntax --> indicating 'for' >> >> However I need to put the code on one single line. >> When I separate them like below it works fine. >> >> import locale >> locale.setlocale(locale.LC_ALL, "") >> for i in range(1,20,4): >>print(locale.format("%2f", i, 1)) > > The answer is in Python's syntax definition. Not everything is allowed > on a single line. > > See https://docs.python.org/3/reference/grammar.html> > > Only small_stmt's can be separated by semicolons. > > A for statement is a compound_stmt, which is not a small_stmt. > > > Marko > -- > https://mail.python.org/mailman/listinfo/python-list Hi, if you realy can't use multiple lines (which is rather essencial for python), you can circumvent this in some cases, e.g. using list comprehension and ignoring the created list itself: import locale; locale.setlocale(locale.LC_ALL, ""); [print(locale.format("%2f", i, 1)) for i in range(1,20,4)] vbr -- https://mail.python.org/mailman/listinfo/python-list
Re: import locale and print range on same line
This works also but I thought it was possible to do it easier:
import locale; locale.setlocale(locale.LC_ALL, "");
print('\n'.join(locale.format("%2f", i, 1) for i in range(1,20,4)))
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to simulate C style integer division?
On 22/01/16 04:48, Steven D'Aprano wrote: [ ... ] math.trunc( float(a) / b ) That fails for sufficiently big numbers: py> a = 3**1000 * 2 py> b = 3**1000 py> float(a)/b # Exact answer should be 2 Traceback (most recent call last): File "", line 1, in OverflowError: long int too large to convert to float Note that Python gets the integer division correct: py> a//b 2L And even gets true division correct: py> from __future__ import division py> a/b 2.0 so it's just the intermediate conversion to float that fails. Thanks! I did see recommandations to avoid floats throughout the thread, but didn't understand why. Following code should be exempt from such shortcomings : def intdiv(a, b): return (a - (a % (-b if a < 0 else b))) / b -- https://mail.python.org/mailman/listinfo/python-list
Re: How to simulate C style integer division?
def intdiv(a, b): return (a - (a % (-b if a < 0 else b))) / b Duh ... Got confused with modulos (again). def intdiv(a, b): return (a - (a % (-abs(b) if a < 0 else abs(b / b -- https://mail.python.org/mailman/listinfo/python-list
Re: import locale and print range on same line
On Sat, 23 Jan 2016 09:02 pm, [email protected] wrote: > However I need to put the code on one single line. Why? Is the Enter key on your keyboard broken? -- Steven -- https://mail.python.org/mailman/listinfo/python-list
Re: import locale and print range on same line
On Sun, Jan 24, 2016 at 12:07 AM, Steven D'Aprano wrote: > On Sat, 23 Jan 2016 09:02 pm, [email protected] wrote: > >> However I need to put the code on one single line. > > Why? Is the Enter key on your keyboard broken? Maybe it's for a python -c invocation. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: locale and for loop on same line
According to the documentation, "...simple statements may occur on a
single line separated by semicolons."
The "for" statement is a compound, not simple, statement.
Would it be possible to place your statements in a function and then you
would just need to invoke the function?
Bev in TX
On 1/23/16 4:08 AM, Ramo wrote:
Can someone tell me why this doesn't work?
import locale; locale.setlocale(locale.LC_ALL, ""); for i in range(1,20,4):
print(locale.format("%2f", i, 1))
It gives an error: SyntaxError: invalid syntax (highlighting the word 'for')
I need this code on one and the same line.
However when I separate them it works fine:
import locale
locale.setlocale(locale.LC_ALL, "")
for i in range(1,20,4):
print(locale.format("%2f", i, 1))
--
https://mail.python.org/mailman/listinfo/python-list
Re: import locale and print range on same line
On Sun, 24 Jan 2016 12:19 am, Chris Angelico wrote: > On Sun, Jan 24, 2016 at 12:07 AM, Steven D'Aprano > wrote: >> On Sat, 23 Jan 2016 09:02 pm, [email protected] wrote: >> >>> However I need to put the code on one single line. >> >> Why? Is the Enter key on your keyboard broken? > > Maybe it's for a python -c invocation. [steve@ando ~]$ python -c "for i in range(5): > print 'hello world' > " hello world hello world hello world hello world hello world [steve@ando ~]$ -- Steven -- https://mail.python.org/mailman/listinfo/python-list
Re: import locale and print range on same line
On Sun, Jan 24, 2016 at 12:45 AM, Steven D'Aprano wrote: > On Sun, 24 Jan 2016 12:19 am, Chris Angelico wrote: > >> On Sun, Jan 24, 2016 at 12:07 AM, Steven D'Aprano >> wrote: >>> On Sat, 23 Jan 2016 09:02 pm, [email protected] wrote: >>> However I need to put the code on one single line. >>> >>> Why? Is the Enter key on your keyboard broken? >> >> Maybe it's for a python -c invocation. > > > [steve@ando ~]$ python -c "for i in range(5): >> print 'hello world' >> " > hello world > hello world > hello world > hello world > hello world > [steve@ando ~]$ Well, not everyone's shells are as awesome as bash... and not everyone knows you can do that. That said, though... this is a good reason for giving full context for the question. "I'm trying to X, but I get this error because of Y. How would I go about fixing Y?" - response: "Actually, you can do X in a slightly different way.". Just sayin', there might be other reasons for wanting to put things onto one line. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: import locale and print range on same line
The reason why I want to have it on onto one line has nothing to do with my question, "why doesn't it work on one line" :) But if you want to know it, I use this python code in the commandline of a texteditor :) Btw.. thank you all for your help. Very happy with it :) -- https://mail.python.org/mailman/listinfo/python-list
Question about asyncio and blocking operations
Hi all
I am developing a typical accounting/business application which involves a
front-end allowing clients to access the system, a back-end connecting to a
database, and a middle layer that glues it all together.
Some time ago I converted the front-end from a multi-threaded approach to an
asyncio approach. It was surprisingly easy, and did not require me to delve
into asyncio too deeply.
There was one aspect that I deliberately ignored at that stage. I did not
change the database access to an asyncio approach, so all reading
from/writing to the database involved a blocking operation. I am now ready
to tackle that.
I find I am bumping my head more that I expected, so I thought I would try
to get some feedback here to see if I have some flaw in my approach, or if
it is just in the nature of writing an asynchronous-style application.
Here is the difficulty. The recommended way to handle a blocking operation
is to run it as task in a different thread, using run_in_executor(). This
method is a coroutine. An implication of this is that any method that calls
it must also be a coroutine, so I end up with a chain of coroutines
stretching all the way back to the initial event that triggered it. I can
understand why this is necessary, but it does lead to some awkward
programming.
I use a cache to store frequently used objects, but I wait for the first
request before I actually retrieve it from the database. This is how it
worked -
# cache of database objects for each company
class DbObject(dict):
def __missing__(self, company):
db_object = self[company] = get_db_object _from_database()
return db_object
db_objects = DbObjects()
Any function could ask for db_cache.db_objects[company]. The first time it
would be read from the database, on subsequent requests it would be returned
from the dictionary.
Now get_db_object_from_database() is a coroutine, so I have to change it to
db_object = self[company] = await get_db_object _from_database()
But that is not allowed, because __missing__() is not a coroutine.
I fixed it by replacing the cache with a function -
# cache of database objects for each company
db_objects = {}
async def get_db_object(company):
if company not in db_objects:
db_object = db_objects[company] = await get_db_object
_from_database()
return db_objects[company]
Now the calling functions have to call 'await
db_cache.get_db_object(company)'
Ok, once I had made the change it did not feel so bad.
Now I have another problem. I have some classes which retrieve some data
from the database during their __init__() method. I find that it is not
allowed to call a coroutine from __init__(), and it is not allowed to turn
__init__() into a coroutine.
I imagine that I will have to split __init__() into two parts, put the
database functionality into a separately-callable method, and then go
through my app to find all occurrences of instantiating the object and
follow it with an explicit call to the new method.
Again, I can handle that without too much difficulty. But at this stage I do
not know what other problems I am going to face, and how easy they will be
to fix.
So I thought I would ask here if anyone has been through a similar exercise,
and if what I am going through sounds normal, or if I am doing something
fundamentally wrong.
Thanks for any input
Frank Millman
--
https://mail.python.org/mailman/listinfo/python-list
Re: import locale and print range on same line
On Sun, Jan 24, 2016 at 1:03 AM, Ramo wrote: > The reason why I want to have it on onto one line has nothing to do with my > question, "why doesn't it work on one line" :) > > But if you want to know it, I use this python code in the commandline of a > texteditor :) Called it! :) It actually has a lot to do with your question, because it puts special restrictions on the answer. By giving context, you prevent answers like Steven's that are trying to fix what is thought to be a faulty assumption; instead, we'll attack the problem as an intellectual challenge - how can we achieve this goal within these restrictions? Worded that way, the question will definitely get responses that aren't what we'd normally call "good code", but they fit the situation. It's not uncommon to have questions like this, especially from a security point of view; for instance: "Is it possible to invoke os.system() without using any builtins or imports, and without having an underscore anywhere in the code?" (answer: yes, it is). ChrisA -- https://mail.python.org/mailman/listinfo/python-list
pip install mitmproxy - fails on watchdog-0.8.3.tar.gz with "Permission denied" error (Python 2.7.11 on Win XP SP3);
Greetings To Python-list,
I'm trying to install Python package:
mitmproxy (https://mitmproxy.org/)
on Windows XP SP3.
I'm a complete Python newbie. Not planning to do any Python programming
at this time. Just trying to get package mitmproxy working (or at least
the mitmdump component, since "There is no interactive user interface on
Windows." per:
http://docs.mitmproxy.org/en/stable/install.html
* * *
* * *
I have installed Python that announces itself on the Python command line
as:
Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec 5 2015, 20:32:19) [MSC
v.1500 32 bit (Intel)] on win32
* * *
* * *
I'm using the following command in a Win XP cmd.exe shell (not the
Python shell) to install mitmproxy:
pip install mitmproxy
The first install attempt failed, ending with some diagnostics displayed
in the Win XP cmd.exe shell window:
C:\Documents and Settings\SteveP>pip install mitmproxy
Collecting mitmproxy
Downloading mitmproxy-0.15.tar.gz (1.7MB)
100% || 1.7MB 4.5kB/s
...
Collecting Pillow<3.1,>=3.0.0 (from mitmproxy)
Downloading Pillow-3.0.0-cp27-none-win32.whl (1.2MB)
100% || 1.2MB 6.1kB/s
Collecting pyperclip<1.6,>=1.5.22 (from mitmproxy)
Downloading pyperclip-1.5.26.zip
Exception:
Traceback (most recent call last):
File "e:\a p p s\python27\lib\site-packages\pip\basecommand.py",
line 211, in main
status = self.run(options, args)
File "e:\a p p
s\python27\lib\site-packages\pip\commands\install.py", line 294, in run
requirement_set.prepare_files(finder)
File "e:\a p p s\python27\lib\site-packages\pip\req\req_set.py",
line 334, in prepare_files
functools.partial(self._prepare_file, finder))
File "e:\a p p s\python27\lib\site-packages\pip\req\req_set.py",
line 321, in _walk_req_to_install
more_reqs = handler(req_to_install)
File "e:\a p p s\python27\lib\site-packages\pip\req\req_set.py",
line 491, in _prepare_file
session=self.session)
File "e:\a p p s\python27\lib\site-packages\pip\download.py", line
825, in unpack_url
session,
File "e:\a p p s\python27\lib\site-packages\pip\download.py", line
677, in unpack_http_url
unpack_file(from_path, location, content_type, link)
File "e:\a p p s\python27\lib\site-packages\pip\utils\__init__.py",
line 645,in unpack_file
flatten=not filename.endswith('.whl')
File "e:\a p p s\python27\lib\site-packages\pip\utils\__init__.py",
line 546,in unzip_file
fp = open(fn, 'wb')
IOError: [Errno 13] Permission denied:
'c:\\docume~1\\stevep\\locals~1\\temp\\pip-buildpnnfyf\\pyperclip\\docs/make.bat'
You are using pip version 7.1.2, however version 8.0.0 is available.
You should consider upgrading via the 'python -m pip
install --upgrade pip' command.
C:\Documents and Settings\SteveP>
* * *
* * *
I upgraded pip to version 8.0.0:
C:\Documents and Settings\SteveP>python -m pip install --upgrade pip
Collecting pip
Downloading pip-8.0.0-py2.py3-none-any.whl (1.2MB)
100% || 1.2MB 9.1kB/s
Installing collected packages: pip
Found existing installation: pip 7.1.2
Uninstalling pip-7.1.2:
Successfully uninstalled pip-7.1.2
Successfully installed pip-8.0.0
* * *
* * *
I tried again to install mitmproxy, and the installation got a little
further, but failed again on a similar error, on a different file
(watchdog-0.8.3.tar.gz instead of pyperclip-1.5.26.zip):
C:\Documents and Settings\SteveP>pip install mitmproxy
C:\Documents and Settings\SteveP>pip install mitmproxy
e:\a p p s\python27\lib\site-packages\pip\pep425tags.py:89:
RuntimeWarning: Config variable 'Py_DEBUG' is unset, Python ABI tag may
be incorrect
warn=(impl == 'cp')):
e:\a p p s\python27\lib\site-packages\pip\pep425tags.py:93:
RuntimeWarning: Config variable 'WITH_PYMALLOC' is unset, Python ABI tag
may be incorrect
warn=(impl == 'cp')):
e:\a p p s\python27\lib\site-packages\pip\pep425tags.py:99:
RuntimeWarning: Config variable 'Py_UNICODE_SIZE' is unset, Python ABI
tag may be incorrect
sys.version_info < (3, 3))) \
Collecting mitmproxy
Using cached mitmproxy-0.15.tar.gz
Collecting six<1.11,>=1.10.0 (from mitmproxy)
Using cached six-1.10.0-py2.py3-none-any.whl
...
Collecting pyperclip<1.6,>=1.5.22 (from mitmproxy)
Using cached pyperclip-1.5.26.zip
Collecting pydivert>=0.0.7 (from mitmproxy)
Downloading pydivert-0.0.7.zip
Collecting watchdog<0.9,>=0.8.3 (from mitmproxy)
Downloading watchdog-0.8.3.tar.gz (83kB)
100% || 86kB 6.8kB/s
Exception:
Traceback (most recent call last):
File "e:\a p p s\python27\lib\site-packages\pip\basecommand.py",
line 209, in main
status = self.run(options, args)
File "e:\a p p
s\python27\lib\site-packages\pip\commands\install.py", line 299, in run
requirement_set.prep
Release of PyGreSQL version 4.2
The PyGreSQL team is please to announce release 4.2 of PyGreSQL. It is available at: http://pygresql.org/files/PyGreSQL-4.2.tgz. If you are running NetBSD, look in the packages directory under databases for py-postgresql. There should also be a package in the FreeBSD ports collection. Please refer to the changelog for things that have changed in this version. The changelog and other information can be found at the web site http://www.pygresql.org/. This version has been built and unit tested on: - NetBSD - FreeBSD - openSUSE - Ubuntu - Windows 7 with both MinGW and Visual Studio - PostgreSQL 8.4 and 9.3 64bit - Python 2.4, 2.5, 2.6 and 2.7 32 and 64bit This is the last major release before 5.0 which will include support for Python 3.x. Only bug fixes will be applied to this branch. A beta of 5.0 is available at http://pygresql.org/files/PyGreSQL-beta.tar.gz. -- D'Arcy J.M. Cain PyGreSQL Development Group http://www.PyGreSQL.org IM:[email protected] -- https://mail.python.org/mailman/listinfo/python-list
Deprecation warning for async and await
I enabled the deprecation warning in Python 3.5.1 and Python 3.6 dev, but I did not get any warning when assigning to async or await: $ python -Wd -c "import sys; print(sys.version); async = 33" 3.5.1 (default, Jan 21 2016, 19:59:28) [GCC 4.8.4] Is it normal? -- Marco Buttu INAF-Osservatorio Astronomico di Cagliari Via della Scienza n. 5, 09047 Selargius (CA) Phone: 070 711 80 217 Email: [email protected] -- https://mail.python.org/mailman/listinfo/python-list
FW: Bill Sconce memorial, SAT 13 FEB, Boire Field, Nashua, NH
:-( -- Forwarded message -- From: [email protected] Date: Sat, Jan 16, 2016 at 2:11 PM Subject: Bill Sconce obituary and memorial date (February 13th at Boire Field, Nashua, NH) To: "Linux, Greater" Cc: "Hall, Jon" Bill (William Joseph) Sconce, age 72, Lyndeborough, NH, died on January 5, 2016 at Lahey Hospital in Burlington, MA. The cause was a cerebral hemorrhage. He was a good man. Bill was born April 19, 1943 in Indianapolis, IN, and came home to the House on the Hill in Edinburgh, IN. Bill grew up there with his brother David, who predeceased him. His parents were Eva Mae and Joseph Byce Sconce. Bill soon became a proficient Spelunker and surveyor in the caves of Indiana and Kentucky, and a motorcycle enthusiast. Graduating from Culver Military Academy, where he earned his Amateur Radio License, he received a Fulbright scholarship and rode his Norton motorcycle to CalTech in San Francisco, CA where he studied Physics and worked in a crystalography laboratory. He was drafted during the Vietnam war protests at that school and served in Taigu Korea, where he studied IBM Cobol and the Korean language, and rode a Honda 90 motorcycle in the mountains. He returned to Louisville, KY and began a long career in computer science and founded his company Industrial Specialities. He met the love of his life in Louisville, Janet Levy, and with her encouragement he completed his dream of becoming a pilot, holding a Commercial, Instrument, and Instructor license. He continued studies at University of Louisville in linguistics and computer science. Bill & Janet moved to NH in 1979 for Bill to graduate from being Symposium Coordinator for DECUS to assume the position of Product Manager for the RSTS Group at Digital Equipment Corporation. Bill worked for and was layed off from DEC, Compaq, and Hewlett Packard, at which point he revived his corporation, named it In Spec, Inc. and divided his time between software engineering and flight instruction. Bill was a devoted supporter of GPL and "free" Linux software and the Python programming language. Bill was a member of the Vintage BMW Motorcycle Owners, Ltd., the BMW MOVer Motorcycle Club of Vermont, the Contoocook Valley Radio Club, a life member of the National Speological Society and the American Radio Relay League. He supported the EAA and was a Regional Judge for aerobatic competitions for IAC for many years. He loved aviation, including hot air ballooning and skydiving. He participated in Young Eagles at Boire Airport in Nashua, NH and enjoyed teaching young people to fly. He taught spins in his Cessna Aerobat. And he was a Quiet Birdman. He was a member of the Rex Stout Wolfepack Book Club and The Wodehouse Society. Bill loved theatre, classical and rock music, and especially lately, attending Dr. David Landman's Poetry Nights of medieval poetry in Lexington, MA. He loved fixing things and if there were no parts available for a project he promptly made them himself on his metal lathe, or just used his ingenuity to create something needed. He loved cigars, scotch, butter, reading, airplanes, old test equipment, Paris, BMWs, his red convertible Cabriolet with red earmuffs, and his big black 4 cylinder 4WD truck, bird watching (outwitting squirrels), camping, hiking on Pitcher Mountain, William Blake, and he suffered not fools. One of his favorite lead-ins: "As an engineer..." Bill is survived by his wife, Janet Levy Sconce, his sister-in-law, June Levy and her family, and many dear friends. Bill was a kind and loving "daddy" to Virgil Fox and RDB, the cats of his home. Thanks to "The Committee" and especially Donna Shea, Chris Levin, Ken Hamel, Donna Giovannini, Tom Steger, Michelle Donovan, Simon Hutchings, John & Cathy Gubernat and the surgeons, doctors, and nurses at Lahey Hospital. The family is grateful to all of his many friends who offered support and love. There will be a memorial for Bill on February 13, 11:00-2:00 at Nashua Jet Aviation located on Boire Field in Nashua, NH. Call Air Direct Airways, (603) 882-5606 for more information. ___ gnhlug-discuss mailing list [email protected] http://mail.gnhlug.org/mailman/listinfo/gnhlug-discuss/ -- https://mail.python.org/mailman/listinfo/python-list
Re: How to simulate C style integer division?
Grobu writes: >> def intdiv(a, b): >> return (a - (a % (-b if a < 0 else b))) / b >> >> > > Duh ... Got confused with modulos (again). > > def intdiv(a, b): > return (a - (a % (-abs(b) if a < 0 else abs(b / b You should use // here to get an exact integer result. -- https://mail.python.org/mailman/listinfo/python-list
Re: Question about asyncio and blocking operations
On Sun, Jan 24, 2016 at 1:38 AM, Frank Millman wrote:
> I find I am bumping my head more that I expected, so I thought I would try
> to get some feedback here to see if I have some flaw in my approach, or if
> it is just in the nature of writing an asynchronous-style application.
I don't have a lot of experience with Python's async/await as such,
but I've written asynchronous apps using a variety of systems (and
also written threaded ones many times). so I'll answer questions on
the basis of design principles that were passed down to me through the
generations.
> I use a cache to store frequently used objects, but I wait for the first
> request before I actually retrieve it from the database. This is how it
> worked -
>
> # cache of database objects for each company
> class DbObject(dict):
>def __missing__(self, company):
>db_object = self[company] = get_db_object _from_database()
>return db_object
> db_objects = DbObjects()
>
> Any function could ask for db_cache.db_objects[company]. The first time it
> would be read from the database, on subsequent requests it would be returned
> from the dictionary.
>
> Now get_db_object_from_database() is a coroutine, so I have to change it to
>db_object = self[company] = await get_db_object _from_database()
>
> But that is not allowed, because __missing__() is not a coroutine.
>
> I fixed it by replacing the cache with a function -
>
> # cache of database objects for each company
> db_objects = {}
> async def get_db_object(company):
>if company not in db_objects:
>db_object = db_objects[company] = await get_db_object
> _from_database()
>return db_objects[company]
>
> Now the calling functions have to call 'await
> db_cache.get_db_object(company)'
>
> Ok, once I had made the change it did not feel so bad.
I would prefer the function call anyway. Subscripting a dictionary is
fine for something that's fairly cheap, but if it's potentially hugely
expensive, I'd rather see it spelled as a function call. There's
plenty of precedent for caching function calls so only the first one
is expensive.
> Now I have another problem. I have some classes which retrieve some data
> from the database during their __init__() method. I find that it is not
> allowed to call a coroutine from __init__(), and it is not allowed to turn
> __init__() into a coroutine.
>
> I imagine that I will have to split __init__() into two parts, put the
> database functionality into a separately-callable method, and then go
> through my app to find all occurrences of instantiating the object and
> follow it with an explicit call to the new method.
>
> Again, I can handle that without too much difficulty. But at this stage I do
> not know what other problems I am going to face, and how easy they will be
> to fix.
The question here is: Until you get that data from the database, what
state would the object be in? There are two basic options:
1) If the object is somewhat usable and meaningful, divide
initialization into two parts - one that sets up the object itself
(__init__) and one that fetches stuff from the database. If you can,
trigger the database fetch in __init__ so it's potentially partly done
when you come to wait for it.
2) If the object would be completely useless, use an awaitable factory
function instead. Rather than constructing an object, you ask an
asynchronous procedure to give you an object. It's a subtle change,
and by carefully managing the naming, you could make it almost
transparent in your code:
# Old way:
class User:
def __init__(self, domain, name):
self.id = blocking_database_call("get user", domain, name)
# And used thus:
me = User("example.com", "rosuav")
# New way:
class User:
def __init__(self, id):
self.id = id
_User = User
async def User(domain, name):
id = await async_database_call("get user", domain, name)
return _User(id)
# And used thus:
me = await User("example.com", "rosuav")
> So I thought I would ask here if anyone has been through a similar exercise,
> and if what I am going through sounds normal, or if I am doing something
> fundamentally wrong.
I think this looks pretty much right. There are some small things you
can do to make it look a bit easier, but it's minor.
ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: pip install mitmproxy - fails on watchdog-0.8.3.tar.gz with "Permission denied" error (Python 2.7.11 on Win XP SP3);
On Fri, Jan 22, 2016 at 3:40 AM, Steve Petrie, P.Eng. wrote: > In both failure cases, it looks to me like there is a bug in the pip logic, > that is using a *nix forward slash "/" instead of a double backslash "\\" > before the file name "make.bat". I'm not sure what your exact problem is, but I can say that it isn't this; the Unix-style forward slash is perfectly legal under Windows (and it's even legal to mix and match). ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Question about asyncio and blocking operations
On Sat, Jan 23, 2016 at 7:38 AM, Frank Millman wrote:
> Here is the difficulty. The recommended way to handle a blocking operation
> is to run it as task in a different thread, using run_in_executor(). This
> method is a coroutine. An implication of this is that any method that calls
> it must also be a coroutine, so I end up with a chain of coroutines
> stretching all the way back to the initial event that triggered it.
This seems to be a common misapprehension about asyncio programming.
While coroutines are the focus of the library, they're based on
futures, and so by working at a slightly lower level you can also
handle them as such. So while this would be the typical way to use
run_in_executor:
async def my_coroutine(stuff):
value = await get_event_loop().run_in_executor(None,
blocking_function, stuff)
result = await do_something_else_with(value)
return result
This is also a perfectly valid way to use it:
def normal_function(stuff):
loop = get_event_loop()
coro = loop.run_in_executor(None, blocking_function, stuff)
task = loop.create_task(coro)
task.add_done_callback(do_something_else)
return task
> I use a cache to store frequently used objects, but I wait for the first
> request before I actually retrieve it from the database. This is how it
> worked -
>
> # cache of database objects for each company
> class DbObject(dict):
>def __missing__(self, company):
>db_object = self[company] = get_db_object _from_database()
>return db_object
> db_objects = DbObjects()
>
> Any function could ask for db_cache.db_objects[company]. The first time it
> would be read from the database, on subsequent requests it would be returned
> from the dictionary.
>
> Now get_db_object_from_database() is a coroutine, so I have to change it to
>db_object = self[company] = await get_db_object _from_database()
>
> But that is not allowed, because __missing__() is not a coroutine.
>
> I fixed it by replacing the cache with a function -
>
> # cache of database objects for each company
> db_objects = {}
> async def get_db_object(company):
>if company not in db_objects:
>db_object = db_objects[company] = await get_db_object
> _from_database()
>return db_objects[company]
>
> Now the calling functions have to call 'await
> db_cache.get_db_object(company)'
>
> Ok, once I had made the change it did not feel so bad.
This all sounds pretty reasonable to me.
> Now I have another problem. I have some classes which retrieve some data
> from the database during their __init__() method. I find that it is not
> allowed to call a coroutine from __init__(), and it is not allowed to turn
> __init__() into a coroutine.
>
> I imagine that I will have to split __init__() into two parts, put the
> database functionality into a separately-callable method, and then go
> through my app to find all occurrences of instantiating the object and
> follow it with an explicit call to the new method.
>
> Again, I can handle that without too much difficulty. But at this stage I do
> not know what other problems I am going to face, and how easy they will be
> to fix.
>
> So I thought I would ask here if anyone has been through a similar exercise,
> and if what I am going through sounds normal, or if I am doing something
> fundamentally wrong.
This is where it would make sense to me to use callbacks instead of
subroutines. You can structure your __init__ method like this:
def __init__(self, params):
self.params = params
self.db_object_future = get_event_loop().create_task(
get_db_object(params))
async def method_depending_on_db_object():
db_object = await self.db_object_future
result = do_something_with(db_object)
return result
The caveat with this is that while __init__ itself doesn't need to be
a coroutine, any method that depends on the DB lookup does need to be
(or at least needs to return a future).
--
https://mail.python.org/mailman/listinfo/python-list
Re: Question about asyncio and blocking operations
On Sat, Jan 23, 2016 at 8:44 AM, Ian Kelly wrote: > This is where it would make sense to me to use callbacks instead of > subroutines. You can structure your __init__ method like this: Doh. s/subroutines/coroutines -- https://mail.python.org/mailman/listinfo/python-list
Re: Question about asyncio and blocking operations
"Frank Millman" wrote in message news:[email protected]... So I thought I would ask here if anyone has been through a similar exercise, and if what I am going through sounds normal, or if I am doing something fundamentally wrong. Thanks for any input Just a quick note of thanks to ChrisA and Ian. Very interesting responses and plenty to think about. I will have to sleep on it and come back with renewed vigour in the morning. I may well be back with more questions :-) Frank -- https://mail.python.org/mailman/listinfo/python-list
Re: How to simulate C style integer division?
On 23/01/16 16:07, Jussi Piitulainen wrote: Grobu writes: def intdiv(a, b): return (a - (a % (-b if a < 0 else b))) / b Duh ... Got confused with modulos (again). def intdiv(a, b): return (a - (a % (-abs(b) if a < 0 else abs(b / b You should use // here to get an exact integer result. You're totally right, thanks! It isn't an issue for Python 2.x's "classic division", but it becomes one with Python 3's "True division", and '//' allows to play it safe all along. -- https://mail.python.org/mailman/listinfo/python-list
Install Numba on Debian
I'd like to install Numba on Debian Jessie to work with the system Python 2.7.9 (rather than installing Anaconda). When I follow the instructions at https://github.com/numba/numba#custom-python-environments ...I get errors when trying to install Numba either with the git clone method or installing it with pip. I'll put the error log on Pastebin since its so large: http://pastebin.com/gbrj0iHw I've Googled for recipes for installing numba on Debian, installed every Debian package suggested as a possible dependency, and still no luck. Anybody know a working method for installing Numba on Debian Jessie without Anaconda? -- https://mail.python.org/mailman/listinfo/python-list
Re: pip install mitmproxy - fails on watchdog-0.8.3.tar.gz with "Permission denied" error (Python 2.7.11 on Win XP SP3);
On Sun, Jan 24, 2016 at 6:43 AM, Steve Petrie, P.Eng. wrote: > In case other Windows XP "orphans" want to use mitmdump, here's what I > learned (via Google): > > I changed the bang line (wrapping the pathname in double quotes) in file > mitmdump-script.py: > > from: #!e:\a p p s\python27\python.exe > to: #!"e:\a p p s\python27\python.exe" Spaces in names are often a pain. I'd raise this upstream as a bug report - it should be fixed properly rather than depending on manual editing. > I haven't yet got mitmdump to actually write anything to the output file, > but I expect that's just a matter of studying the mitmdump docs. I'd say you'll do best with a mailing list dedicated to mitmdump; what you're looking at now is application-specific issues. > P.S. Be assured -- I will soon be moving away from Windows XP to a new > computer running Debian Linux. Good plan! That's what I use on all my systems. So many things are easier when you move off XP. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Same function but different names with different set of default arguments
Às 07:30 de 21-01-2016, Paulo da Silva escreveu: > Hi all. > > What is the fastest implementation of the following code? > > def g(p): > ... > return something > > def f1(p="p1"): > return g(p) > > def f2(p="p2"): > return g(p) > Thanks to all who responded. I'll try "partial" in my case. It seems good and simple. Paulo -- https://mail.python.org/mailman/listinfo/python-list
Re: Question about how to do something in BeautifulSoup?
On Fri, Jan 22, 2016 at 8:01 AM, inhahe wrote:
> Say I have the following HTML (I hope this shows up as plain text here
> rather than formatting):
>
> "Is
> today the day?"
>
> And I want to extract the "Is today the day?" part. There are other places
> in the document with and , but this is the only place that
> uses color #00, so I want to extract anything that's within a color
> #00 style, even if it's nested multiple levels deep within that.
>
> - Sometimes the color is defined as RGB(0, 0, 0) and sometimes it's
defined
> as #00
> - Sometimes the is within the and sometimes the is
> within the .
> - There may be other discrepancies I haven't noticed yet
>
> How can I do this in BeautifulSoup (or is this better done in lxml.html)?
I hope this helps you get started:
This may help you get started:
from bs4 import BeautifulSoup
from itertools import chain
soup = BeautifulSoup('''\
"Is
today the day?"
"Is
tomorrow the day?"''')
# We're going to get all the tags that specify the color, either using hex
or RGB.
# If you only want to get the span tags, just give the positional argument
'span' to
# find_all:
# for tag in chain(soup.find_all('span', style='color: #00;'),
# soup.find_all('span', style='color: RGB(0, 0, 0);')):
for tag in chain(soup.find_all(style='color: #00;'),
soup.find_all(style='color: RGB(0, 0, 0);')):
try:
print(tag.em.strong.text)
except AttributeError:
try:
print(tag.strong.em.text)
except AttributeError:
print('ooh no no text')
Cody
--
https://mail.python.org/mailman/listinfo/python-list
Re: Install Numba on Debian
Dave Farrance wrote: >I'd like to install Numba on Debian Jessie to work with the system >Python 2.7.9 (rather than installing Anaconda). OK, never mind. Fixed. By Googling the first error code, finding a suggested fix for that, running again, Googling the new error, and several repeats of that, and I've now got a working solution. This recipe that somebody put on Github overcomes the main hurdle: https://gist.github.com/sshillo/62955e46cb05e2b47ad3 The main issue seems to be that contrary to the instructions on the Numba website, it's now a requirement to have LLVM 3.6 installed, not LLVM 3.5 which is in the Debian repository, and not LLVM 3.7 which is the latest release at llvm.org. Fortunately, a prebuilt LLVM 3.6 is available at llvm.org. Also I needed to install this lot from the Debian repository: python-pip python-setuptools build-essential python-enum34 libedit-dev python-dev -- https://mail.python.org/mailman/listinfo/python-list
Calculating longitudinal acceleration, lateral acceleration and normal acceleration
Is there a python library to calculate longitudinal acceleration, lateral acceleration and normal acceleration? Thanks. -- https://mail.python.org/mailman/listinfo/python-list
How do I add 18 seconds ISO-8301 String in Python?
How do I add 18 seconds to this string in Python? 2000-01-01T16:36:25.000Z -- https://mail.python.org/mailman/listinfo/python-list
Re: How do I add 18 seconds to an ISO-8601 String in Python?
Robert James Liguori writes: (I've corrected the Subject field. The standard you're referring to is ISO 8601, I believe.) > How do I add 18 seconds to this string in Python? > > 2000-01-01T16:36:25.000Z Two separate parts: * How do I get a timestamp object from a text representation in ISO 8601 format? * How do I add 18 seconds to a timestamp object? Parsing a text representation of a timestamp to get a timestamp object is done with ‘datetime.strptime’ from the Python standard library https://docs.python.org/3/library/datetime.html#datetime.datetime.strptime>:: >>> import datetime >>> foo_timestamp_text = "2000-01-01T16:36:25.000Z" >>> iso8601_format = "%Y-%m-%dT%H:%M:%S.%fZ" >>> foo_timestamp = datetime.datetime.strptime(foo_timestamp_text, iso8601_format) >>> foo_timestamp datetime.datetime(2000, 1, 1, 16, 36, 25) Arithmetic on timestamps is done using the ‘datetime.timedelta’ type https://docs.python.org/3/library/datetime.html#timedelta-objects>:: >>> increment = datetime.timedelta(seconds=18) >>> increment datetime.timedelta(0, 18) >>> foo_timestamp + increment datetime.datetime(2000, 1, 1, 16, 36, 43) -- \“You don't change the world by placidly finding your bliss — | `\you do it by focusing your discontent in productive ways.” | _o__) —Paul Z. Myers, 2011-08-31 | Ben Finney -- https://mail.python.org/mailman/listinfo/python-list
Re: How do I add 18 seconds to an ISO-8601 String in Python?
Oh... How do I convert it back to ISO 8301? -- https://mail.python.org/mailman/listinfo/python-list
Re: How do I add 18 seconds to an ISO-8601 String in Python?
I cant thank you enough -- https://mail.python.org/mailman/listinfo/python-list
Re: How do I add 18 seconds to an ISO-8601 String in Python?
Thank you so much! Btw, how do I convert back to ISO-8301? -- https://mail.python.org/mailman/listinfo/python-list
Re: How do I add 18 seconds to an ISO-8601 String in Python?
On 01/23/2016 07:22 PM, Robert James Liguori wrote: > Thank you so much! Btw, how do I convert back to ISO-8301? Have a look at the documentation for the datetime module. The docs will tell you how you can convert to a string, formatted to your specifications and needs. As always, the documentation should be the first place you look, followed by internet searches. -- https://mail.python.org/mailman/listinfo/python-list
Re: import locale and print range on same line
On 1/23/2016 8:58 AM, Chris Angelico wrote: On Sun, Jan 24, 2016 at 12:45 AM, Steven D'Aprano wrote: On Sun, 24 Jan 2016 12:19 am, Chris Angelico wrote: On Sun, Jan 24, 2016 at 12:07 AM, Steven D'Aprano wrote: On Sat, 23 Jan 2016 09:02 pm, [email protected] wrote: However I need to put the code on one single line. Why? Is the Enter key on your keyboard broken? Maybe it's for a python -c invocation. [steve@ando ~]$ python -c "for i in range(5): print 'hello world' " hello world hello world hello world hello world hello world [steve@ando ~]$ Well, not everyone's shells are as awesome as bash... Like Windows command prompt is not. I tried: C:\Users\Terry>python -c "for i in range(5):\n\tprint('hello world')" File "", line 1 for i in range(5):\n print('hello world') ^ SyntaxError: unexpected character after line continuation character -c does not preprocess the code string before executing. I may propose that it do so. However, Python is still pretty awesome. C:\Users\Terry>python -c "exec('''for i in range(5):\n print('hello world')''')" hello world hello world hello world hello world hello world and not everyone knows you can do that. One can even combine -i (interactive) with -c (code). C:\Users\Terry> python -i -c "exec('''a=[]\nfor i in (1,2,3):\n\ta.append(i)''')" >>> a [1, 2, 3] >>> -- Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list
Re: import locale and print range on same line
On Sun, Jan 24, 2016 at 1:45 PM, Terry Reedy wrote:
> C:\Users\Terry>python -c "for i in range(5):\n\tprint('hello world')"
> File "", line 1
> for i in range(5):\n print('hello world')
> ^
> SyntaxError: unexpected character after line continuation character
>
> -c does not preprocess the code string before executing. I may propose that
> it do so.
As an alternative, maybe multiple -c parameters could result in
multiple lines? Something like:
python3 -c "for i in range(5):" -c " print('Hello, world')"
Currently, a second -c is unparsed (it ends up in sys.argv), so this
would technically be a behavioural change (but then, so would
preprocessing).
ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: How do I add 18 seconds to an ISO-8601 String in Python?
Robert James Liguori writes: > Thank you so much! Btw, how do I convert back to ISO-8301? You are consistently referring to “ISO 8301”, but I am confident that you are not intending to talk about: ISO 8301: Thermal insulation -- Determination of steady-state thermal resistance and related properties -- Heat flow meter apparatus http://www.iso.org/iso/catalogue_detail.htm?csnumber=15421> Please take care to refer to the correct standard code for the standard you're referencing :-) -- \ “You say I took the name in vain / I don't even know the name / | `\But if I did, well, really, what's it to you?” —Leonard Cohen, | _o__) _Hallelujah_ | Ben Finney -- https://mail.python.org/mailman/listinfo/python-list
Re: Calculating longitudinal acceleration, lateral acceleration and normal acceleration
On Sunday 24 January 2016 11:27, Robert James Liguori wrote: > Is there a python library to calculate longitudinal acceleration, lateral > acceleration and normal acceleration? Calculate acceleration of what? I think we need some more detail before we can give a sensible answer, but you could try numpy and scipy: http://www.numpy.org/ https://www.scipy.org/ or Sage: http://www.sagemath.org/ -- Steve -- https://mail.python.org/mailman/listinfo/python-list
Re: Calculating longitudinal acceleration, lateral acceleration and normal acceleration
Showing any bits of input would help us help you. Your question also seems too vague. Almost a "how to optimize". -- https://mail.python.org/mailman/listinfo/python-list
Re: Calculating longitudinal acceleration, lateral acceleration and normal acceleration
Second attempt. On Sunday 24 January 2016 11:27, Robert James Liguori wrote: > Is there a python library to calculate longitudinal acceleration, lateral > acceleration and normal acceleration? Calculate acceleration of what? I think we need some more detail before we can give a sensible answer, but you could try numpy and scipy: http://www.numpy.org/ https://www.scipy.org/ or Sage: http://www.sagemath.org/ -- Steve -- https://mail.python.org/mailman/listinfo/python-list
