Re: Behavior of auto in Enum and Flag.
On 04/02/2017 09:49 PM, Oren Ben-Kiki wrote:
The current behavior of `auto` is to pick a value which is one plus the
previous value.
Starting with 1 if no previous value exists.
It would probably be better if `auto` instead picked a value that is not
used by any named member (either the minimal unused value, or the minimal
higher than the previous value). That is, in this simple case:
class MyEnum(Enum):
FOO = 1
BAR = auto()
BAZ = 2
It would be far better for BAR to get the value 3 rather than today's value
2.
Possibly.
After all, `auto` is supposed to be used when:
"If the exact value is unimportant you may use auto instances and an
appropriate value will be chosen for you."
Choosing a value that conflicts with BAZ in above cases doesn't seem
"appropriate" for a value that is "unimportant".
MyEnum.BAR exists, and works. It just happens to be the same as MyEnum.BAZ -- which did not exist when the value for
BAR was chosen.
The docs also state "Care must be taken if you mix auto with other values."
- fair enough. But:
First, why require "care" if the code can take care of the issue for us?
Second, the docs don't go into further detail about what exactly to avoid.
In particular, the docs do not state that the automatic value will only
take into account the previous values, and will ignore following values.
Python code is executed top-down. First FOO, then BAR, then BAZ. It is not saved up and executed later in random
order. Or, put another way, the value was appropriate when it was chosen -- it is not the fault of auto() that the user
chose a conflicting value (hence why care should be taken).
However, this restriction is baked into the current implementation:
It is not possible to just override `_generate_next_value_` to skip past
named values which were not seen yet, because the implementation only
passes it the list of previous values.
In other words, the currently existing values, because the future values don't
exist yet.
I propose that:
1. The documentation will be more explicit about the way `auto` behaves in
the presence of following values.
I can do that.
2. The default behavior of `auto` would avoid generating a conflict with
following values.
I could do that, but I'm not convinced it's necessary, plus there would be backwards compatibility constraints at this
point.
3. Whether `auto` chooses (A) the minimal unused value higher than the
previous value, or (B) the minimal overall unused value, or (C) some other
strategy, would depend on the specific implementation.
This might work for you (untested):
def _generate_next_value_(name, start, count, previous_values):
if not count:
return start or 1
previous_values.sort()
last_value = previous_values[-1]
if last_value < 1000:
return 1001
else:
return last_value + 1
3. To allow for this, the implementation will include a
`_generate_auto_value_` which will take both the list of previous ("last")
values (including auto values) and also a second list of the following
("next") values (excluding auto values).
No, I'm not interested in doing that. I currently have that kind of code in aenum[1] for 2.7 compatibility, and it's a
nightmare to maintain.
--
~Ethan~
[1] https://pypi.python.org/pypi/aenum
Advanced Enumerations
NamedConstants
NamedTuple (metaclass based namedtuple -- no code eval)
plus a bunch of other goodies
--
https://mail.python.org/mailman/listinfo/python-list
Best practices interfacing to device with Python's asyncio and pyserial-asyncio
Hello, I have written a Python package to read from and write to a serial device that uses short telegrams to communicate with sensors and actuators. My classes include one to model the transceiver (it establishes the serial connection using serial.aio.create_serial_connection) and one for the telegram protocol based on asyncio.Protocol. I also have a bunch of classes for the different types of telegrams and devices. While this all works, the device model and the protocol don't look very pythonic to me. Are there any tutorials or examples demonstrating best practices of how to do something like this? Thanks, Malte -- https://mail.python.org/mailman/listinfo/python-list
Re: Behavior of auto in Enum and Flag.
On Mon, Apr 3, 2017 at 11:03 AM, Ethan Furman wrote:
> Python code is executed top-down. First FOO, then BAR, then BAZ. It is
> not saved up and executed later in random order. Or, put another way, the
> value was appropriate when it was chosen -- it is not the fault of auto()
> that the user chose a conflicting value (hence why care should be taken).
This is not to say that there's no possible workaround for this - the code
could pretty easily defer invocation of _generate_next_macro_ until after
the whole class was seen. It would still happen in order (since members are
an ordered dictionary these days).
So it is a matter of conflicting values - what would be more "Pythonic":
treating auto as executed immediately, or avoiding conflicts between auto
and explicit values.
> 1. The documentation will be more explicit about the way `auto` behaves in
> the presence of following value
> I can do that.
>
Barring changing the way auto works, that would be best ("explicit is
better than implicit" and all that ;-)
> 2. The default behavior of `auto` would avoid generating a conflict with
>> following values.
>>
>
> I could do that, but I'm not convinced it's necessary, plus there would be
> backwards compatibility constraints at this point.
"Necessity" depends on the judgement call above.
As for backward compatibility, the docs are pretty clear about "use auto
when you don't care about the value"... and Enum is pretty new, so there's
not _that_ much code that relies on "implementation specific" details.
*If* backward compatibility is an issue here, then the docs might as well
specify "previous value plus 1, or 1 if this is the first value" as the
"standard" behavior, and be done.
This has the advantage of being deterministic and explicit, so people would
be justified in relying on it. It would still have to be accompanied by
saying "auto() can only consider previous values, not following ones".
> This might work for you (untested):
>
> def _generate_next_value_(name, start, count, previous_values):
> if not count:
> return start or 1
> previous_values.sort()
> last_value = previous_values[-1]
> if last_value < 1000:
> return 1001
> else:
> return last_value + 1
This assumes no following enum values have values > 1000 (or some
predetermined constant), which doesn't work in my particular case, or in
the general case. But yes, this might solve the problem for some people.
> 3. To allow for this, the implementation will include a
>> `_generate_auto_value_` which will take both the list of previous ("last")
>> values (including auto values) and also a second list of the following
>> ("next") values (excluding auto values).
>>
>
> No, I'm not interested in doing that. I currently have that kind of code
> in aenum[1] for 2.7 compatibility, and it's a nightmare to maintain.
>
Understood. Another alternative would be to have something like
_generate_next_value_ex_ with the additional argument (similar to
__reduce_ex__), which isn't ideal either.
Assuming you buy into my "necessity" claim, that is...
Thanks,
Oren.
--
https://mail.python.org/mailman/listinfo/python-list
ANN: Python Meeting Düsseldorf - 05.04.2017
[This announcement is in German since it targets a local user group meeting in Düsseldorf, Germany] ANKÜNDIGUNG Python Meeting Düsseldorf http://pyddf.de/ Ein Treffen von Python Enthusiasten und Interessierten in ungezwungener Atmosphäre. Mittwoch, 05.04.2017, 18:00 Uhr Raum 1, 2.OG im Bürgerhaus Stadtteilzentrum Bilk Düsseldorfer Arcaden, Bachstr. 145, 40217 Düsseldorf Diese Nachricht ist auch online verfügbar: http://www.egenix.com/company/news/Python-Meeting-Duesseldorf-2017-04-05 NEUIGKEITEN * Bereits angemeldete Vorträge: Stefan Richthofer "pytypes" André Aulich "Python-Webanwendungen als native Desktop-Apps verteilen" Charlie Clark "Frankenstein — OO-Komposition statt Vererbung" Weitere Vorträge können gerne noch angemeldet werden: [email protected] * Startzeit und Ort: Wir treffen uns um 18:00 Uhr im Bürgerhaus in den Düsseldorfer Arcaden. Das Bürgerhaus teilt sich den Eingang mit dem Schwimmbad und befindet sich an der Seite der Tiefgarageneinfahrt der Düsseldorfer Arcaden. Über dem Eingang steht ein großes "Schwimm' in Bilk" Logo. Hinter der Tür direkt links zu den zwei Aufzügen, dann in den 2. Stock hochfahren. Der Eingang zum Raum 1 liegt direkt links, wenn man aus dem Aufzug kommt. Google Street View: http://bit.ly/11sCfiw EINLEITUNG Das Python Meeting Düsseldorf ist eine regelmäßige Veranstaltung in Düsseldorf, die sich an Python Begeisterte aus der Region wendet: * http://pyddf.de/ Einen guten Überblick über die Vorträge bietet unser YouTube-Kanal, auf dem wir die Vorträge nach den Meetings veröffentlichen: * http://www.youtube.com/pyddf/ Veranstaltet wird das Meeting von der eGenix.com GmbH, Langenfeld, in Zusammenarbeit mit Clark Consulting & Research, Düsseldorf: * http://www.egenix.com/ * http://www.clark-consulting.eu/ PROGRAMM Das Python Meeting Düsseldorf nutzt eine Mischung aus (Lightning) Talks und offener Diskussion. Vorträge können vorher angemeldet werden, oder auch spontan während des Treffens eingebracht werden. Ein Beamer mit XGA Auflösung steht zur Verfügung. (Lightning) Talk Anmeldung bitte formlos per EMail an [email protected] KOSTENBETEILIGUNG Das Python Meeting Düsseldorf wird von Python Nutzern für Python Nutzer veranstaltet. Um die Kosten zumindest teilweise zu refinanzieren, bitten wir die Teilnehmer um einen Beitrag in Höhe von EUR 10,00 inkl. 19% Mwst, Schüler und Studenten zahlen EUR 5,00 inkl. 19% Mwst. Wir möchten alle Teilnehmer bitten, den Betrag in bar mitzubringen. ANMELDUNG Da wir nur für ca. 20 Personen Sitzplätze haben, möchten wir bitten, sich per EMail anzumelden. Damit wird keine Verpflichtung eingegangen. Es erleichtert uns allerdings die Planung. Meeting Anmeldung bitte formlos per EMail an [email protected] WEITERE INFORMATIONEN Weitere Informationen finden Sie auf der Webseite des Meetings: http://pyddf.de/ Mit freundlichen Grüßen, -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Experts (#1, Apr 03 2017) >>> Python Projects, Coaching and Consulting ... http://www.egenix.com/ >>> Python Database Interfaces ... http://products.egenix.com/ >>> Plone/Zope Database Interfaces ... http://zope.egenix.com/ ::: We implement business ideas - efficiently in both time and costs ::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/ http://www.malemburg.com/ -- https://mail.python.org/mailman/listinfo/python-list
Re: Two variable dictionary comprehension
Gregory Ewing wrote: > > Part of being a good programmer is knowing how to track > down the information you need! > A very *large* part of it! :-) > Having said that, the index of the Python docs could be > improved a bit in this area -- currently it only mentions > "list" under "comprehension" (although the page it leads > to discusses the other types as well). > It's an area I tend to have trouble with, there are rather more 'lists' in Python than I find easy to remember all about. I'm sure if I was a full-time programmer in Python (I used to earn my living writing C) I'd have it all at my finger tips. Hoever, given that I'm just a 'recreational' programmer in Python, and I'm getting old and forgetful, I need something easy to refer to. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
SocketServer and Ctrl-C on Windows
I know I've seen this before, but for the life of me I can't find any reference.
If I write a simple web server using wsgiref, something like
from wsgiref.simple_server import make_server, demo_app
with make_server('', 8000, demo_app) as httpd:
print("Serving HTTP on port 8000...")
# Respond to requests until process is killed
httpd.serve_forever()
(from the docs), then run it on Windows, the server won't respond to Ctrl-C. I
know I've seen a solution that traps ctrl-C and shuts the server down
gracefully, but I can't for the life of me remember how to do it.
Can anyone point me to a way to do this? It's not crucial - I can use
Ctrl-Break to stop the server - but I'd like an option for a clean shutdown if
possible.
Thanks,
Paul
--
https://mail.python.org/mailman/listinfo/python-list
Re: Spam user
Il giorno Sat 01 Apr 2017 09:15:41p, *Mario R. Osorio* ha inviato su comp.lang.python il messaggio news:[email protected]. Vediamo cosa ha scritto: > I'm not in the business of starting an argument about best/worse > newsreader, Ammammata, but could you please recommend a few? > not my intention, I just think a dedicated news *program* is better than the google *web interface* and gives you more filter tools here I'm using xnews, but I have also mesnews configured (for stats) as well as pan for windows (for other uses) at home I use pan (on linux) on the tablet I use a chinese piece of software, can't remember the name sorry, but it's in the playstore -- /-\ /\/\ /\/\ /-\ /\/\ /\/\ /-\ T /-\ -=- -=- -=- -=- -=- -=- -=- -=- - -=- > http://www.bb2002.it :) < ... [ al lavoro ] ... -- https://mail.python.org/mailman/listinfo/python-list
Re: SocketServer and Ctrl-C on Windows
On Mon, Apr 3, 2017 at 9:08 AM, Paul Moore wrote:
> I know I've seen this before, but for the life of me I can't find any
> reference.
>
> If I write a simple web server using wsgiref, something like
>
> from wsgiref.simple_server import make_server, demo_app
>
> with make_server('', 8000, demo_app) as httpd:
> print("Serving HTTP on port 8000...")
>
> # Respond to requests until process is killed
> httpd.serve_forever()
>
> (from the docs), then run it on Windows, the server won't respond to Ctrl-C. I
> know I've seen a solution that traps ctrl-C and shuts the server down
> gracefully,
> but I can't for the life of me remember how to do it.
>
> Can anyone point me to a way to do this? It's not crucial - I can use
> Ctrl-Break to
> stop the server - but I'd like an option for a clean shutdown if possible.
It works for me when run from a command prompt in Windows 10.
serve_forever() uses select() with a timeout of 0.5s, so it doesn't
block the main thread.
--
https://mail.python.org/mailman/listinfo/python-list
Re: SocketServer and Ctrl-C on Windows
On Monday, 3 April 2017 13:23:11 UTC+1, eryk sun wrote: > It works for me when run from a command prompt in Windows 10. > serve_forever() uses select() with a timeout of 0.5s, so it doesn't > block the main thread. Odd. For me, it doesn't work (Windows 7, but I can't see why that would affect it). Having investigated a bit more, it seems like if I just start the server then hit Ctrl-C it shuts down fine. But if I start the server, then I navigate to http://localhost:8000 in my browser, and *then* hit Ctrl-C, the server just continues running. So I don't know if it's something to do with having handled a request? I have a vague recollection that it's to do with threading - maybe the exception being delivered to the wrong thread? Or a non-daemon thread being left active? Thanks for checking for me, though. Paul -- https://mail.python.org/mailman/listinfo/python-list
"pandas" pronunciation
I hear people say it like the plural of "panda", and others as "panduss". Is there a correct way? j -- https://mail.python.org/mailman/listinfo/python-list
Re: SocketServer and Ctrl-C on Windows
On Mon, Apr 3, 2017 at 12:34 PM, Paul Moore wrote: > On Monday, 3 April 2017 13:23:11 UTC+1, eryk sun wrote: >> It works for me when run from a command prompt in Windows 10. >> serve_forever() uses select() with a timeout of 0.5s, so it doesn't >> block the main thread. > > Odd. For me, it doesn't work (Windows 7, but I can't see why that would > affect it). > > Having investigated a bit more, it seems like if I just start the server then > hit Ctrl-C > it shuts down fine. But if I start the server, then I navigate to > http://localhost:8000 in > my browser, and *then* hit Ctrl-C, the server just continues running. It should service the request and return to the serve_forever() loop. Do you see a line logged for each request, like "[IP] - - [date] "GET ..."? -- https://mail.python.org/mailman/listinfo/python-list
Re: SocketServer and Ctrl-C on Windows
On Monday, 3 April 2017 14:00:18 UTC+1, eryk sun wrote: > It should service the request and return to the serve_forever() loop. > Do you see a line logged for each request, like "[IP] - - [date] "GET > ..."? Yes, I see that and the page is served. >py .\example.py Serving HTTP on port 8000... 127.0.0.1 - - [03/Apr/2017 13:25:34] "GET / HTTP/1.1" 200 5470 127.0.0.1 - - [03/Apr/2017 13:25:34] "GET /favicon.ico HTTP/1.1" 200 5438 But if I press Ctrl-C at this point, nothing happens. Even if I press Ctrl-C multiple times. Paul -- https://mail.python.org/mailman/listinfo/python-list
Re: Spam user
On 01/04/17 16:15, Mario R. Osorio wrote: I'm not in the business of starting an argument about best/worse newsreader, Ammammata, but could you please recommend a few? Mozilla Thunderbird works very well. Spam is close to nothing using this free nntp server: news.aioe.org Regards -- Cholo Lennon Bs.As. ARG -- https://mail.python.org/mailman/listinfo/python-list
EuroPython 2017: Launching early-bird sales tomorrow
We are starting ticket sales tomorrow and, as is tradition, we have allocated a number of tickets to be sold at very low rates - in fact you can save between 40-50% on these early-bird rates, compared to the regular ticket prices. Sales at the early-bird rate will open tomorrow, April 4th, at 10:00 CEST We will have 200 tickets available at the early-bird rate and they usually sell out within a few days, so be sure to get yours quickly. Regular ticket sales will then commence shortly after the early-bird phase. Please see our registration page for full details: https://ep2017.europython.eu/en/registration/buy-tickets/ Enjoy, -- EuroPython 2017 Team http://ep2017.europython.eu/ http://www.europython-society.org/ PS: Please forward or retweet to help us reach all interested parties: https://twitter.com/europython/status/848894062715437057 Thanks. -- https://mail.python.org/mailman/listinfo/python-list
Re: SocketServer and Ctrl-C on Windows
On Mon, Apr 3, 2017 at 1:20 PM, Paul Moore wrote:
> On Monday, 3 April 2017 14:00:18 UTC+1, eryk sun wrote:
>> It should service the request and return to the serve_forever() loop.
>> Do you see a line logged for each request, like "[IP] - - [date] "GET
>> ..."?
>
> Yes, I see that and the page is served.
>
>>py .\example.py
> Serving HTTP on port 8000...
> 127.0.0.1 - - [03/Apr/2017 13:25:34] "GET / HTTP/1.1" 200 5470
> 127.0.0.1 - - [03/Apr/2017 13:25:34] "GET /favicon.ico HTTP/1.1" 200 5438
>
> But if I press Ctrl-C at this point, nothing happens. Even if I press Ctrl-C
> multiple times.
Try adding a low-level console control handler that shuts down the
server. For example:
import sys
import ctypes
from wsgiref.simple_server import make_server, demo_app
kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
CTRL_C_EVENT = 0
with make_server('', 8000, demo_app) as httpd:
print("Serving HTTP on port 8000...")
@ctypes.WINFUNCTYPE(ctypes.c_int, ctypes.c_uint)
def ctrl_handler(dwCtrlType):
if dwCtrlType == CTRL_C_EVENT:
print("Ctrl+C shutdown", file=sys.stderr)
httpd.shutdown()
return False
if not kernel32.SetConsoleCtrlHandler(ctrl_handler, True):
raise ctypes.WinError(ctypes.get_last_error())
# Respond to requests until process is killed
httpd.serve_forever()
--
https://mail.python.org/mailman/listinfo/python-list
Re: SocketServer and Ctrl-C on Windows
On Monday, 3 April 2017 14:20:43 UTC+1, Paul Moore wrote: > On Monday, 3 April 2017 14:00:18 UTC+1, eryk sun wrote: > > It should service the request and return to the serve_forever() loop. > > Do you see a line logged for each request, like "[IP] - - [date] "GET > > ..."? > > Yes, I see that and the page is served. > > >py .\example.py > Serving HTTP on port 8000... > 127.0.0.1 - - [03/Apr/2017 13:25:34] "GET / HTTP/1.1" 200 5470 > 127.0.0.1 - - [03/Apr/2017 13:25:34] "GET /favicon.ico HTTP/1.1" 200 5438 > > But if I press Ctrl-C at this point, nothing happens. Even if I press Ctrl-C > multiple times. Hmm, looks like sometimes it works. I wonder if the browser is holding open the page request somehow. That seems unlikely as HTTP is supposed to be stateless. I guess this isn't worth worrying about. It seems like it's only an occasional problem, and I have a workaround (Ctrl-Break) anyway. Paul -- https://mail.python.org/mailman/listinfo/python-list
Re: SocketServer and Ctrl-C on Windows
On Mon, Apr 3, 2017 at 11:56 PM, Paul Moore wrote: > On Monday, 3 April 2017 14:20:43 UTC+1, Paul Moore wrote: >> On Monday, 3 April 2017 14:00:18 UTC+1, eryk sun wrote: >> > It should service the request and return to the serve_forever() loop. >> > Do you see a line logged for each request, like "[IP] - - [date] "GET >> > ..."? >> >> Yes, I see that and the page is served. >> >> >py .\example.py >> Serving HTTP on port 8000... >> 127.0.0.1 - - [03/Apr/2017 13:25:34] "GET / HTTP/1.1" 200 5470 >> 127.0.0.1 - - [03/Apr/2017 13:25:34] "GET /favicon.ico HTTP/1.1" 200 5438 >> >> But if I press Ctrl-C at this point, nothing happens. Even if I press Ctrl-C >> multiple times. > > Hmm, looks like sometimes it works. I wonder if the browser is holding open > the page request somehow. That seems unlikely as HTTP is supposed to be > stateless. > You're getting HTTP/1.1 requests. Maybe you need to send a "Connection: close" header to tell the browser to leave you be? ChrisA -- https://mail.python.org/mailman/listinfo/python-list
RE: Two variable dictionary comprehension
Gregory Ewing wrote, on April 02, 2017 11:35 PM > > Deborah Swanson wrote: > > > Oh, come on. That's a fairly obscure citation in the docs, one that > > would take a good deal of experience and time reading > through them to > > know was there, > > You seemed to know that there was something called a "dict > comprehension". Googling for "python 3 dict comprehension" > gives me a link to an example of one in the docs as the second result. > > The first result is a page in Dive Into Python containing > a section on dict comprehensions. > > Part of being a good programmer is knowing how to track > down the information you need! > > Having said that, the index of the Python docs could be > improved a bit in this area -- currently it only mentions > "list" under "comprehension" (although the page it leads to > discusses the other types as well). > > -- > Greg Despite my earlier words and protestations that I did look for two variable dict comprehensions, fairly diligently, I am taking what you said seriously. Obviously I shouldn't expect to find handy and recognizable entries in the index for everything I might want to find in the docs, and I plan to spend more time browsing around. Clearly there's more to be found in nooks, crannies and byways in the docs than you'll get to from the given pointers in the index. Maybe it would be worthwhile to scrape the whole mess and have it in searchable text form. Another thing Python would be the right tool for the job for. Regular updates as the docs are updated would be a good idea too. It's obvious that today's Google isn't up to it, although it occurs to me that I haven't tried Google's site search on python.org. I hope you won't be miffed though if I still come up empty handed and come back here to ask again. Deborah -- https://mail.python.org/mailman/listinfo/python-list
Re: SocketServer and Ctrl-C on Windows
On Monday, 3 April 2017 15:10:12 UTC+1, Chris Angelico wrote: > You're getting HTTP/1.1 requests. Maybe you need to send a > "Connection: close" header to tell the browser to leave you be? That sounds possible - I don't really know enough about HTTP to even know that was a thing, so I'm not surprised if I got it wrong. My test harness is using a Bottle server, which (apparently) actively gets in the way of setting this header, so I'll have to do a bit more work to confirm this, but it certainly seems like a good lead. Paul -- https://mail.python.org/mailman/listinfo/python-list
Improve Python + Influxdb import performance
Hello World
The following script is an extract from
https://github.com/RittmanMead/obi-metrics-agent/blob/master/obi-metrics-agent.py
<>
import calendar, time
import sys
import getopt
print '---'
# Check the arguments to this script are as expected.
# argv[0] is script name.
argLen = len(sys.argv)
if argLen -1 < 2:
print "ERROR: got ", argLen -1, " args, must be at least two."
print '$FMW_HOME/oracle_common/common/bin/wlst.sh obi-metrics-agent.py
[] []
[] [] [targetDB influx db>'
exit()
outputFormat='CSV'
url='t3://localhost:7001'
targetHost='localhost'
targetDB='obi'
targetPort='8086'
try:
wls_user = sys.argv[1]
wls_pw = sys.argv[2]
url = sys.argv[3]
outputFormat=sys.argv[4]
targetHost=sys.argv[5]
targetPort=sys.argv[6]
targetDB=sys.argv[7]
except:
print ''
print wls_user, wls_pw,url, outputFormat,targetHost,targetPort,targetDB
now_epoch = calendar.timegm(time.gmtime())*1000
if outputFormat=='InfluxDB':
import httplib
influx_msgs=''
connect(wls_user,wls_pw,url)
results = displayMetricTables('Oracle_BI*','dms_cProcessInfo')
for table in results:
tableName = table.get('Table')
rows = table.get('Rows')
rowCollection = rows.values()
iter = rowCollection.iterator()
while iter.hasNext():
row = iter.next()
rowType = row.getCompositeType()
keys = rowType.keySet()
keyIter = keys.iterator()
inst_name= row.get('Name').replace(' ','-')
try:
server= row.get('Servername').replace(' ','-').replace('/','_')
except:
try:
server= row.get('ServerName').replace(' ','-').replace('/','_')
except:
server='unknown'
try:
host= row.get('Host').replace(' ','-')
except:
host=''
while keyIter.hasNext():
columnName = keyIter.next()
value = row.get(columnName )
if columnName.find('.value')>0:
metric_name=columnName.replace('.value','')
if value is not None:
if value != 0:
if outputFormat=='InfluxDB':
influx_msg=
('%s,server=%s,host=%s,metric_group=%s,metric_instance=%s value=%s %s') %
(metric_name,server,host,tableName,inst_name, value,now_epoch*100)
influx_msgs+='\n%s' % influx_msg
conn = httplib.HTTPConnection('%s:%s' %
(targetHost,targetPort))
## TODO pretty sure should be urlencoding this ...
a=conn.request("POST", ("/write?db=%s" % targetDB),
influx_msg)
r=conn.getresponse()
<>
It currently takes about 3 minutes to execute completely and I was thinking of
a way to make it run faster
Data alignment (Influx line protocol) & data loading - done together takes up
most of the time
Influxdb is currently loading data at around 3 points/second
Any way to align the data separately, store it and load it as a batch?
I feel that would help improve performance
Please let me know if you have any pointers
I can send the data sheet if required
Thanks P
--
https://mail.python.org/mailman/listinfo/python-list
Obtain Ceritificate Information from Invalid or Self-Signed Certificate in Python
I'm working on a Python 2.7.13 (Win x64) script to verify SSL certificates, and alert for problems. Specifically, I'm looking to return the date the cert expires or did expire. However, I'm running into an issue where the script will return information only if the certificate is valid. If the certificate is invalid, I receive a CERTIFICATE_VERIFY_FAILED SSL error. Normally I would simply use a try/catch when the error is raised and just alert that the cert is invalid, but the issue here is that the I need the actual date the certificate expired, or in some cases the cert will be a self-signed cert, which will be acceptable UNLESS the cert is expired. I'm dealing with an organization that has thousands of certs, so adding every single self signed cert to the cert store locally won't be an option. Per https://docs.python.org/2/library/ssl.html I tried to use conn._https_verify_certificates(enable=False) to disable certificate validation, but get an error that the attribute _https_verify_certificates doesn't exist. Here is my code so far. I'm sure I'm missing something obvious. Surely Python can pull the SSL certificate without validating it, right? import socketimport ssl def ssl_expiry_datetime(hostname): ssl_date_fmt = r'%b %d %H:%M:%S %Y %Z' context = ssl.create_default_context() conn = context.wrap_socket( socket.socket(socket.AF_INET), server_hostname=hostname, ) # 3 second timeout because Lambda has runtime limitations conn.settimeout(3.0) #conn._https_verify_certificates(enable=False) conn.connect((hostname, 443)) ssl_info = conn.getpeercert() # parse the string from the certificate into a Python datetime object return ['notAfter'] myhost = 'www.google.com' print ssl_expiry_datetime(myhost) Thanks! Ken -- https://mail.python.org/mailman/listinfo/python-list
Re: Improve Python + Influxdb import performance
You can reuse connection, instead of creating for each request. (HTTP
keep-alive).
On Tue, Apr 4, 2017 at 1:11 AM, Prathamesh wrote:
> Hello World
>
> The following script is an extract from
>
> https://github.com/RittmanMead/obi-metrics-agent/blob/master/obi-metrics-agent.py
>
> <>
>
> import calendar, time
> import sys
> import getopt
>
> print '---'
>
> # Check the arguments to this script are as expected.
> # argv[0] is script name.
> argLen = len(sys.argv)
> if argLen -1 < 2:
> print "ERROR: got ", argLen -1, " args, must be at least two."
> print '$FMW_HOME/oracle_common/common/bin/wlst.sh obi-metrics-agent.py
> [] []
> [] [] [targetDB influx db>'
> exit()
>
> outputFormat='CSV'
> url='t3://localhost:7001'
> targetHost='localhost'
> targetDB='obi'
> targetPort='8086'
>
> try:
> wls_user = sys.argv[1]
> wls_pw = sys.argv[2]
> url = sys.argv[3]
> outputFormat=sys.argv[4]
> targetHost=sys.argv[5]
> targetPort=sys.argv[6]
> targetDB=sys.argv[7]
> except:
> print ''
>
> print wls_user, wls_pw,url, outputFormat,targetHost,targetPort,targetDB
>
> now_epoch = calendar.timegm(time.gmtime())*1000
>
> if outputFormat=='InfluxDB':
> import httplib
> influx_msgs=''
>
> connect(wls_user,wls_pw,url)
> results = displayMetricTables('Oracle_BI*','dms_cProcessInfo')
> for table in results:
> tableName = table.get('Table')
> rows = table.get('Rows')
> rowCollection = rows.values()
> iter = rowCollection.iterator()
> while iter.hasNext():
> row = iter.next()
> rowType = row.getCompositeType()
> keys = rowType.keySet()
> keyIter = keys.iterator()
> inst_name= row.get('Name').replace(' ','-')
> try:
> server= row.get('Servername').replace(' ','-').replace('/','_')
> except:
> try:
> server= row.get('ServerName').replace('
> ','-').replace('/','_')
> except:
> server='unknown'
> try:
> host= row.get('Host').replace(' ','-')
> except:
> host=''
> while keyIter.hasNext():
> columnName = keyIter.next()
> value = row.get(columnName )
> if columnName.find('.value')>0:
> metric_name=columnName.replace('.value','')
> if value is not None:
> if value != 0:
> if outputFormat=='InfluxDB':
> influx_msg=
> ('%s,server=%s,host=%s,metric_group=%s,metric_instance=%s value=%s %s') %
> (metric_name,server,host,tableName,inst_name, value,now_epoch*100)
> influx_msgs+='\n%s' % influx_msg
> conn = httplib.HTTPConnection('%s:%s' %
> (targetHost,targetPort))
> ## TODO pretty sure should be urlencoding this ...
> a=conn.request("POST", ("/write?db=%s" %
> targetDB), influx_msg)
> r=conn.getresponse()
>
> <>
>
> It currently takes about 3 minutes to execute completely and I was thinking
> of a way to make it run faster
>
> Data alignment (Influx line protocol) & data loading - done together takes up
> most of the time
>
> Influxdb is currently loading data at around 3 points/second
>
> Any way to align the data separately, store it and load it as a batch?
>
> I feel that would help improve performance
>
> Please let me know if you have any pointers
> I can send the data sheet if required
>
> Thanks P
> --
> https://mail.python.org/mailman/listinfo/python-list
--
https://mail.python.org/mailman/listinfo/python-list
Re: SocketServer and Ctrl-C on Windows
On Tue, Apr 4, 2017 at 12:34 AM, Paul Moore wrote: > On Monday, 3 April 2017 15:10:12 UTC+1, Chris Angelico wrote: >> You're getting HTTP/1.1 requests. Maybe you need to send a >> "Connection: close" header to tell the browser to leave you be? > > That sounds possible - I don't really know enough about HTTP to even know > that was a thing, so I'm not surprised if I got it wrong. > > My test harness is using a Bottle server, which (apparently) actively gets in > the way of setting this header, so I'll have to do a bit more work to confirm > this, but it certainly seems like a good lead. An alternative way to diagnose this would be to see if you still have an established socket connection. I'm not sure how you do that on Windows, but it'd be a good thing to check. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Behavior of auto in Enum and Flag.
On 04/03/2017 01:53 AM, Oren Ben-Kiki wrote:
On Mon, Apr 3, 2017 at 11:03 AM, Ethan Furman wrote:
Python code is executed top-down. First FOO, then BAR, then BAZ. It is not
saved up and executed later in random
order. Or, put another way, the value was appropriate when it was chosen -- it
is not the fault of auto() that the
user chose a conflicting value (hence why care should be taken).
This is not to say that there's no possible workaround for this - the code
could pretty easily defer invocation of
_generate_next_macro_ until after the whole class was seen. It would still
happen in order (since members are an ordered
dictionary these days).
Possible, yes. Easy, no.
So it is a matter of conflicting values - what would be more "Pythonic":
treating auto as executed immediately, or
avoiding conflicts between auto and explicit values.
Since most things execute immediately, that is the pattern I chose.
1. The documentation will be more explicit about the way `auto` behaves in the
presence of following value
I can do that.
Barring changing the way auto works, that would be best ("explicit is better than
implicit" and all that ;-)
As for backward compatibility, the docs are pretty clear about "use auto when you
don't care about the value"... and
Enum is pretty new, so there's not _that_ much code that relies on "implementation
specific" details.
Fair point.
*If* backward compatibility is an issue here, then the docs might as well specify
"previous value plus 1, or 1 if this
is the first value" as the "standard" behavior, and be done.
Actually, (Int)Flag uses the next power of two (not already seen) -- so it isn't as easy
as "last value + 1".
This has the advantage of being deterministic and explicit, so people would be
justified in relying on it. It would
still have to be accompanied by saying "auto() can only consider previous values,
not following ones".
Something to that effect sounds good.
This might work for you (untested):
def _generate_next_value_(name, start, count, previous_values):
if not count:
return start or 1
previous_values.sort()
last_value = previous_values[-1]
if last_value < 1000:
return 1001
else:
return last_value + 1
This assumes no following enum values have values > 1000 (or some predetermined
constant), which doesn't work in my
particular case, or in the general case. But yes, this might solve the problem
for some people.
General code isn't going to "do what I need" 100% of the time for 100% of the people. That's why I made the
_generate_next_value_ method user over-ridable
3. To allow for this, the implementation will include a
`_generate_auto_value_` which will take both the list of previous ("last")
values (including auto values) and also a second list of the following
("next") values (excluding auto values).
No, I'm not interested in doing that. I currently have that kind of code in
aenum[1] for 2.7 compatibility, and
it's a nightmare to maintain.
Understood. Another alternative would be to have something like
_generate_next_value_ex_ with the additional argument
(similar to __reduce_ex__), which isn't ideal either.
It's a lot more work than just making another method.
Assuming you buy into my "necessity" claim, that is...
It this point I do not. If you can give us an example Enum and why it's necessary to be built like that I might be
swayed -- although the odds are good that the change will go into aenum instead (it's already a mess with the 2.7
compatibility code...).
Thanks,
You're welcome.
--
~Ethan~
--
https://mail.python.org/mailman/listinfo/python-list
Re: Behavior of auto in Enum and Flag.
On Tue, Apr 4, 2017 at 2:29 AM, Ethan Furman wrote: > It this point I do not. If you can give us an example Enum and why it's > necessary to be built like that I might be swayed -- although the odds are > good that the change will go into aenum instead (it's already a mess with > the 2.7 compatibility code...). Here's a counter-example that supports the current behaviour: >>> from enum import IntFlag, auto >>> class Spam(IntFlag): ... FOO = auto() ... BAR = auto() ... FOOBAR = FOO | BAR ... SPAM = auto() ... HAM = auto() ... SPAMHAM = SPAM | HAM ... >>> list(Spam) [, , , , , ] It makes perfect sense to define combined flags in terms of the (previously-defined) individual flags. If auto() has to be deferred until the class is fully defined, this would be a lot harder. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Behavior of auto in Enum and Flag.
On Mon, Apr 3, 2017 at 7:43 PM, Chris Angelico wrote: > Here's a counter-example that supports the current behaviour: > > >>> from enum import IntFlag, auto > >>> class Spam(IntFlag): > ... FOO = auto() > ... BAR = auto() > ... FOOBAR = FOO | BAR > ... SPAM = auto() > ... HAM = auto() > ... SPAMHAM = SPAM | HAM > ... > Ugh, good point - I didn't consider that use case, I see how it would be nasty to implement. I guess just improving the documentation is called for, then... Thanks, Oren. -- https://mail.python.org/mailman/listinfo/python-list
Re: Improve Python + Influxdb import performance
On Monday, April 3, 2017 at 9:52:38 PM UTC+5:30, INADA Naoki wrote:
> You can reuse connection, instead of creating for each request. (HTTP
> keep-alive).
>
> On Tue, Apr 4, 2017 at 1:11 AM, Prathamesh
> wrote:
> > Hello World
> >
> > The following script is an extract from
> >
> > https://github.com/RittmanMead/obi-metrics-agent/blob/master/obi-metrics-agent.py
> >
> > <>
> >
> > import calendar, time
> > import sys
> > import getopt
> >
> > print '---'
> >
> > # Check the arguments to this script are as expected.
> > # argv[0] is script name.
> > argLen = len(sys.argv)
> > if argLen -1 < 2:
> > print "ERROR: got ", argLen -1, " args, must be at least two."
> > print '$FMW_HOME/oracle_common/common/bin/wlst.sh obi-metrics-agent.py
> > [] []
> > [] [] [targetDB influx db>'
> > exit()
> >
> > outputFormat='CSV'
> > url='t3://localhost:7001'
> > targetHost='localhost'
> > targetDB='obi'
> > targetPort='8086'
> >
> > try:
> > wls_user = sys.argv[1]
> > wls_pw = sys.argv[2]
> > url = sys.argv[3]
> > outputFormat=sys.argv[4]
> > targetHost=sys.argv[5]
> > targetPort=sys.argv[6]
> > targetDB=sys.argv[7]
> > except:
> > print ''
> >
> > print wls_user, wls_pw,url, outputFormat,targetHost,targetPort,targetDB
> >
> > now_epoch = calendar.timegm(time.gmtime())*1000
> >
> > if outputFormat=='InfluxDB':
> > import httplib
> > influx_msgs=''
> >
> > connect(wls_user,wls_pw,url)
> > results = displayMetricTables('Oracle_BI*','dms_cProcessInfo')
> > for table in results:
> > tableName = table.get('Table')
> > rows = table.get('Rows')
> > rowCollection = rows.values()
> > iter = rowCollection.iterator()
> > while iter.hasNext():
> > row = iter.next()
> > rowType = row.getCompositeType()
> > keys = rowType.keySet()
> > keyIter = keys.iterator()
> > inst_name= row.get('Name').replace(' ','-')
> > try:
> > server= row.get('Servername').replace(' ','-').replace('/','_')
> > except:
> > try:
> > server= row.get('ServerName').replace('
> > ','-').replace('/','_')
> > except:
> > server='unknown'
> > try:
> > host= row.get('Host').replace(' ','-')
> > except:
> > host=''
> > while keyIter.hasNext():
> > columnName = keyIter.next()
> > value = row.get(columnName )
> > if columnName.find('.value')>0:
> > metric_name=columnName.replace('.value','')
> > if value is not None:
> > if value != 0:
> > if outputFormat=='InfluxDB':
> > influx_msg=
> > ('%s,server=%s,host=%s,metric_group=%s,metric_instance=%s value=%s %s') %
> > (metric_name,server,host,tableName,inst_name, value,now_epoch*100)
> > influx_msgs+='\n%s' % influx_msg
> > conn = httplib.HTTPConnection('%s:%s' %
> > (targetHost,targetPort))
> > ## TODO pretty sure should be urlencoding this
> > ...
> > a=conn.request("POST", ("/write?db=%s" %
> > targetDB), influx_msg)
> > r=conn.getresponse()
> >
> > <>
> >
> > It currently takes about 3 minutes to execute completely and I was thinking
> > of a way to make it run faster
> >
> > Data alignment (Influx line protocol) & data loading - done together takes
> > up most of the time
> >
> > Influxdb is currently loading data at around 3 points/second
> >
> > Any way to align the data separately, store it and load it as a batch?
> >
> > I feel that would help improve performance
> >
> > Please let me know if you have any pointers
> > I can send the data sheet if required
> >
> > Thanks P
> > --
> > https://mail.python.org/mailman/listinfo/python-list
How do I do that?
--
https://mail.python.org/mailman/listinfo/python-list
RE: Two variable dictionary comprehension
Dennis Lee Bieber wrote, on April 03, 2017 9:35 AM
>
> On Mon, 3 Apr 2017 07:30:40 -0700, "Deborah Swanson"
> declaimed the following:
>
> >
> >Clearly there's more to be found in nooks, crannies and
> byways in the
> >docs than you'll get to from the given pointers in the
> index. Maybe it
> >would be worthwhile to scrape the whole mess and have it in
> searchable
> >text form. Another thing Python would be the right tool for the job
> >for. Regular updates as the docs are updated would be a good
> idea too.
> >It's obvious that today's Google isn't up to it, although it
> occurs to
> >me that I haven't tried Google's site search on python.org.
> >
> On Windows, the (at least, for ActiveState releases)
> documentation is available in Windows Help format -- though
> I'll admit the "free text search" leaves a lot to be desired...
>
> "dict comprehension" didn't find anything obvious;
> "dictionary comprehension" brought up PEP 274 (note: I still
> use 2.7 as main version).
>
> -=-=-=-=-=-
> Semantics
> The semantics of dict comprehensions can actually be demonstrated
> in stock Python 2.2, by passing a list comprehension to the
> builtin dictionary constructor:
>
> >>> dict([(i, chr(65+i)) for i in range(4)])
>
> is semantically equivalent to
>
> >>> {i : chr(65+i) for i in range(4)}
>
> The dictionary constructor approach has two dictinct disadvantages
> from the proposed syntax though. First, it isn't as legible as a
> dict comprehension. Second, it forces the programmer to create an
> in-core list object first, which could be expensive.
>
> -=-=-=-=-=-
> --
> Wulfraed Dennis Lee Bieber AF6VN
> [email protected]://wlfraed.home.netcom.com/
It would be interesting to play around with different list
comprehensions than the one they've shown.
I'll admit that both dictionaries and comprehensions are still a little
bit fuzzy to me, especially when I get away from the common usages. This
could be a good exercise to clarify some of the fuzzy areas.
Deborah
--
https://mail.python.org/mailman/listinfo/python-list
Re: Two variable dictionary comprehension
On 04/03/2017 10:27 AM, Deborah Swanson wrote:
Dennis Lee Bieber wrote, on April 03, 2017 9:35 AM
On Mon, 3 Apr 2017 07:30:40 -0700, "Deborah Swanson"
declaimed the following:
Clearly there's more to be found in nooks, crannies and
byways in the
docs than you'll get to from the given pointers in the
index. Maybe it
would be worthwhile to scrape the whole mess and have it in
searchable
text form. Another thing Python would be the right tool for the job
for. Regular updates as the docs are updated would be a good
idea too.
It's obvious that today's Google isn't up to it, although it
occurs to
me that I haven't tried Google's site search on python.org.
On Windows, the (at least, for ActiveState releases)
documentation is available in Windows Help format -- though
I'll admit the "free text search" leaves a lot to be desired...
"dict comprehension" didn't find anything obvious;
"dictionary comprehension" brought up PEP 274 (note: I still
use 2.7 as main version).
-=-=-=-=-=-
Semantics
The semantics of dict comprehensions can actually be demonstrated
in stock Python 2.2, by passing a list comprehension to the
builtin dictionary constructor:
>>> dict([(i, chr(65+i)) for i in range(4)])
is semantically equivalent to
>>> {i : chr(65+i) for i in range(4)}
The dictionary constructor approach has two dictinct disadvantages
from the proposed syntax though. First, it isn't as legible as a
dict comprehension. Second, it forces the programmer to create an
in-core list object first, which could be expensive.
-=-=-=-=-=-
--
Wulfraed Dennis Lee Bieber AF6VN
[email protected]://wlfraed.home.netcom.com/
It would be interesting to play around with different list
comprehensions than the one they've shown.
I'll admit that both dictionaries and comprehensions are still a little
bit fuzzy to me, especially when I get away from the common usages. This
could be a good exercise to clarify some of the fuzzy areas.
Deborah
And don't forget
dict_comp : { k:v for (k,v) in kv_provider }
set_comp : { item for item in item_provider }
set_of_tuples_comp : { (k,v) for (k,v) in kv_provider }
Just to make things more complicated.
--
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order. See above to fix.
--
https://mail.python.org/mailman/listinfo/python-list
OFF-TOPIC Bigotry on the list [was Re: Text-mode apps (Was :Who are the "spacists"?)]
On Mon, 3 Apr 2017 08:31 am, Terry Reedy wrote: [...] > I refrained because it would be off-topic and a diversion from my point: > all bigotry is inappropriate here on this list, Indeed it is not appropriate. But calling out bigotry is not itself bigotry. I hope you agree with that. If not, that leaves you with some deeply Unfortunate Implications. Suppose you are right, and merely calling out "Ranting Rick" for his bigoted attitude makes me an anti-American bigot. Then by *you* calling *me* out for bigotry, that makes you equally a bigot. And so the only way to avoid being a bigot is... to do nothing in the face of bigotry. I didn't see you calling out Rick for his prejudice against those who aren't American, his absurd belief that "most" people are satisfied with ASCII, or his claim that the rest of the world don't need file names in their native languages because the "majority" (his word) of them are illiterate. Do you think those attitudes should be left unchallenged? (In fairness, Rick doesn't say that the *entire* rest of the world is mostly illiterate. He only includes China, India, Russia, Japan, Indonesia, Arab-speakers, and Israel. So a bit less than half the world.) > including anti-Americanism. When have I demonstrated prejudice against Americans merely for being American? I stand by my comment to Rick. It is *his behaviour and attitude* which makes him an example of the stereotype, not the fact that he is an American. Of course all people are unique, and all stereotypes are incomplete. People are not their stereotype, and I'm sure that Rick is a wonderful and unique person in his own way. Perhaps he loves animals and donates his time to working in shelters for the homeless. But nevertheless people do behave in stereotypical ways, and we've seen Rick do exactly that. Insensitive to cultural differences, dismissive of those who aren't American, ignorant of everything outside of the borders of the US, and arrogantly certain that the American way is not just the best way but the only way. And *wilfully* so. This is not just an isolated incident from Rick, a mere momentary lapse. He has been expressing these sorts of attitudes for years. > (I actually think raw bigoty is inappropriate everywhere. While there > are places to discuss the relative average beauty and niceness of > various groups of people, this is not one of them. Even comparisons of > computer languages is not the main focus of this list.) I'm not discussing the "relative average beauty and niceness of various groups of people". That's absurd. I'm discussing the behaviour and attitudes demonstrated by one specific individual. [...] > Yesterday, Chris Angelico, I believe it was, asked someone to stop > including anti-Jew messages in posts here. So I asked the same of you > regarding your anti-American expression. Can you really not tell the difference between criticism of an individual American for the views he expresses, and a blanket attack on all Americans merely for being American? >> Not all Americans, perhaps not even a majority or a plurality, are Ugly >> Americans, but there are enough of them to screw it up for everyone else. > > Shall we discuss Ugly Australians, or whatever the appropriate epithet > would be? If one of them turns up and starts expressing those views, absolutely. >> It's an ugly stereotype, not because it is a stereotype, but because it > > embodies a set of ugly attitudes and behaviours. > > I am sure I have seen similar things written by various other people > justifying their prejudices. As have I. Are you judging *me* by *their* behaviour? -- Steve “Cheer up,” they said, “things could be worse.” So I cheered up, and sure enough, things got worse. -- https://mail.python.org/mailman/listinfo/python-list
RE: Two variable dictionary comprehension
Rob Gaddi wrote, on April 03, 2017 10:38 AM
>
> On 04/03/2017 10:27 AM, Deborah Swanson wrote:
> > Dennis Lee Bieber wrote, on April 03, 2017 9:35 AM
> >>
> >> On Mon, 3 Apr 2017 07:30:40 -0700, "Deborah Swanson"
> >> declaimed the following:
> >>
> >>>
> >>> Clearly there's more to be found in nooks, crannies and byways in
the
> >>> docs than you'll get to from the given pointers in the index.
Maybe it
> >>> would be worthwhile to scrape the whole mess and have it in
searchable
> >>> text form. Another thing Python would be the right tool for the
job
> >>> for. Regular updates as the docs are updated would be a good idea
too.
> >>> It's obvious that today's Google isn't up to it, although it
occurs to
> >>> me that I haven't tried Google's site search on python.org.
> >>>
> >>On Windows, the (at least, for ActiveState releases)
documentation
> >> is available in Windows Help format -- though I'll admit the "free
> >> text search" leaves a lot to be desired...
> >>
> >>"dict comprehension" didn't find anything obvious; "dictionary
> >> comprehension" brought up PEP 274 (note: I still use 2.7 as main
> >> version).
> >>
> >> -=-=-=-=-=-
> >> Semantics
> >> The semantics of dict comprehensions can actually be
demonstrated
> >> in stock Python 2.2, by passing a list comprehension to the
> >> builtin dictionary constructor:
> >>
> >> >>> dict([(i, chr(65+i)) for i in range(4)])
> >>
> >> is semantically equivalent to
> >>
> >> >>> {i : chr(65+i) for i in range(4)}
> >>
> >> The dictionary constructor approach has two dictinct
disadvantages
> >> from the proposed syntax though. First, it isn't as legible as
a
> >> dict comprehension. Second, it forces the programmer to create
an
> >> in-core list object first, which could be expensive.
> >>
> >> -=-=-=-=-=-
> >> --
> >>Wulfraed Dennis Lee Bieber AF6VN
> >> [email protected]://wlfraed.home.netcom.com/
> >
> > It would be interesting to play around with different list
> > comprehensions than the one they've shown.
> >
> > I'll admit that both dictionaries and comprehensions are still a
> > little bit fuzzy to me, especially when I get away from the common
> > usages. This could be a good exercise to clarify some of the fuzzy
> > areas.
> >
> > Deborah
> >
>
> And don't forget
>dict_comp : { k:v for (k,v) in kv_provider }
>set_comp : { item for item in item_provider }
>set_of_tuples_comp : { (k,v) for (k,v) in kv_provider }
>
> Just to make things more complicated.
>
> --
> Rob Gaddi, Highland Technology -- www.highlandtechnology.com
> Email address domain is currently out of order. See above to fix.
Added to my list of comprehension types, thank you.
I'm thinking it would also be good to include the PEPs in my searchable
Python reference. Just making the collection and browsing through it
would be highly instructive.
But, if Larry Page and Sergey Brin could tinker around in their dorm
rooms (or wherever they lived then) and they made the first Google (the
first search engine?) to boolean search the World Wide Web, it shouldn't
be so awfully hard to make a collection of Python docs that's boolean
searchable.
I haven't seen a search method for text that comes anywhere near boolean
search's completeness in returning results (though I'm happy to take
suggestions).
Deborah
--
https://mail.python.org/mailman/listinfo/python-list
Request Help With pkexec
Python 3.4.2 Tkinter 8.6 GCC 4.9.1 on Linux I am working on a gui program using Tkinter. The program will have a feature to restart as root. I am testing different gui front-ends from a terminal to raise privileges and I want to be able to use as many as possible for obvious reasons. Gksu, kdesudo and su-to-root all work perfectly. However, I have a problem with pkexec. Here is the command I am using from a terminal: $ pkexec python3 /home/user/Python/linfo-tk/linfo-tk.py I get this error: Traceback (most recent call last): File "/home/user/Python/linfo-tk/linfo-tk.py", line 455, in root = tk.Tk() File "/usr/lib/python3.4/tkinter/__init__.py", line 1854, in __init__ self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use) _tkinter.TclError: no display name and no $DISPLAY environment variable The display environment variable is set: $ echo $DISPLAY :0.0 And the program displays and works perfectly in every other way. I would appreciate any insight. -- GNU/Linux user #557453 May the Source be with you. -- https://mail.python.org/mailman/listinfo/python-list
Re: Two variable dictionary comprehension
On Mon, Apr 3, 2017 at 10:30 AM, Deborah Swanson wrote: > Regular updates as the docs are updated would be a good idea too. It's > obvious that today's Google isn't up to it, although it occurs to me > that I haven't tried Google's site search on python.org. So, when you search google for the phrase "dict comprehension" or "dictionary comprehension", nothing useful comes up for you? When I search either of those phrases, I get lots of useful results, all of which spell out how to do what you were originally asking about. I know Google search results are skewed by past usage, but I'm surprised that you didn't find anything useful in the first couple of search results. When I do a search for 'dict comprehension' I get a boxed result linking to PEP 274 as the first hit, then two Stack Overflow questions, both of which demonstrate how to do dictionary comprehensions. Following that is another link to PEP 274, a link to the Python docs on data structures (which does talk about dict comprehensions, but it's way down on the page), and then links to a bunch of tutorials. If you had to judge based on my search results, Google does a fine job of answering python questions, at least when you already know the key phrase to look for. -- Jerry -- https://mail.python.org/mailman/listinfo/python-list
Re: Two variable dictionary comprehension
I was a bit surprised when I looked at the language reference for 3.6.x. I expected there'd be a direct link to comprehensions, but there's not. You have to know what you're looking for: 6.2.5: List Displays 6.2.6: Set Displays 6.2.7: Dictionary Displays And, then, click on the appropriate element of the sub grammar to find the appropriate syntax. So, it took me about 30 seconds to find the appropriate grammars, when I expected it'd only take about 5 seconds, since I'm very familiar with the python docs & how the grammar documentation is laid out. I can fully understand how someone less familiar with the documentation might have a harder time finding the grammar than I did. FWIW, If one was completely new to Python, even knowing the syntax is known as a "comprehension" might be unknown. I certainly didn't know what a comprehension was when I was learning Python. A coworker showed me, some 13 years ago. Regards, Nate On Mon, Apr 3, 2017 at 3:47 PM, Jerry Hill wrote: > On Mon, Apr 3, 2017 at 10:30 AM, Deborah Swanson > wrote: > > Regular updates as the docs are updated would be a good idea too. It's > > obvious that today's Google isn't up to it, although it occurs to me > > that I haven't tried Google's site search on python.org. > > So, when you search google for the phrase "dict comprehension" or > "dictionary comprehension", nothing useful comes up for you? When I > search either of those phrases, I get lots of useful results, all of > which spell out how to do what you were originally asking about. I > know Google search results are skewed by past usage, but I'm surprised > that you didn't find anything useful in the first couple of search > results. > > When I do a search for 'dict comprehension' I get a boxed result > linking to PEP 274 as the first hit, then two Stack Overflow > questions, both of which demonstrate how to do dictionary > comprehensions. Following that is another link to PEP 274, a link to > the Python docs on data structures (which does talk about dict > comprehensions, but it's way down on the page), and then links to a > bunch of tutorials. If you had to judge based on my search results, > Google does a fine job of answering python questions, at least when > you already know the key phrase to look for. > > -- > Jerry > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
Which directory should requests and openpyxl modules be installed to?
Hi, I am a newbie to Python. I am using Python 2.7.12. I want to install the modules of requests and openpyxl using pip. In the Internet, people only said pip install requests, but they do not say in which directory. I only found one that said install in the script directory within Python. Well, that didn't work. How and which directory in the command line to type pip install requests? Thanks! Pauline -- https://mail.python.org/mailman/listinfo/python-list
Re: Which directory should requests and openpyxl modules be installed to?
Hi Pauline, It depends largely on whether you want to (and have sufficient permissions) to install for all users or just yourself. If, on *nix, you're installing site-wide (for all users), typically you'd do: "sudo pip install " (for python 2) or "sudo pip3 install " (for python 3). If you're installing it for yourself, you may need to add the --prefix= option. Typically, would be somewhere under your home directory. This can be problematic, as you may need to edit the PYTHONPATH (and possibly LD_LIBRARY_PATH) to point to appropriate directories. In this case, you're probably better of setting up a virtual environment. See: http://python-guide-pt-br.readthedocs.io/en/latest/dev/virtualenvs/. With virtualenvs, you can setup a per user/project python configuration. If you're on Windows, you should be able to "py pip install ", and that will install it for you. If you run under and adminstrator cmd prompt & python has been installed for all users, I *think* the module will be available for everyone. Hope this helps. Regards, Nate On Mon, Apr 3, 2017 at 4:02 PM, Pauline wrote: > Hi, > > I am a newbie to Python. I am using Python 2.7.12. I want to install > the modules of requests and openpyxl using pip. In the Internet, people > only said pip install requests, but they do not say in which directory. I > only found one that said install in the script directory within Python. > Well, that didn't work. > > How and which directory in the command line to type pip install requests? > > Thanks! > Pauline > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
Re: Which directory should requests and openpyxl modules be installed to?
On Monday, April 3, 2017 at 2:02:30 PM UTC-7, Pauline wrote: > Hi, > > I am a newbie to Python. I am using Python 2.7.12. I want to install the > modules of requests and openpyxl using pip. In the Internet, people only > said pip install requests, but they do not say in which directory. I only > found one that said install in the script directory within Python. Well, that > didn't work. > > How and which directory in the command line to type pip install requests? > > Thanks! > Pauline Hi Nate, Thank you for your prompt reply. I am just installing it on my computer with administrative privileges. I think I can go into the command line, but then I do not know which directory to go to type: pip install requests If I have installed python 2.7.12 in the Program Files directory, and then there are many directories within the Python 2.7.12 directory, then which directory should I go to install the modules? I think my situation is indeed very simple. Pauline -- https://mail.python.org/mailman/listinfo/python-list
RE: Two variable dictionary comprehension
Jerry Hill wrote, on April 03, 2017 1:48 PM > > On Mon, Apr 3, 2017 at 10:30 AM, Deborah Swanson > wrote: > > Regular updates as the docs are updated would be a good > idea too. It's > > obvious that today's Google isn't up to it, although it > occurs to me > > that I haven't tried Google's site search on python.org. > > So, when you search google for the phrase "dict > comprehension" or "dictionary comprehension", nothing useful > comes up for you? When I search either of those phrases, I > get lots of useful results, all of which spell out how to do > what you were originally asking about. I know Google search > results are skewed by past usage, but I'm surprised that you > didn't find anything useful in the first couple of search results. > > When I do a search for 'dict comprehension' I get a boxed > result linking to PEP 274 as the first hit, then two Stack > Overflow questions, both of which demonstrate how to do > dictionary comprehensions. Following that is another link to > PEP 274, a link to the Python docs on data structures (which > does talk about dict comprehensions, but it's way down on the > page), and then links to a bunch of tutorials. If you had to > judge based on my search results, Google does a fine job of > answering python questions, at least when you already know > the key phrase to look for. > > -- > Jerry Ah, but did you actually try to use the proposed solutions on the two stackoverflow pages? It's been several weeks now, but I did, and neither of those two examples fit my situation, which is why I ended up writing my own, and unsatisfactorily at that. I'm sorry you think the current edition of Google does such a fine job. Has it really been that many years ago that the results Google returned from a properly formatted boolean search were really useful? I'd imagine that the old Google would have returned a good 10 pages or more (probably a lot more) of urls containing the phrase "dict comprehension" or "dictionary comprehension". in which you'd find a rich variety of specific situations to glean through. (You wouldn't have needed to include "python" in the search phrase, since no other programming language that I know of, or other English usage for that matter, has dict comprehensions.) Nowadays Google just comes up with a mere handful of sorta appropriate urls, and rarely do I find exactly what I'm looking for. And usually there's nothing related to all your search terms after a page or two of results. You used to be able to keep sifting through pages of results after the bulk of urls fitting your criteria had passed, and still find useful things to look at, sometimes at page 500 or even much, much farther down the list. It really paid to comb through them all, especially if you didn't find exactly what you wanted in the early batch. But giving users the choice among tens or hundreds of similar pages (fewer if you specify 100 results per page) doesn't give Google as much grist for the advertising mill to pump out at the users, hence the present day useless mess. Deborah -- https://mail.python.org/mailman/listinfo/python-list
Re: Which directory should requests and openpyxl modules be installed to?
If you've installed into Program Files, then you're on Windows, and you've installed for all users. Start a command prompt by right-clicking on the start icon, then selecting "Command Prompt (Admin)". This should work on Windows 8.x and Windows 10. Windows 7, you may need to navigate through Programs to Windows System\Command Prompt (path might be different - may be under Windows Accessories), then right-clicking on Command Prompt (Admin). >From that prompt, you should be able to run "py pip install " and install it site-wide (for all local users) On Mon, Apr 3, 2017 at 4:36 PM, wrote: > On Monday, April 3, 2017 at 2:02:30 PM UTC-7, Pauline wrote: > > Hi, > > > > I am a newbie to Python. I am using Python 2.7.12. I want to install > the modules of requests and openpyxl using pip. In the Internet, people > only said pip install requests, but they do not say in which directory. I > only found one that said install in the script directory within Python. > Well, that didn't work. > > > > How and which directory in the command line to type pip install > requests? > > > > Thanks! > > Pauline > > Hi Nate, > > Thank you for your prompt reply. I am just installing it on my computer > with administrative privileges. I think I can go into the command line, but > then I do not know which directory to go to type: pip install requests > > If I have installed python 2.7.12 in the Program Files directory, and > then there are many directories within the Python 2.7.12 directory, then > which directory should I go to install the modules? > > I think my situation is indeed very simple. > > Pauline > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
RE: Two variable dictionary comprehension
Nathan Ernst wrote, on April 03, 2017 1:59 PM > > I was a bit surprised when I looked at the language reference > for 3.6.x. I expected there'd be a direct link to > comprehensions, but there's not. > > You have to know what you're looking for: > > 6.2.5: List Displays > 6.2.6: Set Displays > 6.2.7: Dictionary Displays > > And, then, click on the appropriate element of the sub > grammar to find the appropriate syntax. > > So, it took me about 30 seconds to find the appropriate > grammars, when I expected it'd only take about 5 seconds, > since I'm very familiar with the python docs & how the > grammar documentation is laid out. I can fully understand > how someone less familiar with the documentation might have a > harder time finding the grammar than I did. > > FWIW, If one was completely new to Python, even knowing the > syntax is known as a "comprehension" might be unknown. I > certainly didn't know what a comprehension was when I was > learning Python. A coworker showed me, some 13 years ago. > > Regards, > Nate Thanks Nate, for your comprehension of the plight of many, if not most, newish Python coders. And it certainly doesn't help to ask the list to fill in some of the holes and be met with criticism for asking, but I digress. It is what it is. Before I started reading the list a few months ago, I'd heard of list comprehensions in an article I'd read, and hardly understood the gist of it. But look at me now Ma, I've learned not only how to use list comprehensions but also a small tribe of other kinds of comprehensions! (If there's a moral to this story, heck if I know exactly what it is. "Keep on trying" is as good as any.) Deborah > On Mon, Apr 3, 2017 at 3:47 PM, Jerry Hill > wrote: > > > On Mon, Apr 3, 2017 at 10:30 AM, Deborah Swanson > > wrote: > > > Regular updates as the docs are updated would be a good idea too. > > > It's obvious that today's Google isn't up to it, although > it occurs > > > to me that I haven't tried Google's site search on python.org. > > > > So, when you search google for the phrase "dict comprehension" or > > "dictionary comprehension", nothing useful comes up for > you? When I > > search either of those phrases, I get lots of useful > results, all of > > which spell out how to do what you were originally asking about. I > > know Google search results are skewed by past usage, but > I'm surprised > > that you didn't find anything useful in the first couple of search > > results. > > > > When I do a search for 'dict comprehension' I get a boxed result > > linking to PEP 274 as the first hit, then two Stack Overflow > > questions, both of which demonstrate how to do dictionary > > comprehensions. Following that is another link to PEP 274, > a link to > > the Python docs on data structures (which does talk about dict > > comprehensions, but it's way down on the page), and then links to a > > bunch of tutorials. If you had to judge based on my search > results, > > Google does a fine job of answering python questions, at least when > > you already know the key phrase to look for. > > > > -- > > Jerry > > -- > > https://mail.python.org/mailman/listinfo/python-list > > > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
Re: Which directory should requests and openpyxl modules be installed to?
On Monday, April 3, 2017 at 2:02:30 PM UTC-7, Pauline wrote: > Hi, > > I am a newbie to Python. I am using Python 2.7.12. I want to install the > modules of requests and openpyxl using pip. In the Internet, people only > said pip install requests, but they do not say in which directory. I only > found one that said install in the script directory within Python. Well, that > didn't work. > > How and which directory in the command line to type pip install requests? > > Thanks! > Pauline Hi Nate, Well, I didn't tell you I am on Windows 7. When I went to cmd, it was C:\Users\myname. Then I navigated to where my Python was installed C:\Program Files\Python 2.7.12> Do I type pip install requests at the end of the arrow, or do I have to go somewhere else? Thanks a bunch! Pauline -- https://mail.python.org/mailman/listinfo/python-list
Re: Which directory should requests and openpyxl modules be installed to?
On Tue, Apr 4, 2017 at 8:14 AM, wrote: > Hi Nate, > > Well, I didn't tell you I am on Windows 7. When I went to cmd, it was > C:\Users\myname. Then I navigated to where my Python was installed C:\Program > Files\Python 2.7.12> > > Do I type pip install requests at the end of the arrow, or do I have to go > somewhere else? > > Thanks a bunch! Ah, this shouldn't matter. If you've installed Python and pip correctly, you should be able to run them from any directory at all, and they'll do their work correctly. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Which directory should requests and openpyxl modules be installed to?
Hi Pauline, I was able to infer you're on Windows, but not which version. Try right-clicking on the start menu to start a command prompt as an administrator (I'm not sure that was available in Windows 7, and I don't have access to a Win7 box currently to verify). Failing that, you should be able to find a shortcut to the command prompt somewhere under Programs\Windows, Programs\Windows Accessories, or a like named folder. Just keep looking, it's there. (the parent folder will be named Windows or Programs - look through all subfolders until you find it). Once you find it, right-click and select run as Administrator. Then, you should be able to run the pip install command just fine. If you cannot find Command Prompt for whatever reason, an alternate way is to create a short cut on your desktop. Just right-click on your desktop. Select New\Create Shortcut. When it asks you to select the location of the file, just enter "cmd.exe". Hit "next" until the dialog exits. Then, right-click on the newly created shortcut and select "run as administrator", then try your pip install command. Where you run the pip command (i.e. current directory should not matter). Regards On Mon, Apr 3, 2017 at 5:14 PM, wrote: > On Monday, April 3, 2017 at 2:02:30 PM UTC-7, Pauline wrote: > > Hi, > > > > I am a newbie to Python. I am using Python 2.7.12. I want to install > the modules of requests and openpyxl using pip. In the Internet, people > only said pip install requests, but they do not say in which directory. I > only found one that said install in the script directory within Python. > Well, that didn't work. > > > > How and which directory in the command line to type pip install > requests? > > > > Thanks! > > Pauline > > Hi Nate, > > Well, I didn't tell you I am on Windows 7. When I went to cmd, it was > C:\Users\myname. Then I navigated to where my Python was installed > C:\Program > Files\Python 2.7.12> > > Do I type pip install requests at the end of the arrow, or do I have to > go somewhere else? > > Thanks a bunch! > Pauline > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
Re: Which directory should requests and openpyxl modules be installed to?
On Monday, April 3, 2017 at 2:02:30 PM UTC-7, Pauline wrote: > Hi, > > I am a newbie to Python. I am using Python 2.7.12. I want to install the > modules of requests and openpyxl using pip. In the Internet, people only > said pip install requests, but they do not say in which directory. I only > found one that said install in the script directory within Python. Well, that > didn't work. > > How and which directory in the command line to type pip install requests? > > Thanks! > Pauline Hi Chris, Really! :) So I can type pip install requests at the prompt: C:\Program Files\Python 2.7.12>pip install requests or at C:\>pip install requests And the modules would be installed either way? Sorry I am also a newbie at windows command line too. THANKS!!! Pauline -- https://mail.python.org/mailman/listinfo/python-list
Re: Two variable dictionary comprehension
No worries, Deborah. Python is by most measurements a relatively easy/simple language to learn, but there are always the dusty corners. If you've not already, I recommend going through the online Python tutorial in it's entirety ( https://docs.python.org/3/tutorial/index.html). After that, learn the language syntax that wasn't covered in the tutorial by reading the Language Reference ( https://docs.python.org/3/reference/index.html). The tutorial should be fairly easy for a straight beginner to follow. The language reference assumes a little higher-level understanding of programming language grammar. The Python Language Reference uses a modified BNF syntax (BNF being Backus-Naur form. You can read about BNF at https://en.wikipedia.org/wiki/Backus%E2%80%93Naur_form). To be honest, I'm not sure what modifications Python uses to BNF, maybe someone else can shed some light (or skin) on it. After you'd done those, peruse the standard library. I don't recommend deep reading there at this point, but at least a cursory reading so you're cognizant of libraries that are built-in that may help do things may you want to do now, or in the future (i.e. make a web request, parse JSON or XML, handle datetimes). Remember: Python comes with batteries included. -Nate On Mon, Apr 3, 2017 at 5:09 PM, Deborah Swanson wrote: > Nathan Ernst wrote, on April 03, 2017 1:59 PM > > > > I was a bit surprised when I looked at the language reference > > for 3.6.x. I expected there'd be a direct link to > > comprehensions, but there's not. > > > > You have to know what you're looking for: > > > > 6.2.5: List Displays > > 6.2.6: Set Displays > > 6.2.7: Dictionary Displays > > > > And, then, click on the appropriate element of the sub > > grammar to find the appropriate syntax. > > > > So, it took me about 30 seconds to find the appropriate > > grammars, when I expected it'd only take about 5 seconds, > > since I'm very familiar with the python docs & how the > > grammar documentation is laid out. I can fully understand > > how someone less familiar with the documentation might have a > > harder time finding the grammar than I did. > > > > FWIW, If one was completely new to Python, even knowing the > > syntax is known as a "comprehension" might be unknown. I > > certainly didn't know what a comprehension was when I was > > learning Python. A coworker showed me, some 13 years ago. > > > > Regards, > > Nate > > Thanks Nate, for your comprehension of the plight of many, if not most, > newish Python coders. And it certainly doesn't help to ask the list to > fill in some of the holes and be met with criticism for asking, but I > digress. It is what it is. > > Before I started reading the list a few months ago, I'd heard of list > comprehensions in an article I'd read, and hardly understood the gist of > it. But look at me now Ma, I've learned not only how to use list > comprehensions but also a small tribe of other kinds of comprehensions! > > (If there's a moral to this story, heck if I know exactly what it is. > "Keep on trying" is as good as any.) > > Deborah > > > On Mon, Apr 3, 2017 at 3:47 PM, Jerry Hill > > wrote: > > > > > On Mon, Apr 3, 2017 at 10:30 AM, Deborah Swanson > > > wrote: > > > > Regular updates as the docs are updated would be a good idea too. > > > > It's obvious that today's Google isn't up to it, although > > it occurs > > > > to me that I haven't tried Google's site search on python.org. > > > > > > So, when you search google for the phrase "dict comprehension" or > > > "dictionary comprehension", nothing useful comes up for > > you? When I > > > search either of those phrases, I get lots of useful > > results, all of > > > which spell out how to do what you were originally asking about. I > > > know Google search results are skewed by past usage, but > > I'm surprised > > > that you didn't find anything useful in the first couple of search > > > results. > > > > > > When I do a search for 'dict comprehension' I get a boxed result > > > linking to PEP 274 as the first hit, then two Stack Overflow > > > questions, both of which demonstrate how to do dictionary > > > comprehensions. Following that is another link to PEP 274, > > a link to > > > the Python docs on data structures (which does talk about dict > > > comprehensions, but it's way down on the page), and then links to a > > > bunch of tutorials. If you had to judge based on my search > > results, > > > Google does a fine job of answering python questions, at least when > > > you already know the key phrase to look for. > > > > > > -- > > > Jerry > > > -- > > > https://mail.python.org/mailman/listinfo/python-list > > > > > -- > > https://mail.python.org/mailman/listinfo/python-list > > > > -- https://mail.python.org/mailman/listinfo/python-list
Re: Two variable dictionary comprehension
Deborah Swanson wrote: I'll admit that both dictionaries and comprehensions are still a little bit fuzzy to me, especially when I get away from the common usages. This could be a good exercise to clarify some of the fuzzy areas. If you're fuzzy about dictionaries in general, it might be a good idea to concentrate on that for now and come back to comprehensions later. They're something of an advanced topic; anything you can do with a comprehension can also be done in more fundamental ways. -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: Two variable dictionary comprehension
Deborah Swanson wrote: All my guesses were based on the single variable (the most common type) examples I found. I just didn't think of putting a colon after 'label', and found nothing to suggest that's what I should do. Hmmm, I'm not sure what the docs could do to make that any clearer. The key:value syntax is part of *every* dict comprehension (it's the only thing that distinguishes a dict comprehension from a set comprehension). Despite my earlier words and protestations that I did look for two variable dict comprehensions, fairly diligently, I am taking what you said seriously. Don't feel too bad. Sometimes it's hard to know what to google for, even for experienced people! Also it's hard for doc writers to anticipate how less experienced people will think. It wouldn't have occurred to me to use the phrase "two variable dict comprehension" when writing documentation. Another thing it's important to be able to do is see through to the principles behind things, and then use those principles to solve new problems that aren't explicitly covered in any docs or examples. The general principle behind all the forms of comprehension is that they consist of a prototypical element, followed by some clauses that generate values for that element. The syntax for the prototypical element mirrors that of the corresponding display. So, for a dict comprehension, the prototypical element has the form 'key:value', where 'key' and 'value' are arbitrary expressions. From there, it's just a matter of figuring out what to put into those expressions. In your case, another part of the puzzle is the fact that you can unpack a tuple obtained from a for-loop into variables. That's a general feature of for-loops, both inside and outside of comprehensions, so you probably won't find it mentioned explicitly under the heading of dict comprehensions. Maybe it would be worthwhile to scrape the whole mess and have it in searchable text form. The HTML Python docs have a "Search" box, although I haven't really used it so I don't know how well it works. In my experience, Google with a site search often works a lot better than search functions provided by sites themselves. I hope you won't be miffed though if I still come up empty handed and come back here to ask again. Deborah -- https://mail.python.org/mailman/listinfo/python-list
Re: Two variable dictionary comprehension
Deborah Swanson wrote: But, if Larry Page and Sergey Brin could tinker around in their dorm rooms (or wherever they lived then) and they made the first Google (the first search engine?) It wasn't the first web search engine. But it was the first one that not only worked, but *kept* working as the web grew bigger, and bigger, and bigger. -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: Two variable dictionary comprehension
Deborah Swanson wrote: I'd imagine that the old Google would have returned a good 10 pages or more (probably a lot more) of urls containing the phrase "dict comprehension" or "dictionary comprehension". It still does, as far as I can see. I just googled for "dict comprehension", and the vast majority of results in the first 10 pages relate to Python. By page 20 it's starting to wander off a bit, but you can hardly blame it for that. There *are* non-Python web pages that mention the words "dict" and "comprehension", and how is Google to know that you don't want those if you don't tell it? You used to be able to keep sifting through pages of results after the bulk of urls fitting your criteria had passed, and still find useful things to look at, sometimes at page 500 Seems to me Google was doing a rather *bad* job if you had to wade through 500 pages of results to find what you wanted. I would never have the patience to do that! Anyhow, the reason Google got brought up was that you were complaining about difficulty of finding things in the Python docs. Google *does* turn up the relevant part of the docs in the very first page of results, so being able to do a direct text search on the docs wouldn't do any better. -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: Text-mode apps (Was :Who are the "spacists"?)
On Saturday, April 1, 2017 at 9:32:17 PM UTC-5, MRAB wrote: > Sometimes he mentions MUDs, sometimes he mentions Pike, but at least he > doesn't rant. I have not even _begun_ to rant. Yet... -- https://mail.python.org/mailman/listinfo/python-list
Re: Which directory should requests and openpyxl modules be installed to?
On Tue, Apr 4, 2017 at 8:31 AM, wrote: > Hi Chris, > >Really! :) So I can type pip install requests at the prompt: C:\Program > Files\Python 2.7.12>pip install requests >or at C:\>pip install requests > >And the modules would be installed either way? > >Sorry I am also a newbie at windows command line too. > > THANKS!!! Correct! The Windows program search path means it'll find pip.exe (if it doesn't, you'll get a wordy error that used to be called SYS1041), and then pip itself looks in the registry and other places to figure out where it should install things. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Text-mode apps (Was :Who are the "spacists"?)
On Sunday, April 2, 2017 at 11:26:50 AM UTC-5, Steve D'Aprano wrote: > On Sun, 2 Apr 2017 04:41 pm, Terry Reedy wrote: > > On 4/1/2017 12:00 PM, Steve D'Aprano wrote: > > > > > example of the Ugly American. > > > > As an American I resent your promotion and perpetuation of > > an ugly ethno-centric stereotype. > > I'm glad you didn't try to describe it as a *unfair* or > *unjustified* stereotype, because (let's face it) after > November 8th 2016, that simply wouldn't fly. D'Aprano, are you still stewing because Donald J Trump spanked Hillary's jumbo sized bottom like an unruly stepchild? You poor widdle partisian hack. I almost feel sorry for you. Heck, Trump has to be the most crass and unstatesmanlike candidate to ever run for the american presidency -- well, certainly in my lifetime -- so it should have been the surest win in history for Hillary (or should i say "HER-"?), but even with the mainstream media planted firmly in her pocket; Bill Clinton's endless charms; a grotesque prosession of hollywood celebs, and Obama on the campaign trail -- she folded like lawn chair! How pathetic. Not to mention that the democratic party cheated Burnie Sanders. (i wonder if he's "feelin' the burn"?) Of course, in their hubris, the Dems put all their chips on the same old tired "game of firsts" crapola, thinking the emotional sleight of hand was enough to clinch them the white house, but fortunately (for america), somebody at the DNC forgot to tell the Dem leaders that america is getting *SICK* and *TIRED* of the emotional games. We only want two types of candidates now: (1) those who are highly qualified non-partisans, or (2) those who will burn the old orders to the ground! With Trump, we got the latter. But as bad a Trump is, he's better than the "business as usual" we get from Repuke-I-Cans and Demon-Rats. But Steven, you're not even an american are you? Nope! You're just another Brit with a stick jambed firmly up his butt waddling around lecturing Americans about how terrible we are, but have you ever stopped to consider your own nation's horrendous history? TIP OF THE DAY: "Great Britain" is the imperialist empire previously known as "England". Now sod-off to your local library so you can catch up on, say, the last umpteen centuries of English tyranny and global colonization. Oh, and, please note that people who live in glass houses would be wise not to start a stone throwing war. just FYI, old buddy. *wink* > Not all Americans, perhaps not even a majority or a > plurality, are Ugly Americans, but there are enough of them > to screw it up for everyone else. There are bad apples in every bunch; every nation; every group. Stupidity and ignorance are ubiquitous traits and are fairly evenly distributed around the globe. So enough with America bashing already. We Americans are no more guilty of hate and intolerance than any other nation or peoples on this planet. Heck, we've only existed for just over 200 years, yet many polmics and media types would have the world believe that hate and intolerance didn't exist in this universe until July 4, 1776. And when has _any_ reigning power in human history ever acted in a 100% benevolent manner? Hmm? (At least, any one that matters, that is). If we read our history books, we would come to the conclusion that tyranny is the natural order of our human society, and, more evidence of universal laws in action. Therefore, only through perpetual conflict can we keep tyranny at bay. For instance, it's not as if we could tell ol' Henry the Eighth: "You know what Henry... you can be a real horse's arse!" -- to which he would "supposedly" reply -- "Indeed. I think i am. And uh, thank you for the healthy criticism" -- NOT! It is more likely that anyone who dared critize the king would win a free all- expense-paid trip to the tower of London, a most horrific place with a most notorious reputation for cruelty. But the English had quite a knack for cruelty, eh Steven? Hmm, remind me again, ol' boy, how many wives did King Henry the VIII have executed for their "supposed" infidelity? Of course, there are rumours that one of the executions did not go as "cleanly" as planned. My observation is that the botched nature of that execution was a feature, not a bug. But depravity has been the SOP for centuries in "Merry ol' England", eh chap? > It's an ugly stereotype, not because it is a stereotype, > but because it embodies a set of ugly attitudes and > behaviours. Your feeble attempts to legitimize your American bashing are deplorable at best. For once in your life, Steven, admit that you're filled with hate and apologize for it. If you don't like America, fine, don't come here. Is that so difficult for you? *WHO* we elect and *HOW* we live our lives is none of your damned business. Personally, i don't give a monkey's toss who your prime minister is, or what your pompous Queen wishes to have dipped in gold and encrusted with diamonds. Personally i think your whole system of go
Re: Text-mode apps (Was :Who are the "spacists"?)
On Sunday, April 2, 2017 at 11:26:50 AM UTC-5, Steve D'Aprano wrote: On Sun, 2 Apr 2017 04:41 pm, Terry Reedy wrote: On 4/1/2017 12:00 PM, Steve D'Aprano wrote: example of the Ugly American. As an American I resent your promotion and perpetuation of an ugly ethno-centric stereotype. I'm glad you didn't try to describe it as a *unfair* or *unjustified* stereotype, because (let's face it) after November 8th 2016, that simply wouldn't fly. D'Aprano, are you still stewing because Donald J Trump spanked Hear hear -- https://mail.python.org/mailman/listinfo/python-list
RE: Two variable dictionary comprehension
On Tue, 4 Apr 2017 03:27 am, Deborah Swanson wrote:
> I'll admit that both dictionaries and comprehensions are still a little
> bit fuzzy to me, especially when I get away from the common usages. This
> could be a good exercise to clarify some of the fuzzy areas.
As far as comprehensions go, how is your mathematics?
If you remember your set builder notation from maths, list comprehensions
are based on that.
In maths, we say something like:
{2n + 1 : 0 ≤ n ≤ 9}
which might be read aloud as "the set of 2 times n, plus 1, such that n is
between 0 and 9 inclusive".
http://www.mathwords.com/s/set_builder_notation.htm
A similar notation might be:
{2n + 1 : n ∈ {1, 2, 3}}
said as "the set of 2 times n, plus 1, such that n is an element of the set
{1, 2, 3}".
If you're a maths geek like me, this is second nature :-)
Now, Python's list comprehension syntax is based on a similar notation,
except we spell it out in words rather than symbols, using the
familiar "for n in ..." syntax from for-loops instead of : "such that".
{2*n + 1 for n in range(10)}
{2*n + 1 for n in (1, 2, 3)}
A few other differences:
- list comprehensions use [ ] for the outermost brackets;
- set and dict comprehensions use { };
- generator expressions use ( ) (unless the parentheses can be implied).
We're not restricted to mathematical expressions, and can use any Python
expression we like:
[value.method(arg)[1] for (value, arg) in zip(values, arguments)]
translates roughly into this for-loop:
result = []
for (value, arg) in zip(values, arguments):
result.append(value.method(arg)[1])
Another difference is that comprehensions allow an "if" clause:
[v.method(a)[1] for (v, a) in zip(values, arguments) if v is not None]
translates something like:
result = []
for (v, a) in zip(values, arguments):
if v is not None:
result.append(v.method(a)[1])
There's more, but that covers probably 98% of comprehension usage.
And of course, remember that dict comprehensions use the same key:value
syntax as ordinary dict "literals" (the official name is "dict display").
result = {}
for key, value in zip(keys, values):
result[key] = value
becomes
result = {key:value for (key, value) in zip(keys, values)}
although frankly that's better written as:
dict(zip(keys, values))
Hope this helps!
--
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.
--
https://mail.python.org/mailman/listinfo/python-list
Re: Two variable dictionary comprehension
On Mon, Apr 3, 2017 at 8:24 PM Dennis Lee Bieber wrote: > On Mon, 3 Apr 2017 11:48:38 -0700, "Deborah Swanson" > declaimed the following: > > >But, if Larry Page and Sergey Brin could tinker around in their dorm > >rooms (or wherever they lived then) and they made the first Google (the > >first search engine?) to boolean search the World Wide Web, it shouldn't > >be so awfully hard to make a collection of Python docs that's boolean > >searchable. > >>Alta Vista predated Google by a few years (it was later absorbed by > Yahoo). Don't forget Ask Jeeves > > -- https://mail.python.org/mailman/listinfo/python-list
Re: Text-mode apps (Was :Who are the "spacists"?)
On Tue, Apr 4, 2017 at 10:13 AM, Rick Johnson wrote: > D'Aprano, are you still stewing because Donald J Trump > spanked Hillary's jumbo sized bottom like an unruly > stepchild? You poor widdle partisian hack. I almost feel > sorry for you. > > [snip massively long political rant] Alright, can the politics please be taken off list? I believe the appropriate newsgroup for this drivel is comp.lang./dev/null. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Appending data to a json file
I created a python program that gets data from a user, stores the data as a dictionary in a list of dictionaries. When the program quits, it saves the data file. My desire is to append the new data to the existing data file as is done with purely text files. However, I can't find a way to do that. The advice I have seen on the web is to load the data when the program starts, append the new user input to the list, then re-write the data file. Is that the best way, or is there a better way? Thanks, Dave -- https://mail.python.org/mailman/listinfo/python-list
Re: Text-mode apps (Was :Who are the "spacists"?)
On Sunday, April 2, 2017 at 3:49:43 PM UTC-5, Gene Heskett wrote: > On Sunday 02 April 2017 12:26:40 Steve D'Aprano wrote: > > On Sun, 2 Apr 2017 04:41 pm, Terry Reedy wrote: > > > On 4/1/2017 12:00 PM, Steve D'Aprano wrote: > > > > > > > > example of the Ugly American. > > > > > > As an American I resent your promotion and perpetuation > > > of an ugly ethno-centric stereotype. > > > > I'm glad you didn't try to describe it as a *unfair* or > > *unjustified* stereotype, because (let's face it) after > > November 8th 2016, that simply wouldn't fly. > > While I too detest that label, I will have to admit that I > am one of the conservatives, believing our Constitution and > Bill of Rights have been used as TP by the "establishment" > over the last 65+ years, who enabled that surprise. Saying what you just said, in _this_ group at least, can bring some nasty rebukes. And not because it is wrong, no, but because the proggie-bots will accuse you of being (1) a racist opining for the return of slavery, (2) a homophobic who would throw people from rooftops, (3) a bigot, (4) something else that is equally "bad", or (5) all of the above. Hopefully though, like me, you're undeterred by lies and slander because your skin is thicker than that of the average grape. As for me, i do not want america society to return to 1950, no, i want to american _ideals_ to return to July 4, 1776. And this time we would: "get it right". The first order of business would be to re-write the constitution in a manner that will remove the subtle semantical errors that have caused so much trouble for these last 200+ years. Starting with the Declaration of Independence. (emphasis *MINE*!) "When in the course of human events, it becomes *NECESSARY* for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth the *SEPARATE* and *EQUAL* station to which the laws of nature and of [the natural processes that] *ENTITLE* them, a decent respect to the [reputation] of [humankind] requires that they should declare the causes which impel them to the separation." "We hold these truths to be self-evident, that all [people] are created equal, that they are endowed by their [existence] with certain unalienable rights: that among these are life, liberty and the pursuit of happiness. -- that to *SECURE* these rights, governments are instituted among [people], deriving their just powers from the *CONSENT* of the governed, -- That whenever any form of government becomes destructive of these ends, it is the *RIGHT* of the people to alter or to abolish it, and to institute new government, laying its foundation on such *PRINCIPLES* and organizing its powers in such *FORM*, as to them shall seem most likely to effect their safety and happiness." "Prudence, indeed, will dictate that governments long established should *NOT* be changed for _light_ and _transient_ causes; and accordingly, all experience hath shewn, that [humankind] are more disposed to suffer while evils are "sufferable", than to right themselves by abolishing the forms to which they are "accustomed". But when a *LNG* train of abuses and usurpations pursuing invariably the same [*GOALS* -- to achieve an absolute despotism --], it is their *RIGHT*, it is their *DUTY*, to throw off such government and to provide new guards for their future security. -- Such has been the patient sufferance of these [people]; and such is now the *NECESSITY* which constrains them to alter their former systems of government. The history of the present [United States Of America] is a history of repeated injuries and usurpations [-- and quite frankly, a charade of vile hypocrisy! --], all having in direct object the establishment of an absolute tyranny over these [people]. To prove this, let facts be submitted to a candid world." [List of modern grievances snipped for bandwidth] Simply removing all the misogynist references and patriarchal terms is a good start. But what is important to realize, is that, removing these mistakes does not undermine the intent of this document, no, it merely broadens and purifies that intent. The fact is, this document has caused a social transformation that has reverberated around the world. (Rhetorically i ask...) What _is_ "life, liberty, and the pursuit of happiness"? Is it merely the monotone words of a random child standing on a random grade-school stage reenacting the events of our forefathers for the sake of mere "cute spectacle"? Or is it something more? -- I believe it be. -- I believe it to be one of the most *PROFOUND* realizations of social justice that the human mind has *EVER* conceived. A culmination of many thousand years of trial and error, and the product of much pain and suffering. The American Declaration Of Independence marks an epoch in our collective human social and intellectual evolu
Re: Two variable dictionary comprehension
On Mon, Apr 3, 2017 at 5:50 PM, Deborah Swanson
wrote:
> Ah, but did you actually try to use the proposed solutions on the two
> stackoverflow pages? It's been several weeks now, but I did, and neither
> of those two examples fit my situation, which is why I ended up writing
> my own, and unsatisfactorily at that.
Well, that first Stack Overflow link has the following as the first
part of the highest scored answer:
In Python 2.6 and earlier, the dict constructor can receive an
iterable of key/value pairs:
d = dict((key, value) for (key, value) in iterable)
From Python 2.7 and 3 onwards, you can just use the dict
comprehension syntax directly:
d = {key: value for (key, value) in iterable}
Isn't that second form the exact thing you were looking for, back in
your first post?
> I'm sorry you think the current edition of Google does such a fine job.
> Has it really been that many years ago that the results Google returned
> from a properly formatted boolean search were really useful? I'd
> imagine that the old Google would have returned a good 10 pages or more
> (probably a lot more) of urls containing the phrase "dict comprehension"
> or "dictionary comprehension". in which you'd find a rich variety of
> specific situations to glean through. (You wouldn't have needed to
> include "python" in the search phrase, since no other programming
> language that I know of, or other English usage for that matter, has
> dict comprehensions.)
Is this not your experience today? I just browsed through the first 5
pages of search results for the phrase "dict comprehension" (without
the quotes), and the results seem to be pretty on point. It's mostly
results talking about python dict comprehensions, general python pages
talking about all sorts of comprehensions (dict, list, and set), and
as you get deeper into the result pages, you start to see some entries
for the word "comprehension" in dictionaries too, which seems like a
reasonable thing to end up mixed in with the desired results. It goes
on in that vein out to page 11 or so, at which point things seem to
devolve a bit.
I'd be totally sympathetic with your plight if you didn't know the key
phrase 'dict comprehension' to find all of that information. I'm just
not seeing the poor results you seem to be getting from Google once
you know the term.
--
Jerry
--
https://mail.python.org/mailman/listinfo/python-list
Re: Appending data to a json file
On 04/03/2017 08:59 PM, Dave wrote:
> I created a python program that gets data from a user, stores the data
> as a dictionary in a list of dictionaries. When the program quits, it
> saves the data file. My desire is to append the new data to the
> existing data file as is done with purely text files. However, I can't
> find a way to do that. The advice I have seen on the web is to load the
> data when the program starts, append the new user input to the list,
> then re-write the data file. Is that the best way, or is there a better
> way?
If you're talking about a plain text file, can't you just open the file
for append mode and write away to it?
f = open("file","at")
with f:
f.write("More text.\n")
--
https://mail.python.org/mailman/listinfo/python-list
Re: Which directory should requests and openpyxl modules be installed to?
On Tue, Apr 4, 2017 at 1:45 AM, Dennis Lee Bieber wrote: > > C:\Users\Wulfraed>assoc .py > .py=Python.File > > C:\Users\Wulfraed>ftype python.file > python.file="C:\Python27\python.exe" "%1" %* The Windows shell stores the user file-association choice in HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.py\UserChoice. The cmd shell's assoc and ftype commands only modify a subset of system associations in HKLM\Software\Classes. If keys with the same names exist in HKCU\Software\Classes (e.g. Python and the launcher were installed just for the current user), they will override the system associations. Moreover, the user may be using a totally different ProgId -- usually due to misconfiguration, which is unfortunately all too common of a problem. A couple of common misconfigurations are HKCU\Software\Classes\py_auto_file and HKCU\Software\Classes\Applications\python.exe. These get created automatically by the Windows shell, and will almost always have a broken "open" command that doesn't pass command-line arguments. By using these ProgIds you're also missing the new drop handler that Steve Dower wrote to support dropping files with non-ANSI names. It's best to switch back to using Python.File, Python.NoConFile and friends. The UserChoice key is doubly secured via its "Hash" value plus a security descriptor that prevents the current user from modifying values in the key. This is to prevent direct modification. The most you can do in code is to delete the key to have the shell reset from defaults. By design, the only way to directly change the user choice is via the GUI. Use the Control Panel's "Default Programs" app. Select "Associate a filetype or protocol...". Type ".py" to advance in the list, double-click on the entry, and select the app to use. If Python.File is configured to use the launcher, the "Python" entry in this application list should have an icon with a rocket on it. -- https://mail.python.org/mailman/listinfo/python-list
RE: Two variable dictionary comprehension
Thank you Nate, for all these sources to study. Python was very easy for me to learn in 2 online courses, but it's been all uphill since then. I've learned a lot and for that I'm grateful, but there's so much farther to go. I've appreciated our discussions, but I am in fact a very sick person and my illness has come back again tonight. I can probably reply to a few more posts and then you won't see me again for awhile. But many thanks again, and I will begin reading the material you suggested. Deborah Nathan Ernst wrote, on Monday, April 03, 2017 3:37 PM No worries, Deborah. Python is by most measurements a relatively easy/simple language to learn, but there are always the dusty corners. If you've not already, I recommend going through the online Python tutorial in it's entirety (https://docs.python.org/3/tutorial/index.html). After that, learn the language syntax that wasn't covered in the tutorial by reading the Language Reference (https://docs.python.org/3/reference/index.html). The tutorial should be fairly easy for a straight beginner to follow. The language reference assumes a little higher-level understanding of programming language grammar. The Python Language Reference uses a modified BNF syntax (BNF being Backus-Naur form. You can read about BNF at https://en.wikipedia.org/wiki/Backus%E2%80%93Naur_form). To be honest, I'm not sure what modifications Python uses to BNF, maybe someone else can shed some light (or skin) on it. After you'd done those, peruse the standard library. I don't recommend deep reading there at this point, but at least a cursory reading so you're cognizant of libraries that are built-in that may help do things may you want to do now, or in the future (i.e. make a web request, parse JSON or XML, handle datetimes). Remember: Python comes with batteries included. -Nate On Mon, Apr 3, 2017 at 5:09 PM, Deborah Swanson wrote: Nathan Ernst wrote, on April 03, 2017 1:59 PM > > I was a bit surprised when I looked at the language reference > for 3.6.x. I expected there'd be a direct link to > comprehensions, but there's not. > > You have to know what you're looking for: > > 6.2.5: List Displays > 6.2.6: Set Displays > 6.2.7: Dictionary Displays > > And, then, click on the appropriate element of the sub > grammar to find the appropriate syntax. > > So, it took me about 30 seconds to find the appropriate > grammars, when I expected it'd only take about 5 seconds, > since I'm very familiar with the python docs & how the > grammar documentation is laid out. I can fully understand > how someone less familiar with the documentation might have a > harder time finding the grammar than I did. > > FWIW, If one was completely new to Python, even knowing the > syntax is known as a "comprehension" might be unknown. I > certainly didn't know what a comprehension was when I was > learning Python. A coworker showed me, some 13 years ago. > > Regards, > Nate Thanks Nate, for your comprehension of the plight of many, if not most, newish Python coders. And it certainly doesn't help to ask the list to fill in some of the holes and be met with criticism for asking, but I digress. It is what it is. Before I started reading the list a few months ago, I'd heard of list comprehensions in an article I'd read, and hardly understood the gist of it. But look at me now Ma, I've learned not only how to use list comprehensions but also a small tribe of other kinds of comprehensions! (If there's a moral to this story, heck if I know exactly what it is. "Keep on trying" is as good as any.) Deborah > On Mon, Apr 3, 2017 at 3:47 PM, Jerry Hill > wrote: > > > On Mon, Apr 3, 2017 at 10:30 AM, Deborah Swanson > > wrote: > > > Regular updates as the docs are updated would be a good idea too. > > > It's obvious that today's Google isn't up to it, although > it occurs > > > to me that I haven't tried Google's site search on python.org. > > > > So, when you search google for the phrase "dict comprehension" or > > "dictionary comprehension", nothing useful comes up for > you? When I > > search either of those phrases, I get lots of useful > results, all of > > which spell out how to do what you were originally asking about. I > > know Google search results are skewed by past usage, but > I'm surprised > > that you didn't find anything useful in the first couple of search > > results. > > > > When I do a search for 'dict comprehension' I get a boxed result > > linking to PEP 274 as the first hit, then two Stack Overflow > > questions, both of which demonstrate how to do dictionary > > comprehensions. Following that is another link to PEP 274, > a link to > > the Python docs on data structures (which does talk about dict > > comprehensions, but it's way down on the page), and then links to a > > bunch of tutorials. If you had to judge based on my search > results, > > Google does a fine job of answering python questions, at least when > > you already know the key phrase to look for. > > > > -- > >
Version 2 of my natural language steganographical scheme released
Version 2 of my natural language steganographical scheme WORDLISTTEXTSTEGANOGRAPHY is available on my home page http://mokkong-shen.privat.t-online.de , together with a few other cryptological and steganographical software of mine. See update notes in it for the differences to earlier versions. I regret that in the earlier versions a sentence was unfortunately missing in the explanation of how to do the examples, with the consequence that new users might have difficulties with the examples. M. K. Shen -- https://mail.python.org/mailman/listinfo/python-list
Re: Obtain Ceritificate Information from Invalid or Self-Signed Certificate in Python
Kenneth Buckler writes:
> I'm working on a Python 2.7.13 (Win x64) script to verify SSL certificates,
> and alert for problems. Specifically, I'm looking to return the date the
> cert expires or did expire. However, I'm running into an issue where the
> script will return information only if the certificate is valid.
You may need to tell the Python socket library that you are
ready to accept any certificate - even expired ones.
I see below, that you already have tried that
("conn._https_verify_certificates(enable=False)") but it failed.
The reason: "_https_verify_certificates" is a function of the "ssl"
module, not a method of "SSLSocket" instances.
It is used to switch (globally) the behavior for verifying certificates,
not locally for a specific "SSLSocket".
Given the architecture of the "ssl" module (with the component
classes "socket", "SSLContext" and "SSLSocket"), the most likely
place to control the certificate verification is the "SSLContext".
And indeed, it has an attribute "verify_mode" to control this behaviour.
Likely, there is an alternative to disable certificate
verification in your case: the "ssl" module has the function
"get_server_certificate"; you could try to perform a normal
ssl connection and if this fails due to certificate problems,
you could fetch the certificate with the above function and analyse it.
> ...
> Per https://docs.python.org/2/library/ssl.html I tried to use
> conn._https_verify_certificates(enable=False) to disable certificate
> validation, but get an error that the attribute _https_verify_certificates
> doesn't exist.
> ...
--
https://mail.python.org/mailman/listinfo/python-list
Re: Appending data to a json file
Dave writes: > I created a python program that gets data from a user, stores the data > as a dictionary in a list of dictionaries. When the program quits, it > saves the data file. My desire is to append the new data to the > existing data file as is done with purely text files. Usually, you cannot do that: "JSON" stands for "JavaScript Object Notation": it is a text representation for a single (!) JavaScript object. The concatenation of two JSON representations is not a valid JSON representation. Thus, you cannot expect that after such a concatenation, a single call to "load" will give you back complete information (it might be that a sequence of "load"s works). Personally, I would avoid concatenated JSON representations. Instead, I would read in (i.e. "load") the existing data, construct a Python object from the old and the new data (likely in the form of a list) and then write it out (i.e. "dump") again. -- https://mail.python.org/mailman/listinfo/python-list
Re: Appending data to a json file
You _can_ just extend a JSON file without loading it, but it will not be "fun". Say the JSON file contains a top-level array. The final significant character in it would be a ']'. So, you can read just a reasonably-sized block from the end of the file, find the location of the final ']', overwrite it with a ',' followed by your additional array entry/entries, with a final ']'. If the JSON file contains a top-level object, the final significant character would be a '}'. Overwrite it with a ',' followed by your additional object key/value pairs, with a final '}'. Basically, if what you want to append is of the same kind as the content of the file (array appended to array, or object to object): - Locate final significant character in the file - Locate first significant character in your appended data, replace it with a ',' - Overwrite the final significant character in the file with your patched data It isn't elegant or very robust, but if you want to append to a very large JSON array (for example, some log file?), then it could be very efficient and effective. Or, you could use YAML ;-) On Tue, Apr 4, 2017 at 8:31 AM, dieter wrote: > Dave writes: > > > I created a python program that gets data from a user, stores the data > > as a dictionary in a list of dictionaries. When the program quits, it > > saves the data file. My desire is to append the new data to the > > existing data file as is done with purely text files. > > Usually, you cannot do that: > "JSON" stands for "JavaScript Object Notation": it is a text representation > for a single (!) JavaScript object. The concatenation of two > JSON representations is not a valid JSON representation. > Thus, you cannot expect that after such a concatenation, a single > call to "load" will give you back complete information (it might > be that a sequence of "load"s works). > > Personally, I would avoid concatenated JSON representations. > Instead, I would read in (i.e. "load") the existing data, > construct a Python object from the old and the new data (likely in the form > of a list) and then write it out (i.e. "dump") again. > > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
RE: Two variable dictionary comprehension
Gregory Ewing wrote, on Monday, April 03, 2017 4:23 PM > > Deborah Swanson wrote: > > All my guesses were based on the > > single variable (the most common type) examples I found. I just didn't > > think of putting a colon after 'label', and found nothing to suggest > > that's what I should do. > > Hmmm, I'm not sure what the docs could do to make that any > clearer. The key:value syntax is part of *every* dict > comprehension (it's the only thing that distinguishes a dict > comprehension from a set comprehension). I guess I saw a lot of examples that weren't clearly using the key:value syntax, and all of it was so unfamiliar, that pattern just didn't stand out to me. But it's starting to now, and that's the direction I need to go in for dict comprehensions. > > Despite my earlier words and protestations that I did look for two > > variable dict comprehensions, fairly diligently, I am taking what you > > said seriously. > > Don't feel too bad. Sometimes it's hard to know what to > google for, even for experienced people! Also it's hard for > doc writers to anticipate how less experienced people will > think. It wouldn't have occurred to me to use the phrase "two > variable dict comprehension" when writing documentation. Yes, I was pretty sure the terminology I phrased the question with wasn't correct, but I didn't know the right words to say, or where to look them up, so I just tried to be as descriptive as I could. But I accomplished my purpose in asking the question, even if it was poorly put, and I've received quite a bit of good information, the terminology I couldn't put my finger on, and some solid pointers of directions to go in the future. > Another thing it's important to be able to do is see through > to the principles behind things, and then use those > principles to solve new problems that aren't explicitly > covered in any docs or examples. Ah, yes, there's the rub. But it's true, from cooking to car mechanics to Python coding, the key is in becoming familiar with the subject, and hands on is the best teacher. (Yes, yes, we could have a discussion about the role of education, but I'll say that it's seeing it all in motion for yourself many times that seals the teaching into something you know and don't need to be told.) > The general principle behind all the forms of comprehension > is that they consist of a prototypical element, followed by > some clauses that generate values for that element. > > The syntax for the prototypical element mirrors that of the > corresponding display. So, for a dict comprehension, the > prototypical element has the form 'key:value', where 'key' > and 'value' are arbitrary expressions. From there, it's just > a matter of figuring out what to put into those expressions. > > In your case, another part of the puzzle is the fact that > you can unpack a tuple obtained from a for-loop into > variables. That's a general feature of for-loops, both > inside and outside of comprehensions, so you probably > won't find it mentioned explicitly under the heading of > dict comprehensions. > > > Maybe it would be worthwhile to scrape the whole mess and have it in > > searchable text form. > > The HTML Python docs have a "Search" box, although I haven't > really used it so I don't know how well it works. It sucks, in a phrase. You're presented with a list of web page titles, very few of which seem to have much to do with the Python language topic you type into the search box. If you're interested in theoretical dissertations you'll be pleased, but if you're looking for barebones mechanical descriptions of the language you'll be wasting your time, in my experience. > In my > experience, Google with a site search often works a lot > better than search functions provided by sites themselves. Yes, I used to use Google's site search on a regular basis but somehow got out of the habit. It likely can't be beat if you know which website will have the answer you're looking for, but the old Google truly shown at finding places you didn't already know about. > > I hope you won't be miffed though if I still come up empty handed and > > come back here to ask again. > > > > Deborah > > > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
RE: Two variable dictionary comprehension
Gregory Ewing wrote, on Monday, April 03, 2017 4:23 PM > > Deborah Swanson wrote: > > I'd > > imagine that the old Google would have returned a good 10 pages or > > more (probably a lot more) of urls containing the phrase "dict > > comprehension" or "dictionary comprehension". > > It still does, as far as I can see. I just googled for "dict > comprehension", and the vast majority of results in the first > 10 pages relate to Python. > > By page 20 it's starting to wander off a bit, but you can > hardly blame it for that. There *are* non-Python web pages > that mention the words "dict" and "comprehension", and how is > Google to know that you don't want those if you don't tell it? Yes, I searched on "python dict comprehension" because I'd gottion the idea somewhere that the "python" was necessary in this Brave New Google world. I'll remember to omit "python" in the future when searching for phrases most frequently found in Python. Boolean rules were so much easier to learn and remember. > > You used to be able to keep sifting through pages of results after the > > bulk of urls fitting your criteria had passed, and still find useful > > things to look at, sometimes at page 500 > > Seems to me Google was doing a rather *bad* job if you had > to wade through 500 pages of results to find what you wanted. > I would never have the patience to do that! Well, it helped if you used 100 search results per page and other various and sundry tools. But patience was good too. It depended how bad you wanted the answer and how good a skim reader you are. Now I rarely find anything I want unless I'm shopping for something to buy, but even then I have to wade through all the garbage results intended to entice the impulse buyer in me, which I ditched decades ago. > Anyhow, the reason Google got brought up was that you were > complaining about difficulty of finding things in the Python > docs. Google *does* turn up the relevant part of the docs in > the very first page of results, so being able to do a direct > text search on the docs wouldn't do any better. We can duke that out some other time. My illness is overcoming me fast though, so it won't be right away. Deborah > -- > Greg -- https://mail.python.org/mailman/listinfo/python-list
RE: Two variable dictionary comprehension
Thanks Steve for writing this, and I'll read it more carefully when my
illness gives my mind back to me.
I was actually a math major before I discovered computer science, and I
had to progress beyond machine language and assembler before I found the
subtle differences and more flexible boundaries of higher level
programming languages you talk about. I'm sure much of it's the same
with Python. They key I think will be in finding more of the logic in
Python, and then fleshing it out with more detail and choices. The core
logic wasn't taught to me in the courses I took, so I need to backfill
it now.
I look forward to studying what you've written here as soon as I can.
Deborah
Steve D'Aprano wrote, on Monday, April 03, 2017 6:05 PM
>
> On Tue, 4 Apr 2017 03:27 am, Deborah Swanson wrote:
>
> > I'll admit that both dictionaries and comprehensions are still a
> > little bit fuzzy to me, especially when I get away from the common
> > usages. This could be a good exercise to clarify some of the fuzzy
> > areas.
>
>
> As far as comprehensions go, how is your mathematics?
>
> If you remember your set builder notation from maths, list
> comprehensions are based on that.
>
> In maths, we say something like:
>
> {2n + 1 : 0 ≤ n ≤ 9}
>
> which might be read aloud as "the set of 2 times n, plus 1,
> such that n is between 0 and 9 inclusive".
>
> http://www.mathwords.com/s/set_builder_notation.htm
>
> A similar notation might be:
>
> {2n + 1 : n ∈ {1, 2, 3}}
>
> said as "the set of 2 times n, plus 1, such that n is an
> element of the set {1, 2, 3}".
>
>
> If you're a maths geek like me, this is second nature :-)
>
>
> Now, Python's list comprehension syntax is based on a similar
> notation, except we spell it out in words rather than
> symbols, using the familiar "for n in ..." syntax from
> for-loops instead of : "such that".
>
> {2*n + 1 for n in range(10)}
>
> {2*n + 1 for n in (1, 2, 3)}
>
>
> A few other differences:
>
> - list comprehensions use [ ] for the outermost brackets;
> - set and dict comprehensions use { };
> - generator expressions use ( ) (unless the parentheses can
> be implied).
>
> We're not restricted to mathematical expressions, and can use
> any Python expression we like:
>
> [value.method(arg)[1] for (value, arg) in zip(values, arguments)]
>
>
> translates roughly into this for-loop:
>
> result = []
> for (value, arg) in zip(values, arguments):
> result.append(value.method(arg)[1])
>
>
>
> Another difference is that comprehensions allow an "if" clause:
>
> [v.method(a)[1] for (v, a) in zip(values, arguments) if v is not None]
>
> translates something like:
>
>
> result = []
> for (v, a) in zip(values, arguments):
> if v is not None:
> result.append(v.method(a)[1])
>
>
> There's more, but that covers probably 98% of comprehension usage.
>
> And of course, remember that dict comprehensions use the same
> key:value syntax as ordinary dict "literals" (the official
> name is "dict display").
>
> result = {}
> for key, value in zip(keys, values):
> result[key] = value
>
>
> becomes
>
> result = {key:value for (key, value) in zip(keys, values)}
>
> although frankly that's better written as:
>
> dict(zip(keys, values))
>
>
>
> Hope this helps!
>
>
>
>
> --
> Steve
> “Cheer up,” they said, “things could be worse.” So I cheered
> up, and sure enough, things got worse.
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
--
https://mail.python.org/mailman/listinfo/python-list
Re: Basic Nested Dictionary in a Loop
Ganesh Pal wrote: >> >> >> Whenever you feel the urge to write range(len(whatever)) -- resist that >> temptation, and you'll end up with better Python code ;) >> >> > Thanks for this suggestion but for my better understanding can explain > this further even Steve did point the same mistake. It's not directly a mistake, it's just that programming against a smaller interface makes your code more flexible and more readable. Readability: items = [...] for i in range(len(items)): ... Are list entries replaced? We cannot know without inspecting the for suite. items = [...] for item in items: ... Are list entries replaced? No. Flexibility: def give_raise(employees, amount): for i in range(len(items)): employee = employees[i] employee.payment += amount employees = [...] give_raise(employees, 10) Now let's adapt the code to raise the payment of some employees: male_employees = [ employees[i] for i in range(len(employees)) if e.sex == "male" ] give_raise(male_emplyees, 10) We need a helper list, basically because there is a len() call inside the give_raise() function. If we rewrite the function in an idiomatic way def give_raise(employees, amount): for employee in employees: employee.payment += amount it will become not just more concise, the calling code no longer needs to build the intermediate list. Instead we save the extra memory and pass a generator expression: give_raise((e for e in employees if e.sex == "female"), 10) In a similar way you can write code that iterates over data that is lazily read into memory, like lines in a file # wrong, reads the whole file into memory lines = open(...).readlines() for i in range(len(lines)): process(lines[i]) # idiomatic, read the lines as needed for line in open(...): process(line) or records from a database. -- https://mail.python.org/mailman/listinfo/python-list
