RE: Context without manager

2023-11-27 Thread David Raymond via Python-list
> I *must* do:
> 
> with device_open() as device:
>device.do_something()
> 
> Nevertheless, I _need_ to have a class
> where the device is opened in the __init__()
> and used in some methods.
> 
> Any ideas?

Perhaps take a look at contextlib.ExitStack and see if you can do something 
with it.

(Below is not tested)

import contextlib
class myClass:
def __init__(self):
self._exitStack = contextlib.ExitStack()
device = self._exitStack.enter_context(device_open(...))
def __del__(self):
self._exitStack.close()
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Initialising a Config class

2023-04-11 Thread David Raymond
Not sure if I'm fully understanding the question. But one option instead of 
making everything class attributes is to just define __getattr__ for when it 
doesn't find an attribute.

Won't work for every single valid section and option name (because of spaces, 
name overlaps, etc) but should cover most things.
For example, you could use a dunder to separate section and option. Then 
something like this?


import configparser

class Config:
def __init__(self, configFile):
self._config = configparser.ConfigParser()
self._config.read(configFile)
def __getattr__(self, option):
if "__" in option:
section, option = option.split("__", 1)
else:
section = self._config.default_section
return self._config[section][option]

c = Config("SomeConfigFile.txt")
print(c.uids__minimum_uid) #Will check for the option "minimum_uid" in the 
"uids" section
print(c.minimum_uid)   #Will check the default section


Not sure if that works as you said the Config class itself should not need to 
be changed

> Hi,
> 
> Having solved my problem regarding setting up 'logger' such that it is
> accessible throughout my program (thanks to the help on this list), I
> now have problem related to a slightly similar issue.
> 
> My reading suggests that setting up a module with a Config class which
> can be imported by any part of the program might be a reasonable approach:
> 
> 
> import configparser
> 
> class Config:
> 
> def __init__(self, config_file):
> 
> config = configparser.ConfigParser()
> config.read(config_file)
>   
> 
> However, in my config file I am using sections, so 'config' is a dict of
> dicts.  Is there any cleverer generic way of initialising the class than
> 
> 
> self.config = config
> 
> 
> ?
> 
> This seems a bit clunky, because I'll end up with something like
> 
> 
>import config
>...
>c = config.Config(config_file)
>uids = get_uids(int(c.config["uids"]["minimum_uid"]))
> 
> 
> rather than something like, maybe
> 
> 
>uids = get_uids(int(c.minimum_uid))
> 
> 
> or
> 
> 
>uids = get_uids(int(c.uids_minimum_uid))
> 
> 
> So the question is: How can I map a dict of dicts onto class attributes
> in a generic way such that only code which wants to use a new
> configuration parameter needs to be changed and not the Config class
> itself?  Or should I be doing this differently?
> 
> Note that the values from ConfigParser are all strings, so I am fine
> with the attributes being strings - I'll just convert them as needed at
> the point of use (but maybe there is also a better way of handling that
> within a class).
>  
> Cheers,
> 
> Loris
>
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Problem with __sub__

2023-03-23 Thread David Raymond
I believe your problem is __rsub__, not __sub__.
When you havethen that 
uses the "r" version of the operators.

In your __rsub__ (used when you have  - ) you instead return 
 -  which is backwards.

Notice how the final return should also be -4,95 and not the +4,95 it's 
returning.

> If on the left side is '0' the result of a subtraction is wrong.
> 
> *b1 = Betragswert(500)
>  b2 = 0 + b1
>  b3 = 0 - b1
>  b4 = 5 + b1
>  b5 = 5 - b1*
> 
> print(b1, b2, b3, b4, b5) shows 5,00 5,00 5,00 5,05 4,95; the third
> value (b3) should be -5,00 (not 5,00).
> 
> Why is the substraction wrong?
>  def __rsub__(self, zweiter):
>  if not isinstance(zweiter, type(self)):
>  zweiter = Betragswert(zweiter)
>  return Betragswert(self._Betrag - zweiter._Betrag)
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Debugging reason for python running unreasonably slow when adding numbers

2023-03-15 Thread David Raymond
> Then I'm very confused as to how things are being done, so I will shut
> up. There's not enough information here to give performance advice
> without actually being a subject-matter expert already.

Short version: In this specific case "weights" is a 5,147 element list of 
floats, and "input" is a 10 element list of integers which has the indexes of 
the 10 elements in weights that he wants to add up.

sum_ = 0
for key in input:
sum_ += weights[key]

vs

sum_ = sum(weights[key] for key in input)

vs... other ways
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Debugging reason for python running unreasonably slow when adding numbers

2023-03-15 Thread David Raymond
> Or use the sum() builtin rather than reduce(), which was
> *deliberately* removed from the builtins. The fact that you can get
> sum() without importing, but have to go and reach for functools to get
> reduce(), is a hint that you probably shouldn't use reduce when sum
> will work.

Out of curiosity I tried a couple variations and am a little confused by the 
results. Maybe I'm having a brain fart and am missing something obvious?

Each of these was run with the same "data" and "perceptrons" values to keep 
that fair.
Times are averages over 150 iterations like the original.
The only thing changed in the trainPerceptron function was how to calculate sum_


Original:
sum_ = 0
for key in input:
v = weights[key]
sum_ += v
418ms

The reduce version:
sum_ = reduce(lambda acc, key: acc + weights[key], input)
758ms

Getting rid of the assignment to v in the original version:
sum_ = 0
for key in input:
sum_ += weights[key]
380ms

But then using sum seems to be slower

sum with generator expression:
sum_ = sum(weights[key] for key in input)
638ms

sum with list comprehension:
sum_ = sum([weights[key] for key in input])
496ms

math.fsum with generator expression:
sum_ = math.fsum(weights[key] for key in input)
618ms

math.fsum with list comprehension:
sum_ = math.fsum([weights[key] for key in input])
480ms


I'm not quite sure why the built-in sum functions are slower than the for loop,
or why they're slower with the generator expression than with the list 
comprehension.
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: How to escape strings for re.finditer?

2023-02-28 Thread David Raymond
> I wrote my previous message before reading this.  Thank you for the test you 
> ran -- it answers the question of performance.  You show that re.finditer is 
> 30x faster, so that certainly recommends that over a simple loop, which 
> introduces looping overhead.  

>>      def using_simple_loop(key, text):
>>      matches = []
>>      for i in range(len(text)):
>>      if text[i:].startswith(key):
>>      matches.append((i, i + len(key)))
>>      return matches
>>
>>      using_simple_loop: [0.1395295020792, 0.1306313000456, 
>> 0.1280345001249, 0.1318618002423, 0.1308461032626]
>>      using_re_finditer: [0.00386140005233, 0.00406190124297, 
>> 0.00347899970256, 0.00341310216218, 0.003732001273]


With a slight tweak to the simple loop code using .find() it becomes a third 
faster than the RE version though.


def using_simple_loop2(key, text):
matches = []
keyLen = len(key)
start = 0
while (foundSpot := text.find(key, start)) > -1:
start = foundSpot + keyLen
matches.append((foundSpot, start))
return matches


using_simple_loop: [0.1732664997689426, 0.1601669997908175, 
0.15792609984055161, 0.157397349591, 0.15759290009737015]
using_re_finditer: [0.003412699792534113, 0.0032823001965880394, 
0.0033694999292492867, 0.003354900050908327, 0.006998894810677]
using_simple_loop2: [0.00256159994751215, 0.0025471001863479614, 
0.0025424999184906483, 0.0025831996463239193, 0.002999018251896]
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Find 6-letter words that are hidden (embedded) within

2023-02-24 Thread David Raymond
> Find 6-letter words that are hidden (embedded) within each row of letters. 
> The letters are in the correct order. 
> 
> 1. JSOYOMFUBELR 
> 2. SCDUARWDRLYE 
> 3. DASNAGEFERTY 
> 4. CLULOOTSCEHN 
> 5. USENEARSEYNE

> The letters are in the correct order.  So this problem is not about 
> Anagraming.


You can get every combination of 6 letters out of it with 
itertools.combinations like below.
Just implement the isWord function to return whether a string actually counts 
as a legit word or not.
12 choose 6 is only 924 combinations to check, so shouldn't be too bad to check 
them all.


def isWord(word):
return True #Best left as an exercise to the reader

startWord = "JSOYOMFUBELR"
subLetterCount = 6

foundWords = set()

for letters in itertools.combinations(startWord, subLetterCount):
word = "".join(letters)
if word not in foundWords and isWord(word):
print(word)
foundWords.add(word)
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Parallel(?) programming with python

2022-08-08 Thread David Raymond
>> But, an easier and often
>> better option for concurrent data access is use a (relational)
>> database, then the appropriate transaction isolation levels
>> when reading and/or writing.
>>
>
> That would obviusly save some coding (but would introduce the need to
> code the interaction with the database), but I'm not sure it would speed
> up the thing. Would the RDBMS allow to read a table while something else
> is writing to it? I doubt it and I'm not sure it doesn't flush the cache
> before letting you read, which would include a normally slow disk access.

SQLite for example allows only 1 write transaction at a time, but in WAL mode 
you can have as many read transactions as you want all going along at the same 
time as that 1 writer. It also allows you to specify how thorough it is in 
flushing data to disk, including not forcing a sync to disk at all and just 
leaving that to the OS to do on its own time.
-- 
https://mail.python.org/mailman/listinfo/python-list


Pip upgrade causing issues in 3.10

2022-07-19 Thread David Raymond
So after a long while I'm finally getting around to upgrading to 3.10 on 
Windows from 3.9, and my first pip upgrade is causing issues with the 
installation.

Problem seems to be that I run pip from a command prompt in the Scripts folder, 
and it seems pip is trying to completely remove the Scripts folder, which it 
can't because the command prompt is in that folder. It successfully removes the 
old installation of pip before dying, leaving you without a pip to run. (Yay 
for ensurepip) It also creates a ~ripts folder in the main Python folder, and a 
few ~ip folders in the site-packages folder before it dies, which then cause 
warnings to be issued by pip from then on (or at least until you go and delete 
those folders)

Upgrading pip this way worked fine in 3.9.x, so I'm curious as to why the 
change for 3.10? And is there a chance to go back to it not trying to 
completely remove that Scripts folder? Or at least have it fail a little more 
gracefully without creating all those ~ folders? Maybe add a hint about 
ensurepip when it removes the old and can't install the new?

For those who will say just change how you update it: I can do that going 
forward, yes.
But nonetheless, running it like this probably shouldn't leave me without a 
pip, and with extra junk folders.



Fresh install here of 3.10.5. For all users, not including the py launcher 
thing. Below is running in an Admin Command Prompt.


C:\Program Files\Python310\Scripts>pip list
PackageVersion
-- ---
pip22.0.4
setuptools 58.1.0
WARNING: You are using pip version 22.0.4; however, version 22.1.2 is available.
You should consider upgrading via the 'C:\Program Files\Python310\python.exe -m 
pip install --upgrade pip' command.

C:\Program Files\Python310\Scripts>..\python.exe -m pip install --upgrade pip
Requirement already satisfied: pip in c:\program 
files\python310\lib\site-packages (22.0.4)
Collecting pip
  Using cached pip-22.1.2-py3-none-any.whl (2.1 MB)
Installing collected packages: pip
  Attempting uninstall: pip
Found existing installation: pip 22.0.4
Uninstalling pip-22.0.4:
ERROR: Could not install packages due to an OSError: [WinError 32] The process 
cannot access the file because it is being used by another process: 
'c:\\program files\\python310\\scripts\\'
Consider using the `--user` option or check the permissions.


At this point the Scripts folder is completely empty, there's a ~ripts folder 
in the main python310 folder, and a few ~ip... folders in the site-packages 
folder


C:\Program Files\Python310\Scripts>..\python.exe -m ensurepip
WARNING: Ignoring invalid distribution -ip (c:\program 
files\python310\lib\site-packages)
WARNING: Ignoring invalid distribution -ip (c:\program 
files\python310\lib\site-packages)
Looking in links: c:\Users\usernamehere\AppData\Local\Temp\tmpt48ibdf3
Requirement already satisfied: setuptools in c:\program 
files\python310\lib\site-packages (58.1.0)
Processing 
c:\users\usernamehere\appdata\local\temp\tmpt48ibdf3\pip-22.0.4-py3-none-any.whl
WARNING: Ignoring invalid distribution -ip (c:\program 
files\python310\lib\site-packages)
Installing collected packages: pip
WARNING: Ignoring invalid distribution -ip (c:\program 
files\python310\lib\site-packages)
Successfully installed pip-22.0.4

C:\Program Files\Python310\Scripts>cd..

C:\Program Files\Python310>python.exe -m pip install -U pip
WARNING: Ignoring invalid distribution -ip (c:\program 
files\python310\lib\site-packages)
WARNING: Ignoring invalid distribution -ip (c:\program 
files\python310\lib\site-packages)
Requirement already satisfied: pip in c:\program 
files\python310\lib\site-packages (22.0.4)
Collecting pip
  Using cached pip-22.1.2-py3-none-any.whl (2.1 MB)
WARNING: Ignoring invalid distribution -ip (c:\program 
files\python310\lib\site-packages)
Installing collected packages: pip
  Attempting uninstall: pip
WARNING: Ignoring invalid distribution -ip (c:\program 
files\python310\lib\site-packages)
Found existing installation: pip 22.0.4
Uninstalling pip-22.0.4:
  Successfully uninstalled pip-22.0.4
WARNING: Ignoring invalid distribution -ip (c:\program 
files\python310\lib\site-packages)
Successfully installed pip-22.1.2
WARNING: Ignoring invalid distribution -ip (c:\program 
files\python310\lib\site-packages)
WARNING: Ignoring invalid distribution -ip (c:\program 
files\python310\lib\site-packages)
WARNING: Ignoring invalid distribution -ip (c:\program 
files\python310\lib\site-packages)

C:\Program Files\Python310>cd Scripts

C:\Program Files\Python310\Scripts>pip list
WARNING: Ignoring invalid distribution -ip (c:\program 
files\python310\lib\site-packages)
PackageVersion
-- ---
pip22.1.2
setuptools 58.1.0
WARNING: Ignoring invalid distribution -ip (c:\program 
files\python310\lib\site-packages)
WARNING: Ignoring invalid distribution -ip (c:\program 
files\python310\lib\site-packages)
WARNING: Ignoring invalid distribution -ip (c:\program 
files\pytho

RE: Changing calling sequence

2022-05-12 Thread David Raymond
>>def TempsOneDay(*dateComponents):
>>if len(dateComponents) == 3:
>>year, month, date = dateComponents
>>elif len(dateComponents) == 1 and isinstance(dateComponents[0], 
>> datetime.date):
>>year, month, date = (dateComponents[0].year, dateComponents[0].month, 
>> dateComponents[0].day)
>>else:
>>raise Exception("Error message here")
>
>|>>> help( TempsOneDay )
>|Help on function TempsOneDay in module __main__:
>|
>|TempsOneDay(*dateComponents)


Then just add an appropriate docstring.

>>> def TempsOneDay(*dateComponents):
... """Can be called either with 3 arguments: year, month, day
...or with a single datetime.date object"""
... if len(dateComponents) == 3:
... year, month, date = dateComponents
... elif len(dateComponents) == 1 and isinstance(dateComponents[0], 
datetime.date):
... year, month, date = (dateComponents[0].year, 
dateComponents[0].month, dateComponents[0].day)
... else:
... raise Exception("Error message here")
...
>>> help(TempsOneDay)
Help on function TempsOneDay in module __main__:

TempsOneDay(*dateComponents)
Can be called either with 3 arguments: year, month, day
or with a single datetime.date object

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


RE: Changing calling sequence

2022-05-11 Thread David Raymond
>> I have a function that I use to retrieve daily data from a
>> home-brew database. Its calling sequence is;
>> 
>> def TempsOneDay( year, month, date ):
>> 
>> After using it (and its friends) for a few years, I've come to
>> realize that there are times where it would be advantageous to
>> invoke it with a datetime.date as its single argument.
>
>You could just use all keyword args:
>
>def TempsOneDay(**kwargs):
>
> if 'date' in kwargs:
> handle_datetime(kwargs['date'])
> elif 'year' in kwargs and 'month' in kwargs and 'day' in kwargs:
> handle_args(kwargs['year'], kwargs['month'], kwargs['day'])
> else:
> raise Exception("Bad keyword args")
>
>TempsOneDay(date=datetime.datetime.now)
>
>TempsOneDay(year=2022, month=11, day=30)
>

Maybe not the prettiest, but you could also define it like this, which also 
wouldn't require changing of any existing calls or the main body of the 
function past this if block.

def TempsOneDay(*dateComponents):
if len(dateComponents) == 3:
year, month, date = dateComponents
elif len(dateComponents) == 1 and isinstance(dateComponents[0], 
datetime.date):
year, month, date = (dateComponents[0].year, dateComponents[0].month, 
dateComponents[0].day)
else:
raise Exception("Error message here")
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: One-liner to merge lists?

2022-02-22 Thread David Raymond
> Is there a simpler way?

>>> d = {1: ['aaa', 'bbb', 'ccc'], 2: ['fff', 'ggg']}
>>> [a for b in d.values() for a in b]
['aaa', 'bbb', 'ccc', 'fff', 'ggg']
>>>

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


RE: frozenset can be altered by |=

2021-11-22 Thread David Raymond
>> (venv_3_10) marco@buzz:~$ python
>> Python 3.10.0 (heads/3.10-dirty:f6e8b80d20, Nov 18 2021, 19:16:18)
>> [GCC 10.1.1 20200718] on linux
>> Type "help", "copyright", "credits" or "license" for more information.
>> >>> a = frozenset((3, 4))
>> >>> a
>> frozenset({3, 4})
>> >>> a |= {5,}
>> >>> a
>> frozenset({3, 4, 5})
> 
> That's the same as how "x = 4; x += 1" can "alter" four into five.
> 
> >>> a = frozenset((3, 4))
> >>> id(a), a
> (140545764976096, frozenset({3, 4}))
> >>> a |= {5,}
> >>> id(a), a
> (140545763014944, frozenset({3, 4, 5}))
> 
> It's a different frozenset.
> 
> ChrisA

Another possible option is instead of

a |= {5,}

change it to

a.update({5,})

If a is a regular set it will update the original object, and if a is a 
frozenset it will raise an AttributeError. Which may not be what you want, but 
at least it won't quietly do something you weren't expecting.

It is a little confusing since the docs list this in a section that says they 
don't apply to frozensets, and lists the two versions next to each other as the 
same thing.

https://docs.python.org/3.9/library/stdtypes.html#set-types-set-frozenset

The following table lists operations available for set that do not apply to 
immutable instances of frozenset:

update(*others)
set |= other | ...

Update the set, adding elements from all others.
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: on writing a while loop for rolling two dice

2021-08-30 Thread David Raymond
> def how_many_times():
>   x, y = 0, 1
>   c = 0
>   while x != y:
> c = c + 1
> x, y = roll()
>   return c, (x, y)

Since I haven't seen it used in answers yet, here's another option using our 
new walrus operator

def how_many_times():
roll_count = 1
while (rolls := roll())[0] != rolls[1]:
roll_count += 1
return (roll_count, rolls)
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: How do you debug in Python? Coming from a Matlab and R user. I'm already aware of pdb.

2021-01-27 Thread David Raymond
In regards to the various comments about adding in print() calls what I've 
found myself doing is to basically always use the logging module, and use 
logging.debug() for those.

Somewhere at the top of the script I'll have a line like...

DEBUG = False

...and when initializing the handler to stdout I'll do something like this...

toScreen = logging.StreamHandler(sys.stdout)
toScreen.setLevel(logging.DEBUG if DEBUG else logging.INFO)


That way I can sprinkle in

logging.debug("Some message here")

in various spots. If things are going wrong I can change DEBUG to True to see 
them on screen, and when I've fixed it I can just set DEBUG back to False. That 
way I don't have to try finding all those print() statements that were only 
there for debugging and comment them out or remove them.

As a bonus, if you also set a file logger for example with its level set to 
logging.DEBUG, then you can have those go into the log file without them 
cluttering the screen output.

As a side effect to using logging I found I also like having the timestamp 
automatically prepended to my output by the logger. I'd find myself checking in 
on something I left running in the background and thinking it's been on that 
step for "a while", but I have no idea how long "a while" really is. So the 
timestamps help quickly show "been stuck there for 3 hours" vs "just got to 
that step 3 seconds before I switched to its window"

I don't claim these are the best practices :) But figured I'd offer another 
take on it.
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: primitive password cracker

2021-01-07 Thread David Raymond
I think you might want to check out itertools.product()
https://docs.python.org/3.9/library/itertools.html#itertools.product

import itertools
import string
passe = 'pass'
for p in itertools.product(string.ascii_lowercase, repeat = 4):
p = "".join(p)
if p == passe:
print("Found it:", p)
break
else:
print("Didn't find it.")

Or for different lengths:

foundIt = False
for repeat in range(1, 6):
for p in itertools.product(string.ascii_lowercase, repeat = repeat):
p = "".join(p)
if p == passe:
print("Found it:", p)
foundIt = True
break
if foundIt:
break
else:
print("Didn't find it.")
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Are there Python ways to execute queries on PostgreSQL without getting data over?

2020-10-19 Thread David Raymond


> Are there Python ways to execute queries on PostgreSQL without getting data 
> over?
> 
> Are there ways just to fire off PostgreSQL queries and not get data into 
> Python?
> 
> Regards,
> 
> David

What is your use case for this? Normally when you do basic things like an 
UPDATE/DELETE, etc all you get back is a row count of how many records the 
statement updated/deleted. Normally SELECT is the only thing to send a bunch of 
data back to you, and well, why would you run selects if you don't care about 
the output?

Without knowing what you're doing it for, the only other option I could suggest 
might be to use EXPLAIN ANALYZE. That would still send back the EXPLAIN ANALYZE 
output. But it will actually run the query, and if the output would have been 
thousands/millions of rows, then the explain output is going to be a lot 
smaller.
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: [RELEASE] Python 3.8.6 is now available

2020-09-29 Thread David Raymond
> Python 3.8.6 is the sixth maintenance release of Python 3.8. Go get it here:

> https://www.python.org/downloads/release/python-386/ 
> 


Just a quick note that there still seem to be a few places on the website which 
are still showing 3.8.5 as the latest release. (Looking at it with Firefox on 
Windows)

On the main page www.python.org it has 3.8.6 down in the latest news section, 
and a direct link in the little download box right above that. But if you hover 
over the big "Downloads" tab at the top it gives a big 3.8.5 button

If you click on that Downloads tab and go to www.python.org/downloads, again 
there's a big gold button for 3.8.5, and if you scroll down to the "Looking for 
a specific release?" section there is no 3.8.6 visible. The most recent line is 
3.5.10 from Sept 5th

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


RE: grouping and sorting within groups using another list

2020-09-02 Thread David Raymond
Would it be something as simple as:

rows.sort(key = lambda x: (x[0], x[3], x[4], sort_list.index(x[6])))



-Original Message-
From: Python-list  On 
Behalf Of Larry Martell
Sent: Wednesday, September 2, 2020 1:55 PM
To: Python 
Subject: grouping and sorting within groups using another list

I have a list of tuples, and I want to group them by 3 items (0, 3, 4)
and then within each group sort the data by a 4th item (6) using a
sort order from another list. The list is always ordered by the 3
grouping items.

For example, if I have this list:
rows =
[('a', 'b', 'c', 'd', 'e', 'f', 'blue', ),
 ('a', 'x', 'y', 'd', 'e', 'f', 'green', ),
 ('a', 'q', 'w', 'd', 'e', 'f', 'white', ),
 ('p', 'x', 'y', 'd', 'e', 'f', 'white', ),
 ('p', 'x', 'y', 'd', 'e', 'f', ' 'blue', ...),
 ('p', 'x', 'y', 'd', 'e', 'f', ' 'green', ...),
 ('z', 'x', 'y', 'm', 'n', 'o', 'green, ...),
 ('z', 'x', 'y', 'm', 'n', 'o', 'white, ...),
 ('z', 'x', 'y', 'm', 'n', 'o', 'blue, ...),
]

and I have a list:

sort_list = ['blue', 'white', 'green']

Then the result list would be:

[('a', 'b', 'c', 'd', 'e', 'f', 'blue', ),
 ('a', 'x', 'y', 'd', 'e', 'f', 'white', ),
 ('a', 'q', 'w', 'd', 'e', 'f', 'green', ),
 ('p', 'x', 'y', 'd', 'e', 'f', 'blue', ),
 ('p', 'x', 'y', 'd', 'e', 'f', ' 'white', ...),
 ('p', 'x', 'y', 'd', 'e', 'f', ' 'green', ...),
 ('z', 'x', 'y', 'm', 'n', 'o', 'blue, ...),
 ('z', 'x', 'y', 'm', 'n', 'o', 'white, ...),
 ('z', 'x', 'y', 'm', 'n', 'o', 'green, ...),
]

Been trying to do with using groupby but have not been successful.
-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Didn't understand the output of the following Python 3 code with reduce function?

2020-08-28 Thread David Raymond
All the numbers in the nums list don't matter and aren't used. Only the first 
number, and how many there are.
https://docs.python.org/3.8/library/functools.html#functools.reduce

Basically it's doing
ADDS(1, 2) which returns 2
that 2 gets fed back into
ADDS(2, 3) which returns 3
that 3 gets fed back into
ADDS(3, 4) which returns 4
...
ADDS(8, 9) which returns 9

> I have seen this code on one of competative programming site but I didn't get 
> it, Why output is 9?
> 
> from functools import *
> 
> def ADDS(a,b):
> return a+1
> nums = [1, 2, 3, 4, 5, 6, 7, 8, 9]
> add = reduce(ADDS, nums)
> print(add)
> 
> output: 9
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Final statement from Steering Council on politically-charged commit messages

2020-08-18 Thread David Raymond
> Do I agree with the PR, not exactly. However, I do think we as a community
> should be accommodating to people
> Whose use of the English language differs from the standard as long as the
> meaning is clear.

Remember that the problem isn't the change in wording of the PEP. That's all 
well and good and not an issue. We're on board with what you just said and on 
board with the wording change.

It's the commit message at the top (on the linked page) that's the issue. The 
commit message was not "hey, we are now being more accommodating and aware." 
The commit message was "hey, you're a racist white supremacist if you pick a 
specific standard."

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


RE: Python Scripts

2020-07-21 Thread David Raymond
Remember to reply-all, so that python-list is included and can still see 
responses and offer help.

If Python won't open them, then how do you know the scripts work? They work on 
someone else's computer you mean?

Please provide the basics then so we can try to help out.

What OS are you using?
How are you trying to start Python/run the scripts?
Are there any exceptions, error messages, etc when you try to run it?
Can you successfully run any other Python script, or is it just this one that's 
a problem?
What is in the script? (can be a summary to start with if the script is long)
What version of Python are you using?
Etc...


> no the scripts work but python wont open them
> 
>> im having problems when running python scripts
>> 
>> When running the scripts it always closes immediately

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


RE: Python Scripts

2020-07-21 Thread David Raymond
> im having problems when running python scripts
> 
> When running the scripts it always closes immediately

If you're running it in Windows, and running it by double clicking on a .py 
file, then it will pop up a console window while it's running, and then 
immediately close that window when the script is complete. So if the script is 
failing immediately or even succeeding quickly, then the window with all the 
output is closing immediately too.

If this is what's going on with you, you can open the console first and run the 
scripts from there. That way all their output will still be there for you to 
see when it's complete.

Alternatively as a temporary debugging solution you could surround the main 
part (or all) of the script in a try statement and in the except clause have a 
"hit enter to continue" bit to freeze it before it closes the window.


import traceback
try:
stuff
except:
traceback.print_exc()
raise
finally:
input("Hit Enter to close")

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


RE: how to let argument be optional falling back to certain integer

2020-06-22 Thread David Raymond


> This is true.  I have written 0 as false in C so many times.  But
> clearly for me times have changed...  I now look at numbers as a thing
> in their own special class not to be confused as truth-values.  (So much
> so that I fell for this.)  But I confess I still think of numbers as all
> TRUE.  (Even zero!)


Also remember that in Python bool is a subclass of int:
>>> isinstance(False, int)
True
>>> 0 == False
True
>>> 1 == True
True
>>> ["A", "B"][False]
'A'
>>> ["A", "B"][True]
'B'

So if you're trying to do something slightly different based on the type of the 
input you might fall into this trap

if isinstance(foo, float):
do float stuff
elif isinstance(foo, int):
do int stuff
elif isinstance(foo, bool):
this line will never run because it would have triggered the int line

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


RE: Is there anything in the script which could cause it to not run its full course please?

2020-05-04 Thread David Raymond
Not necessarily the cause of your problem, but if you're going to compare dates 
it should be as objects, or as text as year-month-day. Here you're comparing 
dates as text in day-month-year format. So January first  comes before May 
4th 2020

"01-01-" < "04-05-2020"

...
04-05-2020 09:30:00 40
04-05-2020 12:30:00 40
04-05-2020 15:30:00 40
04-05-2020 22:30:00 40
...
input_date,input_time,input_duration=date_time_duration.split(' ')
current_datetime = datetime.datetime.now()
current_date = current_datetime.strftime('%d-%m-%Y')
if(input_date>=current_date):
   while(True):
...
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Multiprocessing queue sharing and python3.8

2020-04-06 Thread David Raymond
Looks like this will get what you need.


def some_complex_function(x):
global q
#stuff using q

def pool_init(q2):
global q
q = q2

def main():
#initalize the Queue
mp_comm_queue = mp.Queue()

#Set up a pool to process a bunch of stuff in parallel
pool = mp.Pool(initializer = pool_init, initargs = (mp_comm_queue,))
...



-Original Message-
From: David Raymond 
Sent: Monday, April 6, 2020 4:19 PM
To: python-list@python.org
Subject: RE: Multiprocessing queue sharing and python3.8

Attempting reply as much for my own understanding.

Are you on Mac? I think this is the pertinent bit for you:
Changed in version 3.8: On macOS, the spawn start method is now the default. 
The fork start method should be considered unsafe as it can lead to crashes of 
the subprocess. See bpo-33725.

When you start a new process (with the spawn method) it runs the module just 
like it's being imported. So your global " mp_comm_queue2=mp.Queue()" creates a 
new Queue in each process. Your initialization of mp_comm_queue is also done 
inside the main() function, which doesn't get run in each process. So each 
process in the Pool is going to have mp_comm_queue as None, and have its own 
version of mp_comm_queue2. The ID being the same or different is the result of 
one or more processes in the Pool being used repeatedly for the multiple steps 
in imap, probably because the function that the Pool is executing finishes so 
quickly.

Add a little extra info to the print calls (and/or set up logging to stdout 
with the process name/id included) and you can see some of this. Here's the 
hacked together changes I did for that.

import multiprocessing as mp
import os

mp_comm_queue = None #Will be initalized in the main function
mp_comm_queue2 = mp.Queue() #Test pre-initalized as well

def some_complex_function(x):
print("proc id", os.getpid())
print("mp_comm_queue", mp_comm_queue)
print("queue2 id", id(mp_comm_queue2))
mp_comm_queue2.put(x)
print("queue size", mp_comm_queue2.qsize())
print("x", x)
return x * 2

def main():
global mp_comm_queue
#initalize the Queue
mp_comm_queue = mp.Queue()

#Set up a pool to process a bunch of stuff in parallel
pool = mp.Pool()
values = range(20)
data = pool.imap(some_complex_function, values)

for val in data:
print(f"**{val}**")
print("final queue2 size", mp_comm_queue2.qsize())

if __name__ == "__main__":
main()



When making your own Process object and stating it then the Queue should be 
passed into the function as an argument, yes. The error text seems to be part 
of the Pool implementation, which I'm not as familiar with enough to know the 
best way to handle it. (Probably something using the "initializer" and 
"initargs" arguments for Pool)(maybe)



-Original Message-
From: Python-list  On 
Behalf Of Israel Brewster
Sent: Monday, April 6, 2020 1:24 PM
To: Python 
Subject: Multiprocessing queue sharing and python3.8

Under python 3.7 (and all previous versions I have used), the following code 
works properly, and produces the expected output:

import multiprocessing as mp

mp_comm_queue = None #Will be initalized in the main function
mp_comm_queue2=mp.Queue() #Test pre-initalized as well

def some_complex_function(x):
print(id(mp_comm_queue2))
assert(mp_comm_queue is not None)
print(x)
return x*2

def main():
global mp_comm_queue
#initalize the Queue
mp_comm_queue=mp.Queue()

#Set up a pool to process a bunch of stuff in parallel
pool=mp.Pool()
values=range(20)
data=pool.imap(some_complex_function,values)

for val in data:
print(f"**{val}**")

if __name__=="__main__":
main()

- mp_comm_queue2 has the same ID for all iterations of some_complex_function, 
and the assert passes (mp_comm_queue is not None). However, under python 3.8, 
it fails - mp_comm_queue2 is a *different* object for each iteration, and the 
assert fails. 

So what am I doing wrong with the above example block? Assuming that it broke 
in 3.8 because I wasn’t sharing the Queue properly, what is the proper way to 
share a Queue object among multiple processes for the purposes of inter-process 
communication?

The documentation 
(https://docs.python.org/3.8/library/multiprocessing.html#exchanging-objects-between-processes
 
<https://docs.python.org/3.8/library/multiprocessing.html#exchanging-objects-between-processes>)
 appears to indicate that I should pass the queue as an argument to the 
function to be executed in parallel, however that fails as well (on ALL 
versions of python I have tried) with the error:

Traceback (most recent call last):
  File "test_multi.py", line 32, in 
main()
  File "test_multi.py", line 28, in m

RE: Multiprocessing queue sharing and python3.8

2020-04-06 Thread David Raymond
Attempting reply as much for my own understanding.

Are you on Mac? I think this is the pertinent bit for you:
Changed in version 3.8: On macOS, the spawn start method is now the default. 
The fork start method should be considered unsafe as it can lead to crashes of 
the subprocess. See bpo-33725.

When you start a new process (with the spawn method) it runs the module just 
like it's being imported. So your global " mp_comm_queue2=mp.Queue()" creates a 
new Queue in each process. Your initialization of mp_comm_queue is also done 
inside the main() function, which doesn't get run in each process. So each 
process in the Pool is going to have mp_comm_queue as None, and have its own 
version of mp_comm_queue2. The ID being the same or different is the result of 
one or more processes in the Pool being used repeatedly for the multiple steps 
in imap, probably because the function that the Pool is executing finishes so 
quickly.

Add a little extra info to the print calls (and/or set up logging to stdout 
with the process name/id included) and you can see some of this. Here's the 
hacked together changes I did for that.

import multiprocessing as mp
import os

mp_comm_queue = None #Will be initalized in the main function
mp_comm_queue2 = mp.Queue() #Test pre-initalized as well

def some_complex_function(x):
print("proc id", os.getpid())
print("mp_comm_queue", mp_comm_queue)
print("queue2 id", id(mp_comm_queue2))
mp_comm_queue2.put(x)
print("queue size", mp_comm_queue2.qsize())
print("x", x)
return x * 2

def main():
global mp_comm_queue
#initalize the Queue
mp_comm_queue = mp.Queue()

#Set up a pool to process a bunch of stuff in parallel
pool = mp.Pool()
values = range(20)
data = pool.imap(some_complex_function, values)

for val in data:
print(f"**{val}**")
print("final queue2 size", mp_comm_queue2.qsize())

if __name__ == "__main__":
main()



When making your own Process object and stating it then the Queue should be 
passed into the function as an argument, yes. The error text seems to be part 
of the Pool implementation, which I'm not as familiar with enough to know the 
best way to handle it. (Probably something using the "initializer" and 
"initargs" arguments for Pool)(maybe)



-Original Message-
From: Python-list  On 
Behalf Of Israel Brewster
Sent: Monday, April 6, 2020 1:24 PM
To: Python 
Subject: Multiprocessing queue sharing and python3.8

Under python 3.7 (and all previous versions I have used), the following code 
works properly, and produces the expected output:

import multiprocessing as mp

mp_comm_queue = None #Will be initalized in the main function
mp_comm_queue2=mp.Queue() #Test pre-initalized as well

def some_complex_function(x):
print(id(mp_comm_queue2))
assert(mp_comm_queue is not None)
print(x)
return x*2

def main():
global mp_comm_queue
#initalize the Queue
mp_comm_queue=mp.Queue()

#Set up a pool to process a bunch of stuff in parallel
pool=mp.Pool()
values=range(20)
data=pool.imap(some_complex_function,values)

for val in data:
print(f"**{val}**")

if __name__=="__main__":
main()

- mp_comm_queue2 has the same ID for all iterations of some_complex_function, 
and the assert passes (mp_comm_queue is not None). However, under python 3.8, 
it fails - mp_comm_queue2 is a *different* object for each iteration, and the 
assert fails. 

So what am I doing wrong with the above example block? Assuming that it broke 
in 3.8 because I wasn’t sharing the Queue properly, what is the proper way to 
share a Queue object among multiple processes for the purposes of inter-process 
communication?

The documentation 
(https://docs.python.org/3.8/library/multiprocessing.html#exchanging-objects-between-processes
 
)
 appears to indicate that I should pass the queue as an argument to the 
function to be executed in parallel, however that fails as well (on ALL 
versions of python I have tried) with the error:

Traceback (most recent call last):
  File "test_multi.py", line 32, in 
main()
  File "test_multi.py", line 28, in main
for val in data:
  File 
"/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/pool.py",
 line 748, in next
raise value
  File 
"/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/pool.py",
 line 431, in _handle_tasks
put(task)
  File 
"/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/connection.py",
 line 206, in send
self._send_bytes(_ForkingPickler.dumps(obj))
  File 
"/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/reduction.py",
 line 51, in dumps
cls(buf, protocol).dump(obj)
  File 
"/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiproces

RE: PEP Idea: Multi-get for lists/tuples and dictionaries (inspired in NumPy)

2020-03-19 Thread David Raymond
For dictionaries it'd even be more useful:
d = {
'first_name': 'Frances',
'last_name': 'Allen',
'email': 'fal...@ibm.com'
}
fname, lname = d[['first_name', 'last_name']]

Why not do this?

import operator
first_last = operator.itemgetter("first_name", "last_name")

fname, lname = first_last(d)


https://docs.python.org/3.8/library/operator.html#operator.itemgetter

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


RE: Please solve this problem

2020-03-09 Thread David Raymond
> It was a problem and it was solved.
> Check the second or third e-mail in the thread.
> 
> Thank you.

The first email was blank,

The second email was from the same person and just said "Rply if solved"

The third was a sarcastic reply to the blank emails with just: "Solved, answer 
is:"

The fourth was Wildman trying to helpfully let them know nothing came through.

And the fifth was you saying there was both a problem and a solution.

So where is either the problem or solution you speak of?
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: threading

2019-12-04 Thread David Raymond
100 increments happen very fast, and means each thread will probably complete 
before the main thread has even started the next one. Bump that up to 1_000_000 
or so and you'll probably trigger it.

I did a test with a print(x) at the start of test() to see what the number was 
when each thread kicked off, and the very first thread had got it up to 655,562 
by the time the second thread had started and gotten to that print statement.


-Original Message-
From: Python-list  On 
Behalf Of ast
Sent: Wednesday, December 4, 2019 10:18 AM
To: python-list@python.org
Subject: threading

Hi

An operation like x+=1 on a global variable x is not thread safe because 
there can be a thread switch between reading and writing to x.
The correct way is to use a lock

lock = threading.Lock

with lock:
 x+=1

I tried to write a program without the lock which should fail.
Here it is:

import threading

x = 0

def test():
 global x
 for i in range(100):
 x+=1

threadings = []

for i in range(100):
 t = threading.Thread(target=test)
 threadings.append(t)
 t.start()

for t in threadings:
 t.join()

print(x)

1

The result is always correct: 1
Why ?

Secondly, how the switch between threads is done by the processor ? Is 
there a hardware interrupt coming from a timer ?
-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: How to delay until a next increment of time occurs ?

2019-11-13 Thread David Raymond
I just used .perf_counter() as it's "a clock with the highest available 
resolution to measure a short duration"

The general simplified idea was run it whenever the time is 0 modulo your 
interval. So you ask how long until that next time.


Run whenever the time is 0 modulo .0003
Current time is 33.0143379
So I'm 0.0002379 into the cycle (33.0143379 % .0003)
And I've got .621 to sleep until the next mark (.0003 - .0002379)


-Original Message-
From: Python-list  On 
Behalf Of R.Wieser
Sent: Wednesday, November 13, 2019 1:00 PM
To: python-list@python.org
Subject: Re: How to delay until a next increment of time occurs ?

David,

> timeInterval = 0.0003
> time.sleep(timeInterval - (time.perf_counter() % timeInterval))

Possibly: any reason for the performance counter modulo the interval ?

Regards,
Rudy Wieser


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


RE: How to delay until a next increment of time occurs ?

2019-11-13 Thread David Raymond
Maybe something along the lines of this?

timeInterval = 0.0003

time.sleep(timeInterval - (time.perf_counter() % timeInterval))


-Original Message-
From: Python-list  On 
Behalf Of R.Wieser
Sent: Wednesday, November 13, 2019 11:12 AM
To: python-list@python.org
Subject: Re: How to delay until a next increment of time occurs ?

Skip,

> Take a look at threading.Timer.

Thanks.   It seems to solve the problem by calling it at the start of the 
executed code (instead of the end), but costs thread usage ...

Regards,
Rudy Wieser


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


RE: Syntax Suggestion: Pass Function Definition as Argument

2019-11-07 Thread David Raymond
Here is it rewritten using the proposal:
```
#Definition
def myFoo (str1, str2, foo, str = " "):
print( foo(str = str1), foo(str = str2) )


#Call
myFoo ("hello", "world!"):
str = list(str)[0].upper() + str[1:]
return str
```

Are you looking for multi-line lambdas?
Isn't this basically just defining a function... but without an argument list? 
How would that know what "str" is?

Since you can already define functions inside of functions, what functionality 
does this give you that you can't already do with something like this:

def myFoo(str1, str2, foo):
print(foo(str1), foo(str2))

def main():
print("Stuff here")

def f1(str):
str = list(str)[0].upper() + str[1:]
return str
myFoo("hello", "world", f1)

def f1(str): #yup, same name
str = str[:-1] + list(str)[-1].upper()
return str
myFoo("hello", "world", f1)

del f1

print("More stuff")

main()


Which results in:
Stuff here
Hello World
hellO worlD
More stuff

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


RE: How execute at least two python files at once when imported?

2019-11-06 Thread David Raymond
"Why is importing modules in parallel bad?"

In general I'd say that
"import foo"
is supposed to be there because you want the classes, functions, variables etc. 
in foo to be available in your current program. A module should never run a 
whole bunch of time consuming stuff when it's imported.

If you want to "run foo" rather than import it, then inside foo all the 
"running" code should be in a function which can be called later. If you want 
foo to be runnable directly you can then call that function from the classic if 
__name__ == "__main__": construct. That lets foo be importable, and lets you 
pick when you want the actual long stuff to run.

#In foo.py

def run_long_task():
long_stuff_here
if __name__ == "__main__":
#foo.py is being called directly, not imported
run_long_task()


So your main program should look something like:

import foo  #quick as just the definitions are processed
foo.run_long_task()

Or to "run" multiple other things at once it should look more like:

import threading  #multiprocessing, or other module here
import foo  #quick as just the definitions are processed
import bar  #quick as just the definitions are processed

kick off foo.run_long_task() as its own thread/process/task/etc
kick off bar.run_long_task() as its own thread/process/task/etc

wait for them to finish and process results, or do stuff while they're running


So again, "import" should never be used to "run" another file, just to "bring 
in the stuff from" another file. And any file designed to be imported should 
not run extra stuff during that import.

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


RE: Calculations and Variables

2019-10-31 Thread David Raymond
How are you getting any value at all? You are trying to do math on string 
values just like in your infinite loop question.
This should raise
TypeError: unsupported operand type(s) for /: 'str' and 'str'

Also, tips are usually given as percentages. Here with a tip value of 9 you're 
saying that the tip is 1/9 th of the bill, which is where the number difference 
is coming from. If someone entered 50 this is saying the tip is 1/50 th of the 
bill, etc.


-Original Message-
From: Python-list  On 
Behalf Of ferzan saglam
Sent: Thursday, October 31, 2019 2:46 PM
To: python-list@python.org
Subject: Calculations and Variables

The code below which I have written should print the result of 43.6 with the 
given values I have included at the end of this question, but for some odd 
reason I get the result of 44.44.


bill = (input("Enter the total cost of the meal: \n"))  

tip = (input("Enter how much the tip is: \n"))  

split = (input("Enter how many people there are: \n"))  

total = bill + (bill / tip) 

eachPay = total / split 
print("Each person will have to pay %.2f" % eachPay)



I am aiming for the result of 43.6, but somehow get the result of 44.44.
(meal cost: 200) (Tip: 9) (people: 5)

I seem to do the calculation below, but get different results each time.
Total * Percentage Amount / 100
-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Exception

2019-09-24 Thread David Raymond
I believe the idea is that previously, if you were handling an exception, and 
your handling code caused its own exception, then you would get no info about 
the original exception. So all useful information about the original problem 
would get lost because of an oopsie in the handling code. So it got changed to 
show both the original exception as well as the final one to be more 
useful/helpful.

The raise ... from None is basically you explicitly saying "I am 
 raising a different exception, so don't worry about the old one"

Or such is my limited understanding anyway.


-Original Message-
From: Python-list  On 
Behalf Of ast
Sent: Tuesday, September 24, 2019 9:04 AM
To: python-list@python.org
Subject: Exception

Hi

It is not clear to me why the following code
generates 2 exceptions, ZeroDivisionError and
ArithmeticError. Since ZeroDivisionError is
catched, it shoud not bubble out.

Found here:
https://www.pythonsheets.com/notes/python-new-py3.html

 >>> def func():
... try:
... 1 / 0
... except ZeroDivisionError:
... raise ArithmeticError
...
 >>> func()
Traceback (most recent call last):
   File "", line 3, in func
ZeroDivisionError: division by zero

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
   File "", line 1, in 
   File "", line 5, in func
ArithmeticError


There is a work around (python >= 3.3)

 >>> def func():
... try:
... 1 / 0
... except ZeroDivisionError:
... raise ArithmeticError from None
...
 >>> func()
Traceback (most recent call last):
   File "", line 1, in 
   File "", line 5, in func
ArithmeticError
-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Weird Python Bug

2019-09-13 Thread David Raymond
2 comments:

First: Deleting from a list while you're iterating over it is a bad idea. Your 
first iteration gives nums[0] which is 72. But then you delete that and (in 
effect) everything moves up. So now the 4 is in the nums[0] slot. Your second 
iteration returns nums[1] which is now the 82 meaning you skipped over the 4... 
etc etc etc.,

Second: You are altering the original list that you're passing to the function, 
you're not working on a copy of it. Make sure that that is what you want, and 
that you didn't intend to leave the original thing alone.
>>> nums = [72, 4, 82, 67, 67]
>>> odd_ones_out(nums)
[4, 67, 67]
>>> nums
[4, 67, 67]
>>>



-Original Message-
From: Python-list On Behalf Of CrazyVideoGamez
Sent: Friday, September 13, 2019 3:18 PM
To: python-list@python.org
Subject: Weird Python Bug

For some reason, if you put in the code

def odd_ones_out(numbers):
for num in numbers:
count = numbers.count(num)
if not count % 2 == 0:
for i in range(count):
numbers.remove(num)
return numbers

nums = [72, 4, 82, 67, 67]
print(odd_ones_out(nums))

For some reason, it outputs [4, 67, 67] when it should have deleted the 4 
because it was odd. Another interesting thing is that when you put print(num) 
in the for loop, the number 4 never shows up and neither does the last 67. Help!
-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Python/SQLite best practices

2019-08-05 Thread David Raymond
"What's the advantage of this over letting the connection object do
that for you? As the context manager exits, it will automatically
either commit or roll back. If you want to guarantee closing _as
well_, then you can do that, but you can at least use what already
exists."

After review I guess I should have phrased it more as a "here's what I've found 
for reference" rather than a "here's what _you_ should do"


Part of it is large use of the Command Line Interface for SQLite, and similar 
command line tools for other db's, which all work in autocommit mode by 
default, so that's how my brain is now wired to think about executing things.

The context manager transaction feature I can see using, and might actually 
start switching to it as it's explicit enough. Though oddly, __enter__ doesn't 
seem to actually begin a transaction, not even a deferred one. It's only 
__exit__ that either commits or rolls back.
(Eh, it'd "probably" be simple enough to subclass Connection so that __enter__ 
and __exit__ work properly no matter the isolation_level. Famous last words)

The implicit stuff I hated because it never seemed straightforward enough. 
Especially since there used to be implicit commits as well as implicit begins 
("Changed in version 3.6: sqlite3 used to implicitly commit an open transaction 
before DDL statements. This is no longer the case.") Maybe because I was new to 
both Python and SQLite at the time, but there was a lot of "stop doing hidden 
stuff I didn't tell you do" getting muttered, along with others like "why do I 
need to commit when I never did a begin?" The documentation on it is all of 1 
sentence, so there was a lot of trial an error going on.
"The Python sqlite3 module by default issues a BEGIN statement implicitly 
before a Data Modification Language (DML) statement (i.e. 
INSERT/UPDATE/DELETE/REPLACE)."


"(Also, I'd definitely use conn.commit() rather than
cur.execute("commit"), in case there's extra functionality in the
commit method.)"

True. I know for example that if you try to rollback when not in a transaction 
that cur.execute("rollback;") will raise an exception whereas conn.rollback() 
will quietly suppress it for you. So there might be similarly useful stuff in 
.commit()
sqlite3 is (almost) all C though, so there'd be noticeably more digging and 
decyphering required to check. (For me anyway)

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


RE: Python/SQLite best practices

2019-08-05 Thread David Raymond
Not a full expert, but some notes:


I believe the default Connection context manager is set up for the context to 
be a single transaction, with a commit on success or a rollback on a failure. 
As far as I know it does NOT close the connection on exiting the context 
manager. That only happens automatically when it's getting garbage 
collected/going out of scope/correct terminology that I can't seem to remember.


For transactions and general use I vastly prefer using "isolation_level = None" 
when creating my connections, and then explicitly issuing all begin, commit, 
and rollback commands with cur.execute("begin;"), conn.commit(), 
conn.rollback() etc.


contextlib.closing() can be used to wrap cursors for use with with
(and also connections if they are created with isolation_level = None)

with contextlib.closing(sqlite3.connect(fi, isolation_level = None)) as conn:
conn.row_factory = sqlite3.Row
with contextlib.closing(conn.cursor()) as cur:
cur.execute("begin;")
stuff
conn.commit()



Normally though my stuff tends to look like the below (for better or for worse):

conn = sqlite3.connect(fi, isolation_level = None)
try:
conn.row_factory = sqlite3.Row
with contextlib.closing(conn.cursor()) as cur:
cur.execute("standalone query not needing an explicit transaction;")
stuff
cur.execute("begin;")
multiple queries that needed the explicit transaction
stuff
cur.execute("commit;")
except something bad:
blah
finally:
conn.rollback()
conn.close()



-Original Message-
From: Python-list  On 
Behalf Of Dave via Python-list
Sent: Monday, August 05, 2019 1:49 PM
To: python-list@python.org
Subject: Python/SQLite best practices

I'm looking for some tips from experienced hands on on this subject. 
Some of the areas of interest are (feel free to add more):

* Passing connections and cursors - good, bad indifferent?  I try to 
avoid passing file handles unless necessary, so I view connections and 
cursors the same.  Though that said, I'm not aware of any specific 
problems in doing so.

For designs with multiple tables:
* Better to pass an sql string to functions that create/add 
data/update/delete data and pass them to create, insert, update, delete 
functions; or have those functions for each table?  Taking table 
creation for example, if there are five tables, and the sql string is 
passed, there would need to be six functions to do it, though the 
complexity of each function may be reduced a little.  [table1create with 
sql and establishing a cursor, to table5create and then a function that 
executes the sql].

Best way to establish the connection and cursor, as well as close them? 
I have seen many ways to do this, and know that the with block can be 
used to create a connection and close it automatically, but the same is 
not true of the cursor.  Also, using only a with block does not handle 
any errors as well as a try/with.  For example:

 |   try:
 |  # Use with block to create connection – it will close self.
 |   with sqlite3.connect(path) as conn:
 |   cur = conn.cursor()
 |   cur.execute(sql_ProjectsTable)
 |   cur.close()
 |   except Error as e:
 |   print(e)

What else?

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


RE: Xml File Error

2019-07-30 Thread David Raymond
Is that the correct path to the file, without any typos?

os.path.abspath and os.path.join don't do any checking on whether their 
resulting path exists. So if there is a typo or error in your path it doesn't 
get reported until you actually try and open it by running ElementTree.parse

You can check if your path is ok using either os.path.exists() or 
os.path.isfile() before trying to actually open it to see if it is already 
there.



-Original Message-
From: Python-list  On 
Behalf Of Dipangi Shah
Sent: Tuesday, July 30, 2019 3:20 AM
To: python-list@python.org
Subject: Xml File Error

Hi,

import os from xml.etree import ElementTree
file_name = 'Users.xml' full_file = os.path.abspath(os.path.join('data',
file_name)) print(full_file) with above code, path is successfully printed
as "C:\Users\Evosys\PycharmProjects\Python Level1\data\Users.xml" but when
I add below 2 lines, there is an error saying "FileNotFoundError: [Errno 2]
No such file or directory: 'C:\\Users\\Evosys\\PycharmProjects\\Python
Level1\\data\\Users.xml' dom = ElementTree.parse(full_file,None) print(dom) Can
you suggest to me why I am facing the error?

-- 
Regards,

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


RE: Boolean comparison & PEP8

2019-07-29 Thread David Raymond
I think the other part of the discussion to be had here is: how do you name 
your booleans?

As with other things, "x", and "greeting" aren't really the best names. 
"greeting" sounds like it should hold what the greeting _is_, not whether or 
not there _should be_ a greeting. If it were something like "greetingRequired" 
or "greetingRequested" then it gets to be more readable as to what you're 
storing and looking for with the variable.

if greeting:
something...

vs.

if greetingRequired:
something...

I think the "greetingRequired" version is much more readable. To me the name of 
a boolean variable should be obvious that it's a boolean, be it as a statement 
or as an instruction. Looking at the arguments for subprocess.popen for 
example, lets put some of the boolean arguments behind an if and see how 
readable they are:

if start_new_session:
#instruction, I need to "start (a) new session"

if close_fds:
#instruction, I need to "close file descriptors"

if restore_signals:
#instruction, I need to "restore signals"

if shell:
#wait, "shell" is not really a statement or an instruction. "If shell"... 
what? "I need to shell"? Is this whether we want to use a shell, or if we 
discovered that we're already in one? Or is "shell" not really a boolean, and 
is holding which shell we want, and we're just using the "truthyness" to make 
sure it's not None? What's going on here? Dangit, ok, where's the documentation?
(subprocess is common enough that people know this, but imagine a much less 
frequently used module/function)


Do I have a point or am I just smoking crack? Does anyone else have examples?



-Original Message-
From: Python-list  On 
Behalf Of Jonathan Moules
Sent: Sunday, July 28, 2019 7:56 AM
To: python-list@python.org
Subject: Boolean comparison & PEP8

Hi List,
Lets say I want to know if the value of `x` is bool(True).
My preferred way to do it is:

if x is True:
     pass

Because this tests both the value and the type.

But this appears to be explicitly called out as being "Worse" in PEP8:

"""
Don't compare boolean values to True or False using ==.

Yes:   if greeting:
No:    if greeting == True:
Worse: if greeting is True:
"""

Why?

If `x` can also have a value of "1"(str) or 1(int) then in both cases 
this would be a false positive if I were to do the below as they'll both 
equate to True:

if x:
     pass

The PEP for boolean type (285 - 
https://www.python.org/dev/peps/pep-0285/) doesn't mention the "is" 
comparison keyword at all as far as I can tell.

What am I missing?
Thanks



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


RE: Threading Keyboard Interrupt issue

2019-05-29 Thread David Raymond
That's a little weird, and my running it works slightly differently. Please 
paste exactly what you're running (no time import and true being lowercase for 
example means we couldn't copy and paste it and have it immediately run)

In your script, the main thread hits y.start() which completes successfully as 
soon as the new thread gets going, so it exits the try/except block as a 
success. Then since there's no more code, the main thread completes.

The loop thread you started inherits the daemon-ness of the thread that called 
it, so by default it's started as a regular thread, and not a daemon thread. As 
a regular thread it will keep going even when the main thread completes.

Keyboard interrupts are only received by the main thread, which in this case 
completes real quick.

So what happens for me is that the main thread runs to completion instantly and 
leaves nothing alive to receive the keyboard interrupt, which means the loop 
thread will run forever until killed externally. (Task manager, ctrl-break, etc)

In this case, even if the main thread _was_ still alive to catch the keyboard 
interrupt, that exception does not get automatically passed to all threads, 
only the main one. So the main thread would have to catch the exception, then 
use one of the available signaling mechanisms to let the other threads know, 
and each of those other threads would have to consciously check for your signal 
of choice to see if the main thread wanted them to shut down.

Or, the other threads would have to be declared as demonic before they were 
started, in which case they would be killed automatically once all non-daemonic 
threads had ended.


-Original Message-
From: Python-list 
[mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of 
nihar Modi
Sent: Wednesday, May 29, 2019 4:39 AM
To: python-list@python.org
Subject: Threading Keyboard Interrupt issue

I have written a simple code that involves threading, but it does not go to
except clause after Keyboard interrupt. Can you suggest a way out. I have
pasted the code below. It does not print 'hi' after keyboard interrupt and
just stops.

import threading

def loop():
 while true:
  print('hello')
  time.sleep(5)

if __name__ == '__main__':
 try:
  y = threading.Thread(target = loop, args = ())
  y.start()
 except KeyboardInterrupt:
  print('hi')

The program does not print hi and terminates immediately after ctrl+c
-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: More CPUs doen't equal more speed

2019-05-23 Thread David Raymond
You really need to give more info on what you're doing in doit() to know what's 
going on. Are you using subprocess, threading, multiprocessing, etc?

Going off of what you've put there those nested for loops are being run in the 
1 main thread. If doit() kicks off a program and doesn't wait for it to finish, 
then you're just instantly starting 1,200 versions of the external program. If 
doit() _does_ wait for it to finish then you're not doing anything different 
than 1,200 one-at-a-time calls with no parallelization.

How are you making sure you have CPU_COUNT versions running, only that many 
running, and kicking off the next one once any of those completes?



-Original Message-
From: Python-list 
[mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of 
Bob van der Poel
Sent: Thursday, May 23, 2019 2:40 PM
To: Python
Subject: More CPUs doen't equal more speed

I've got a short script that loops though a number of files and processes
them one at a time. I had a bit of time today and figured I'd rewrite the
script to process the files 4 at a time by using 4 different instances of
python. My basic loop is:

for i in range(0, len(filelist), CPU_COUNT):
for z in range(i, i+CPU_COUNT):
doit( filelist[z])

With the function doit() calling up the program to do the lifting. Setting
CPU_COUNT to 1 or 5 (I have 6 cores) makes no difference in total speed.
I'm processing about 1200 files and my total duration is around 2 minutes.
No matter how many cores I use the total is within a 5 second range.

This is not a big deal ... but I really thought that throwing more
processors at a problem was a wonderful thing :) I figure that the cost of
loading the python libraries and my source file and writing it out are
pretty much i/o bound, but that is just a guess.

Maybe I need to set my sights on bigger, slower programs to see a
difference :)

-- 

 Listen to my FREE CD at http://www.mellowood.ca/music/cedars 
Bob van der Poel ** Wynndel, British Columbia, CANADA **
EMAIL: b...@mellowood.ca
WWW:   http://www.mellowood.ca
-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: PYTHONHASHSEED VALUE

2019-05-14 Thread David Raymond
What is the test case actually checking for, and why?

One of the main aspects of a set is that it's unordered. So something checking 
to make sure the order of an unordered object is some static value seems like 
it's testing for the wrong thing.


-Original Message-
From: Python-list 
[mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of 
Rishabh Mishra
Sent: Tuesday, May 14, 2019 5:47 AM
To: python-list@python.org
Subject: PYTHONHASHSEED VALUE

Dear Sir/Ma'am,

I was upgrading a codebase from python2 to python3. The testcases fail in
python3 code because the set() function produces a different order from
python2.

e.g. set in py2 ={"it","is","rishabh","Mishra"}

Here in python 2.7 the PYTHONHASHSEED is disabled.

in py3 set ={"rishabh","it","is","mishra"}

I want the order to stay consistent with python2.7 to pass the test cases.

In python 3.3 above PYTHONHASHSEED=0 disbales the random shuffling but it
doesn't yield the same order as 2.7

So can you tell me for what value of PYTHONHASHSEED the order will stay
consistent?

Thank-You!

Yours sincerely,
Rishabh Mishra
-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: break out of a program

2019-04-19 Thread David Raymond
The normal way of saying you want to exit the whole program is with sys.exit()
https://docs.python.org/3.7/library/sys.html#sys.exit

You can optionally give it the return code/exit status you want the interpreter 
to give to the OS when it exits.


-Original Message-
From: Python-list 
[mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of 
Tamara Berger
Sent: Friday, April 19, 2019 10:38 AM
To: python-list@python.org
Subject: 

Hi Python-List,

What code can I use to break out of a program completely, and not just out
of a loop? I wrote code with 3 conditions for saving for a downpayment. The
first addresses cases that don't meet the minimum condition; i.e., enough
money to save for a downpayment within the allotted time. It has its own
print line, but also executes the irrelevant print lines for the other two
conditions.

Thanks,
Tamara
-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: I want understand how this word wrap program playing on input

2019-04-04 Thread David Raymond
> Yep, spotted that too! :-) BTW, your fix also a bug: the last word on a
> line won't be followed by a space (off-by-one). The easiest fix for that
> is to add 1 to line_length initially, another little trick.

> Or, equivalently, to reset current_line_length to -1, which is an elegant 
> hack.

I'm actually going to stand my ground on this one. The incrementing of the line 
length happens _after_ you've added that word to the line, and the check for 
length is checking against the length of the new word. So adding the 1 for "the 
space at the end" won't come into effect until the next word, where you're 
asking "if I add this new word..." at which point you'll want to have included 
that space.


So copy and paste wrap to wrap2 and make your changes to wrap2, then run 
something like this, playing with the line length to check. The pipes show the 
last allowed position of a character.



def wrap(text, line_length):
"""Wrap a string to a specified line length
Original Version"""
words = text.split()
lines_of_words = []
current_line_length = line_length

for word in words:
if current_line_length + len(word) > line_length:
lines_of_words.append([]) # new line
current_line_length = 0
lines_of_words[-1].append(word)
current_line_length += len(word)

lines = [' '.join(line_of_words) for line_of_words in lines_of_words]
return '\n'.join(lines)

def wrap2(text, line_length):
"""Wrap a string to a specified line length
Altered Version"""
words = text.split()
lines_of_words = []
current_line_length = line_length

for word in words:
if current_line_length + len(word) > line_length:
lines_of_words.append([]) # new line
current_line_length = 0
lines_of_words[-1].append(word)
current_line_length += len(word) + 1

lines = [' '.join(line_of_words) for line_of_words in lines_of_words]
return '\n'.join(lines)

foo = "a b antidisestablishmentarianism c d e f g h i j k l m n o p queue are 
ess tee you vee doubleyou ecks why zee"

for wrapWidth in range(12, 19):
print(f"Width = {wrapWidth:d}")
showWidth = " " * (wrapWidth - 1) + "|"
print(showWidth)
print(wrap(foo, wrapWidth))
print(showWidth)
print(wrap2(foo, wrapWidth))
print(showWidth)
print("\n")
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: I want understand how this word wrap program playing on input

2019-04-04 Thread David Raymond
The function is constructing a list of the lines, which it will combine at the 
end. Answering the questions in reverse order:

3. Also why that `if` test is required there.
The if statement is saying "I don't have room on my current line for the next 
word, so time to start a new line"

2. In the if clause why `current_line_length` is set back to 0 every time the 
condition is true.
current_line_length is, well, the length of the current line. Where it's set to 
0 is when you've just started a new line, so it's saying "I'm now on a new line 
and have used up 0 characters so far on that line"

1. why  `line_length` is set to `current_line_length` initially?
lines_of_words starts out empty. Setting current_line_length to line_length 
triggers the "hey, I need to make a new line" on the first word, effectively 
creating the first line.



Also, I feel like there's a bug in this, where

current_line_length += len(word)

should be...

current_line_length += (len(word) + 1)

Otherwise the line length doesn't count the spaces. And a lot of little words 
will result in longer lines than you asked for.


-Original Message-
From: Python-list 
[mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of 
Arup Rakshit
Sent: Thursday, April 04, 2019 2:33 PM
To: Python
Subject: I want understand how this word wrap program playing on input 

I am reading a Python book, where the author used a simple word wrap program to 
explain another concept. But I am not understanding some parts of the program.

def wrap(text, line_length):
"""Wrap a string to a specified line length"""
words = text.split()
lines_of_words = []
current_line_length = line_length

for word in words:
if current_line_length + len(word) > line_length:
lines_of_words.append([]) # new line
current_line_length = 0
lines_of_words[-1].append(word)
current_line_length += len(word)

lines = [' '.join(line_of_words) for line_of_words in lines_of_words]
return '\n'.join(lines)

wealth_of_nations = "The annual labour of every nation is the fund which 
or" \
"iginally supplies it with all the necessaries and conveniencies of life 
wh" \
"ich it annually consumes, and which consist always either in the 
immediate" \
" produce of that labour, or in what is purchased with that produce from 
ot" \
"her nations. According, therefore, as this produce, or what is purchased 
w" \
"ith it, bears a greater or smaller proportion to the number of those who 
a" \
"re to consume it, the nation will be better or worse supplied with all 
the" \
" necessaries and conveniencies for which it has occasion."

Now when I call it:

python3 -i wrapper.py 
>>> wrap(wealth_of_nations, 25)
'The annual labour of every\nnation is the fund which\noriginally supplies 
it with\nall the necessaries and\nconveniencies of life which\nit annually 
consumes, and\nwhich consist always either\nin the immediate produce of\nthat 
labour, or in what is\npurchased with that produce\nfrom other 
nations.\nAccording, therefore, as\nthis produce, or what is\npurchased with 
it, bears a\ngreater or smaller\nproportion to the number of\nthose who are to 
consume it,\nthe nation will be better or\nworse supplied with all 
the\nnecessaries and\nconveniencies for which it\nhas occasion.'
>>> print(_)
The annual labour of every
nation is the fund which
originally supplies it with
all the necessaries and
conveniencies of life which
it annually consumes, and
which consist always either
in the immediate produce of
that labour, or in what is
purchased with that produce
from other nations.
According, therefore, as
this produce, or what is
purchased with it, bears a
greater or smaller
proportion to the number of
those who are to consume it,
the nation will be better or
worse supplied with all the
necessaries and
conveniencies for which it
has occasion.

My questions are:

1. why  `line_length` is set to `current_line_length` initially?
2. In the if clause why `current_line_length` is set back to 0 every time the 
condition is true.
3. Also why that `if` test is required there.

Can anyone help me to understand this program, I am totally confused.

Thanks,

Arup Rakshit
a...@zeit.io



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


RE: Losing words

2019-04-01 Thread David Raymond
https://docs.python.org/3.7/library/socket.html#socket.socket.send

.send returns the number of bytes that it actually succeeded in sending. From 
the docs: "Applications are responsible for checking that all data has been 
sent; if only some of the data was transmitted, the application needs to 
attempt delivery of the remaining data."

You could also switch to using .sendall, which will do retries for you.

But in either case you get a return code which lets you know if everything went 
through ok.


-Original Message-
From: Python-list 
[mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of 
John Doe
Sent: Monday, April 01, 2019 9:39 AM
To: python-list@python.org
Subject: Losing words

I'm learning SOCKETS and working with Irc.
 ---
 s.send(bytes("PRIVMSG " + channel +" "+ message + "\n", "UTF-8"))
 
 When more than one word ( for example: This is a message) 
 in *message* it sends the FIRST word only "This" and skips the rest.
 Any ideas how to solve the problem? I was seating on it on the night
 but nothing came up.
-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: What is the difference between "ws.Messagebeep(1)" and "ws.Messagebeep(-1)" ?

2019-03-21 Thread David Raymond
I'm assuming by ws you mean winsound?

Check the docs: https://docs.python.org/3.7/library/winsound.html




winsound.MessageBeep(type=MB_OK)

Call the underlying MessageBeep() function from the Platform API. This 
plays a sound as specified in the registry. The type argument specifies which 
sound to play; possible values are -1, MB_ICONASTERISK, MB_ICONEXCLAMATION, 
MB_ICONHAND, MB_ICONQUESTION, and MB_OK, all described below. The value -1 
produces a “simple beep”; this is the final fallback if a sound cannot be 
played otherwise. If the system indicates an error, RuntimeError is raised.


winsound.MB_ICONASTERISK

Play the SystemDefault sound.

winsound.MB_ICONEXCLAMATION

Play the SystemExclamation sound.

winsound.MB_ICONHAND

Play the SystemHand sound.

winsound.MB_ICONQUESTION

Play the SystemQuestion sound.

winsound.MB_OK

Play the SystemDefault sound.


-Original Message-
From: Python-list 
[mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of 
Steve
Sent: Thursday, March 21, 2019 10:00 AM
To: python-list@python.org
Subject: What is the difference between "ws.Messagebeep(1)" and 
"ws.Messagebeep(-1)" ?

Also: What is the code for other tones that I can call?

 

 

Footnote:

When someone asks "A penny for your thoughts" and you give your 2c worth,  

I wonder what happens to that other penny?

TTKMAWAN

 

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


RE: System Beep?

2019-03-08 Thread David Raymond
I believe you added an extra D there. winsound, not winDsound



-Original Message-
From: Steve [mailto:Gronicus@SGA.Ninja] 
Sent: Friday, March 08, 2019 3:16 PM
To: David Raymond; python-list@python.org
Subject: RE: System Beep?

= RESTART: C:\Gork\Med Insulin codes\MedReminder 127.py
=
Traceback (most recent call last):
  File "C:\Gork\Med Insulin codes\MedReminder 127.py", line 13, in

windsound.Beep  #(frequency, duration)
NameError: name 'windsound' is not defined

Something missing in my code?


98% of lawyers give the other 2% a bad name.


-Original Message-
From: Python-list  On
Behalf Of David Raymond
Sent: Friday, March 8, 2019 3:02 PM
To: python-list@python.org
Subject: RE: System Beep?

Windows has the built in winsound module that lets you set frequency and
duration, not sure about other OS's.

https://docs.python.org/3.7/library/winsound.html


-Original Message-
From: Python-list
[mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf
Of Steve
Sent: Friday, March 08, 2019 1:14 PM
To: python-list@python.org
Subject: System Beep?

How can I cause a system beep using code?
This is using the computer's internal speaker, not an audio external
speaker.
Do I have control of duration or volume?

Steve





Footnote:
Every minute, every single day, the equivalent of a truckload of plastic
enters our oceans.


--
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: System Beep?

2019-03-08 Thread David Raymond
Windows has the built in winsound module that lets you set frequency and 
duration, not sure about other OS's.

https://docs.python.org/3.7/library/winsound.html


-Original Message-
From: Python-list 
[mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of 
Steve
Sent: Friday, March 08, 2019 1:14 PM
To: python-list@python.org
Subject: System Beep?

How can I cause a system beep using code?
This is using the computer's internal speaker, not an audio external
speaker.
Do I have control of duration or volume?

Steve





Footnote:
Every minute, every single day, the equivalent of a truckload of plastic
enters our oceans.


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


RE: in a pickle

2019-03-06 Thread David Raymond
https://docs.python.org/3.7/library/pickle.html#pickling-class-instances 
includes 2 notes, which I think are the relevant ones:

When a class instance is unpickled, its __init__() method is usually not 
invoked. The default behaviour first creates an uninitialized instance and then 
restores the saved attributes.

At unpickling time, some methods like __getattr__(), __getattribute__(), or 
__setattr__() may be called upon the instance. In case those methods rely on 
some internal invariant being true, the type should implement __getnewargs__() 
or __getnewargs_ex__() to establish such an invariant; otherwise, neither 
__new__() nor __init__() will be called.


So I think that without using the various dunders you can't rely on the bits 
being restored in a specific order.

So at the point in the restoration where you're getting the exception, 
self.shape hasn't been restored yet while it's trying to set self[1], which is 
why trying to print self.shape is failing. Here's a modified version with more 
prints to show some of that:


import pickle
class test(dict):
def __init__(self, keys, shape = None):
print("__init__ is running")
print("keys:", keys)
print("shape:", shape)

self.shape = shape
for key in keys:
self[key] = None

def __setitem__(self, key, val):
print("__setitem__ is running")
print("self:", self)
print("key:", key)
print("val:", val)

try:
print("self.shape:", self.shape)
except AttributeError as err:
print("AttributeError:", err)

dict.__setitem__(self, key, val)

x = test([1, 2, 3])
print(x, x.shape)
s = pickle.dumps(x)
print("\n---About to load it---\n")
y = pickle.loads(s)
print(".loads() complete")
print(y, y.shape)



This is what that outputs:


__init__ is running
keys: [1, 2, 3]
shape: None
__setitem__ is running
self: {}
key: 1
val: None
self.shape: None
__setitem__ is running
self: {1: None}
key: 2
val: None
self.shape: None
__setitem__ is running
self: {1: None, 2: None}
key: 3
val: None
self.shape: None
{1: None, 2: None, 3: None} None

---About to load it---

__setitem__ is running
self: {}
key: 1
val: None
AttributeError: 'test' object has no attribute 'shape'
__setitem__ is running
self: {1: None}
key: 2
val: None
AttributeError: 'test' object has no attribute 'shape'
__setitem__ is running
self: {1: None, 2: None}
key: 3
val: None
AttributeError: 'test' object has no attribute 'shape'
.loads() complete
{1: None, 2: None, 3: None} None



Alas, I can't offer any help with how to use __getnewargs__() or the other 
dunders to properly handle it.



-Original Message-
From: Python-list 
[mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of 
duncan smith
Sent: Wednesday, March 06, 2019 11:14 AM
To: python-list@python.org
Subject: in a pickle

Hello,
  I've been trying to figure out why one of my classes can be
pickled but not unpickled. (I realise the problem is probably with the
pickling, but I get the error when I attempt to unpickle.)

A relatively minimal example is pasted below.


>>> import pickle
>>> class test(dict):
def __init__(self, keys, shape=None):
self.shape = shape
for key in keys:
self[key] = None

def __setitem__(self, key, val):
print (self.shape)
dict.__setitem__(self, key, val)


>>> x = test([1,2,3])
None
None
None
>>> s = pickle.dumps(x)
>>> y = pickle.loads(s)
Traceback (most recent call last):
  File "", line 1, in 
y = pickle.loads(s)
  File "", line 8, in __setitem__
print (self.shape)
AttributeError: 'test' object has no attribute 'shape'


I have DUCkDuckGo'ed the issue and have tinkered with __getnewargs__ and
__getnewargs_ex__ without being able to figure out exactly what's going
on. If I comment out the print call, then it seems to be fine. I'd
appreciate any pointers to the underlying problem. I have one or two
other things I can do to try to isolate the issue further, but I think
the example is perhaps small enough that someone in the know could spot
the problem at a glance. Cheers.

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


RE: confusion with os.chmod() and follow_symlinks

2019-02-22 Thread David Raymond
Not sure, but the way I read it follow_symlinks = True is the default behavior 
of systems that don't allow you to set it, and being able to set it to False is 
the special bit. So "allowing follow_symlinks" means it "allows you to change 
it to whatever you want", not "allows it to be True"

Under https://docs.python.org/3.7/library/os.html#files-and-directories it even 
lists the bullet point as _not_ following symlinks


-Original Message-
From: Python-list 
[mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of 
Karsten Hilbert
Sent: Friday, February 22, 2019 3:46 PM
To: python-list@python.org
Subject: Re: confusion with os.chmod() and follow_symlinks

On Fri, Feb 22, 2019 at 09:21:07PM +0100, Karsten Hilbert wrote:

> Am I confused ?
> 
>   ncq@hermes:~$ python3
>   Python 3.7.2+ (default, Feb  2 2019, 14:31:48)
>   [GCC 8.2.0] on linux
>   Type "help", "copyright", "credits" or "license" for more information.
>   >>> import os
>   >>> print(os.supports_follow_symlinks)
>   {, ,  function access>, , }
>   >>> os.chmod in os.supports_follow_symlinks
>   False
>   >>> os.chmod('/tmp/test', 0o0700, follow_symlinks = False)
>   Traceback (most recent call last):
> File "", line 1, in 
>   NotImplementedError: chmod: follow_symlinks unavailable on this platform
>   >>>
> 
> I would only have expected this exception when I actually
> request the unavailable functionality, like so:
> 
>   os.chmod('/tmp/test', 0o0700, follow_symlinks = True)
> 
> This, however, works:
> 
>   os.chmod('/tmp/test', 0o0700)
> 
> DESPITE the documentation saying
> 
>   os.chmod(path, mode, *, dir_fd=None, follow_symlinks=True)
> 
> IOW, the default for  being "True", which is
> certainly illogical to succeed when it is not even supported
> on this platform.

Because, naively, I'd have assumed this to work:

os.chmod(directory, mode, follow_symlinks = (os.chmod in 
os.supports_follow_symlinks))

Karsten
-- 
GPG  40BE 5B0E C98E 1713 AFA6  5BC0 3BEA AC80 7D4F C89B
-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Convert a list with wrong encoding to utf8

2019-02-14 Thread David Raymond
Next question is how did you _insert_ those names into the database previously? 
Are the names showing up ok using any other tool to look at them?

The error might have been on insert and you're just seeing weird stuff now 
because of that. Maybe, where instead of giving it the text and letting the 
module deal with encodings, you gave it the raw UTF-8 encoding, and the module 
or db server said "let me encode that into the field or database defined 
default of latin-1 for you"... or something like that.


-Original Message-
From: Python-list 
[mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of 
vergos.niko...@gmail.com
Sent: Thursday, February 14, 2019 2:56 PM
To: python-list@python.org
Subject: Re: Convert a list with wrong encoding to utf8

Τη Πέμπτη, 14 Φεβρουαρίου 2019 - 8:56:31 μ.μ. UTC+2, ο χρήστης MRAB έγραψε:

> It doesn't have a 'b' prefix, so either it's Python 2 or it's a Unicode 
> string that was decoded wrongly from the bytes.

Yes it doesnt have the 'b' prefix so that hexadecimal are representation of 
strings and not representation of bytes.

I just tried:

names = tuple( [s.encode('latin1').decode('utf8') for s in names] )

but i get
UnicodeEncodeError('latin-1', 'Άκης Τσιάμης', 0, 4, 'ordinal not in range(256)')

'Άκης Τσιάμης' is a valid name but even so it gives an error.

Is it possible that Python3 a Unicode had the string wrongly decoded from the 
bytes ?

What can i do to get the names?!
-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: I cannot seem to write time/date to the file.

2019-02-12 Thread David Raymond
DateReading.write = (nowTimeDate2 + "\n")

You're re-assigning the write method to be a string here. You want to call the 
method with the string as the argument.

DateReading.write(nowTimeDate2 + "\n")



-Original Message-
From: Python-list 
[mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of 
Steve
Sent: Tuesday, February 12, 2019 2:20 PM
To: python-list@python.org
Subject: I cannot seem to write time/date to the file.

I am using nowTimeDate2 elsewhere in my program and it can be printed to the 
screen.

The file exists and the program successfully opens the file, writes to it, and 
closes the file.

All is working as designed.

 

However,

Now I want to place an entry into the first line of the file to note the 
time/date the file was created.
It doesn’t show up in the file.

How do I fix this?

 

from time import gmtime, strftime 

nowTimeDate2=strftime("%Y %a %b %d %H:%M") 

print(nowTimeDate2)  #This works

DateReading=open("Date-ReadingsAndDoses.txt", "w")

DateReading.write = (nowTimeDate2 + "\n")

DateReading.close()

 

I also tried to append but that did not work either.

Interestingly enough, this is a feature that really has no real purpose in the 
final product yet it is hounding me.

 

 

 

Footnote:

 

 Ultrasound Technician Asks Pregnant Woman If She’d Like To Know Baby’s Name

 

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


RE: more pythonic way

2019-02-11 Thread David Raymond
My non-expert vote is for

if month is None:
month = datetime.date.today().month

Because you're checking for your default value, not whether the boolean version 
of what they did give you is True or False. It's explicit, it's not reliant on 
any __bool__() function implementations or overrides, etc.


-Original Message-
From: Python-list 
[mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of 
Felix Lazaro Carbonell
Sent: Monday, February 11, 2019 2:30 PM
To: python-list@python.org
Subject: more pythonic way

 

Hello to everyone:

Could you please tell me wich way of writing this method is more pythonic:

 

..

def find_monthly_expenses(month=None, year=None):

month = month or datetime.date.today()

..

 

Or it should better be:

...

if not month:

month = datetime.date.today()

..

 

Cheers,

Felix.

 

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


RE: timezones

2019-02-07 Thread David Raymond
I'd say if the documentation mentions it, but doesn't say why, then we're not 
gonna be able to do much better for you as far as "why" goes.

http://pytz.sourceforge.net/

"Unfortunately using the tzinfo argument of the standard datetime constructors 
"does not work" with pytz for many timezones."

But it looks like they suggest something along the lines of...

timezone('Europe/Amsterdam').localize(datetime(2018, 12, 17, 11, 31, 26))


>From the examples on http://pytz.sourceforge.net/#problems-with-localtime ...

>>> eastern = timezone('US/Eastern')
>>> fmt = '%Y-%m-%d %H:%M:%S %Z%z'
>>> loc_dt = datetime(2002, 10, 27, 1, 30, 00)
>>> est_dt = eastern.localize(loc_dt, is_dst=True)
>>> edt_dt = eastern.localize(loc_dt, is_dst=False)
>>> print(est_dt.strftime(fmt) + ' / ' + edt_dt.strftime(fmt))
2002-10-27 01:30:00 EDT-0400 / 2002-10-27 01:30:00 EST-0500


Browse through their examples and see if you can find something similar that 
works for you.


-Original Message-
From: Python-list 
[mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of 
Jaap van Wingerde
Sent: Wednesday, February 06, 2019 9:27 AM
To: python-list@python.org
Subject: timezones

I made a small script to practise with timezones:

#!/usr/bin/env python3
# -*- coding: utf_8 -*-
from datetime import datetime, timedelta
from pytz import timezone
import pytz
amsterdam_datetime = datetime(2018, 12, 17, 11, 31, 26,
tzinfo=timezone('Europe/Amsterdam'))
print(amsterdam_datetime)
utc_datetime = amsterdam_datetime.astimezone(pytz.utc)
print(utc_datetime)
amsterdam_datetime = datetime(2018, 6, 17, 11, 31, 26,
tzinfo=timezone('Europe/Amsterdam'))
print(amsterdam_datetime)
utc_datetime = amsterdam_datetime.astimezone(pytz.utc)
print(utc_datetime)

The output of the script is:
2018-12-17 11:31:26+00:20
2018-12-17 11:11:26+00:00
2018-06-17 11:31:26+00:20
2018-06-17 11:11:26+00:00

I respected:
2018-12-17 11:31:26+01:00
2018-12-17 10:31:26+00:00
2018-06-17 11:31:26+02:00
2018-06-17 09:31:26+00:00

I need this functionality for adjusting wrong timestamps in Android
JPG-images as the 'Exif.GPSInfo.GPSTimeStamp' and
'Exif.GPSInfo.GPSDateStamp' are missing.

Why I get this unrespected results?

Kind regards,
Jaap.


-- 

Jaap van Wingerde
e-mail: 1234567...@vanwingerde.nl

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


RE: Loop with else clause

2019-02-05 Thread David Raymond
As mentioned somewhere, "readability counts", so why not just go with exactly 
what you said...

if len(nominees) > 0:#if a condition exists
for nominee in nominees: #the loop should be executed
print(nominee)
else:#but if not
print("D'oh! No one is worthy.") #something else should be done


The fancy features of the language can be cool and all, but forcing yourself to 
use them just for the sake of using them and "being pythonic" can result in 
weird situations, and wrong or unclear assumptions or intents. When someone 
else reads it later it may hide what you were actually intending the check to 
look for.

Hmm, "if nominees:" Did they make it like that to look for a 0 length list, or 
to make sure they didn't get None by accident, or both, or something different?

Hmm, "if len(nominees) > 0:" Ahh ok, we want to make sure there's something in 
there.

So my 2 cents: Feel free to be non-pythonic if it makes things clear and works.


-Original Message-
From: Python-list 
[mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of 
Peter Otten
Sent: Tuesday, February 05, 2019 3:44 AM
To: python-list@python.org
Subject: Re: Loop with else clause

DL Neil wrote:

> What is the pythonic way to handle the situation where if a condition
> exists the loop should be executed, but if it does not something else
> should be done?

> Possible solution:
> To make anything more than the trivial case readable, I think I'd put
> the list processing into one function, and the exception into another
> (except that this case is so trivial), ie
> 
> if list:
> process_list() #the heading and for-loop, as above
> else:
> print( "Sorry...
> 
> 
> Others wanted to add a semaphore/flag inside the loop to indicate if it
> was executed at least once. Yes, could even use the else clause then!

An argument in favour of the flag is that it works with arbitrary iterables 
whereas if ...: fails:

>>> numbered = enumerate([])
>>> if numbered:
... print("the winners are")
... for ix in numbered: print(*ix)
... 
the winners are
 
> The ideas went (rapidly) down-hill from there...
> 
> 
> Is there another, more pythonic, approach to conditional (for/while)
> loop processing?

I'm not aware of such an approach.

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


time.strftime question on 0 as argument

2019-01-28 Thread David Raymond
https://docs.python.org/3.7/library/time.html#time.strftime

In the docs there is
"0 is a legal argument for any position in the time tuple; if it is normally 
illegal the value is forced to a correct one."

and yet if given 0 for the year/first item in the tuple it raises a ValueError.

Is that a bug in the code, the documentation, or my understanding?


Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit 
(AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> time.strftime("%b", (0, 1, 0, 0, 0, 0, 0, 0, 0))
Traceback (most recent call last):
  File "", line 1, in 
ValueError: strftime() requires year in [1; ]
>>>


Looks like it works in 2, so maybe an incorrect documentation carryover?

Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:30:26) [MSC v.1500 64 bit 
(AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> time.strftime("%b", (0, 1, 0, 0, 0, 0, 0, 0, 0))
'Jan'
>>>
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Pythonic Y2K

2019-01-18 Thread David Raymond
Reminds me of a similar problem that didn't get noticed until it did actually 
hit: In 2007 the first time a group of F-22's crossed the international date 
line every computer system in the aircraft crashed, losing comms, navigation, 
avionics, and a host of other systems. Fortunately their engines, landing gear, 
and enough other systems still worked, so they were able to visually follow 
their refueling tankers back to Hawaii and land, where they had to sit for a 
couple days before Lockheed could patch their software.

If the circumstances had been a little different they could have lost a whole 
group of shiny new $150 million aircraft to a software bug and left a bunch a 
pilots floating in life rafts for a while in the middle of the Pacific.


-Original Message-
From: Python-list 
[mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of 
Michael Torrie
Sent: Friday, January 18, 2019 10:36 AM
To: python-list@python.org
Subject: Re: Pythonic Y2K

On 01/16/2019 12:02 PM, Avi Gross wrote:
> I recall the days before the year 2000 with the Y2K scare when people
> worried that legacy software might stop working or do horrible things once
> the clock turned. It may even have been scary enough for some companies to
> rewrite key applications and even switch from languages like COBOL.

Of course it wasn't just a scare.  The date rollover problem was very
real. It's interesting that now we call it the Y2K "scare" and since
most things came through that okay we often suppose that the people who
were warning about this impending problem were simply being alarmist and
prophets of doom.  We often deride them.  But the fact is, people did
take these prophets of doom seriously and there was a massive, even
heroic effort, to fix a lot of these critical backend systems so that
disaster was avoided (just barely).  I'm not talking about PCs rolling
over to 00.  I'm talking about banking software, mission critical
control software.  It certainly was scary enough for a lot of companies
to spend a lot of money rewriting key software.  The problem wasn't with
COBOL necessarily.

In the end disaster was averted (rather narrowly) thanks to the hard
work of a lot of people, and thanks to the few people who were vocal in
warning of the impending issue.

That said, I'm not sure Python 2.7's impending EOL is comparable to the
Y2K crisis.
-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Pythonic Y2K

2019-01-18 Thread David Raymond
Which brings up the assumption that this whole A.D. thing is gonna stick around 
for more than a few millennia and isn't just a fad. Sloppy to just use positive 
for A.D. and negative for B.C. without a discrete unit for Age. What happens 
when Sauron is defeated and the Third Age is declared? Or if we go back to "in 
the 65th year of the reign of Elizabeth, second of her name"? Or if someone 
declares a new epoch and that this is really the year 49 A.U. (Anno Unixy)?


-Original Message-
From: Python-list 
[mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of 
Avi Gross
Sent: Thursday, January 17, 2019 5:46 PM
To: python-list@python.org
Subject: RE: Pythonic Y2K

Ian,

You just scared me. It is 2019 which has four digits. In less than 8,000
years we will need to take the fifth to make numbers from 10,000 to 10,999.
90,000 years later we will need a sixth digit and so on.

Do you know how many potential Y2K-like anomalies we may have between now
and year 292,277,026,596 when it may all be over? Will people evert learn
and just set aside lots of room that dates can grow into or allow varying
lengths?

Makes me wonder though why anyone in the distant future would want numbers
that long to represent that date. I suspect that long before then, some
surviving members of whatever the human race becomes will do a reset to a
new calendar such as the date the first settlers arrived in the Gamma
Quadrant.

So whatever unit they store time in, though, may still need a way to reach
back to historic times just as we do by talking about what may have happened
in 2000 B.C.


-Original Message-
From: Python-list  On
Behalf Of Ian Kelly
Sent: Thursday, January 17, 2019 2:14 PM
To: Python 
Subject: Re: Pythonic Y2K

On Wed, Jan 16, 2019 at 9:57 PM Avi Gross  wrote:
>
> The forthcoming UNIX 2038 problem will, paradoxically happen on 
> January
19. I wonder what they will do long before then. Will they just add a byte
or four or 256 and then make a date measurable in picoseconds? Or will they
start using a number format that can easily deal with 1 Million B.C. and 5
Billion A.D. just in case we escape earth before it co-locates with the
expanding sun.

The obvious solution is to stop using 32-bit Unix timestamps and start using
64-bit Unix timestamps. This change has already been made in some OSes, and
the problem will not repeat until the year 292,277,026,596, by which time it
is highly unlikely that either Unix timestamps or humankind itself will
still exist. Even if they will, that moment in time is so far out from the
present that I can't really be bothered by the possibility.

We have 19 years to take care of the problem before it happens. Hopefully
this time around we won't be trying to fix it right up until the last
minute.
--
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: How can I find the indices of an array with float values in python?

2019-01-10 Thread David Raymond
Maybe something along the lines of...

indices = [i for i, val in enumerate(x) where 0 <= val <= 15000]


-Original Message-
From: Python-list 
[mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of 
Madhavan Bomidi
Sent: Thursday, January 10, 2019 12:05 PM
To: python-list@python.org
Subject: Re: How can I find the indices of an array with float values in python?

Sorry for re-posting with a correction.

I have an array (numpy.ndarray) with shape (1500L,) as below:
 
 x = array([  3.e+01,   6.e+01,   9.e+01, ...,
  4.4940e+04,   4.4970e+04,   4.5000e+04])
 
Now, I wanted to determine the indices of the x values between 0.0 and 15000.0. 
While this is simple in MATLAB or IDL by using find or where functions, I was 
unable to find a best way to find the indices of all elements between 0.0 and 
15000.0 in the x array. Can you please suggest me a solution for the same?

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


RE: subprocess : AttributeError: 'Popen' object has no attribute 'read'

2019-01-03 Thread David Raymond
Agreeing with the other poster that it's probably not the best way to handle it.

But for the sake of helping with subprocess:
https://docs.python.org/3.7/library/subprocess.html#popen-objects

Popen Objects don't have read() as the error says. That's on their .stdout and 
.stderr streams. So you'd want g_info.stdout.read() Or .stderr maybe, depending 
on what you're running and how it does its output. If you want them both to go 
to the same thing you can use stderr = subprocess.STDOUT instead of 
subprocess.PIPE, then both will end up in your .stdout stream.

And while it is indeed gonna be a quick thing that you're running, you have 
nothing in there that makes sure your subprocess actually runs and finishes 
before you're trying to read the results, which will bite you on anything more 
complicated.



-Original Message-
From: Python-list 
[mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of 
Mohan Mohta
Sent: Thursday, January 03, 2019 2:44 PM
To: python-list@python.org
Subject: subprocess : AttributeError: 'Popen' object has no attribute 'read'

Hello,
I am trying to grep the keyword (which I got from report_file ) from report_file

I tried multiple ways but am unable to get it to work.

Below are the methods I tried.


fp=open(txt_file,'r')
 for line in fp :
line=line.strip()
var1=line.lower()
g_info=subprocess.Popen('cat report_file| grep -i '+var1, 
stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
g_info=g_info.read()
g_info=g_info.strip()
info=g_info.strip('||')
print g_info
print info
fp.close()

Error:
AttributeError: 'Popen' object has no attribute 'read'


fp=open(txt_file,'r')
 for line in fp :
line=line.strip()
var1=line.lower()
cmd='cat report_file| grep -i '+var1
g_info=subprocess.Popen(cmd, 
stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
g_info=g_info.read()
g_info=g_info.strip()
info=g_info.strip('||')
print g_info
print info
fp.close()

Error:
AttributeError: 'Popen' object has no attribute 'read'
+++

fp=open(txt_file,'r')
 for line in fp :
line=line.strip()
var1=line.lower()
cmd='cat report_file| grep -i '+var1
g_info=os.command(cmd)
g_info=g_info.read()
g_info=g_info.strip()
info=g_info.strip('||')
print g_info
print info
fp.close()

Result :
The Code executes but the output is in screen and does not get stored in a 
variable.
I am interested if I can achieve the same result with subprocess calls
+


fp=open(txt_file,'r')
 for line in fp :
line=line.strip()
var1=line.lower()
cmd='cat report_file| grep -i '+var1
g_info=subprocess.Popen(cmd, 
stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True).
g_info=g_info.stdout.read()
g_info=g_info.strip()
info=g_info.strip('||')
print g_info
print info
fp.close()

Error
AttributeError: 'Popen' object has no attribute 'read'



fp=open(txt_file,'r')
 for line in fp :
line=line.strip()
var1=line.lower()
cmd=['cat','report_file','|','grep','-i',serv_name]
g_info=subprocess.Popen(cmd)
g_info.wait()
try :
  g_info=g_info.stdout.readlines()
  print g_info
except AttributeError :
  pass
g_info=g_info.strip()
info=g_info.strip('||')
print g_info
print info
fp.close()

Result :
Nothing gets printed out




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


RE: Variable number of arguments

2018-12-17 Thread David Raymond
argparse works fine on 3.x
https://docs.python.org/3.6/library/argparse.html

You can't really have back to back _positional_ arguments with nargs = "+" as 
it won't be able to tell where you meant one group to end and the next to 
begin. You'd have to call it with switches like

script.py -if inFile1 inFile2 inFile3 -of outFile -swf stopwordsFile1 
stopwordsFile2


-Original Message-
From: Python-list 
[mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of F 
Massion
Sent: Monday, December 17, 2018 1:38 PM
To: python-list@python.org
Subject: Variable number of arguments

My script is used by a web interface which gets some parameters from a form.
The parameters are passed on as arguments like this:

(...)
def start(self, _Inputfile ,_Outputfile ,_Stopwordsfile)

As long as the number of arguments is set (i.e. 3), there are no problems 
running the script.

Currently I have e.g. 
ARGV[0] = _Inputfile
ARGV[1] = _Outputfile
ARGV[2] = _Stopwordsfile

Now I want to allow a variable number of arguments, i.e. 1..n input files or 
stopwords lists.

In this case ARGV[0] would become [filename1.txt, filename2.txt,...], but I 
wonder how ARGV[1] would still remain _Outputfile.

I thought of using the argparse module with nargs='+' for a variable number 
arguments but it seems to work only with Python 2.7 and I have 3.6 running.

Any suggestions?
-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Why do integers compare equal to booleans?

2018-11-16 Thread David Raymond
A boolean type didn't come about until version 2.3, and even now they still 
inherit from integers.

Some links for you:

https://docs.python.org/3.7/whatsnew/2.3.html#pep-285-a-boolean-type
https://docs.python.org/3.7/library/stdtypes.html#boolean-values
https://docs.python.org/3.7/reference/datamodel.html#the-standard-type-hierarchy



-Original Message-
From: Python-list 
[mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of 
Steve Keller
Sent: Friday, November 16, 2018 9:51 AM
To: python-list@python.org
Subject: Why do integers compare equal to booleans?

Why do the integers 0 and 1 compare equal to the boolean values False
and True and all other integers to neither of them?

$ python3
Python 3.5.2 (default, Nov 12 2018, 13:43:14)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 0 == False
True
>>> 1 == True
True
>>> 2 == False
False
>>> 2 == True
False
>>> -1 == False
False
>>> -1 == True
False
>>>

Since these are objects of different types I would expect they cannot
be equal.  I know that 0 means false and != 0 means true in C, C++,
etc. but in Python that surprises me.

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


RE: Windows file associations fix

2018-11-13 Thread David Raymond
Thank you for the detailed info.

HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Python.File\shell   only had "Edit with 
Pythonwin", and didn't have an "open" key. I added that in, along with one for 
the Python.NoConFile which was also missing "open", and it seems to be working 
happily now.

Again, many thanks for the replies.


-Original Message-
From: Python-list 
[mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of 
eryk sun
Sent: Friday, November 09, 2018 6:43 PM
To: Python
Subject: Re: Windows file associations fix

On 11/9/18, David Raymond  wrote:
>
> And why does the Python installer have a check box for "Associate files with
> Python" if it then promptly proceeds to destroy all those associations?
> Could we maybe get that part of the installer fixed for future versions?

The installer configures the HKCR setting to associate .py[w] files;
however, it doesn't delete a user-choice setting in the shell. You
should be able to restore the user choice to the launcher via the
open-with -> choose another app dialog. Select the Python icon with a
rocket on it (for py.exe; the python.exe icon has no rocket), enable
the option to always use this application, and click ok.

If that doesn't restore the association, you'll have to manually
inspect and modify the settings. The following lists the shell's
cached association for the .py file extension, including the user
choice, if any:

set "winkey=HKCU\Software\Microsoft\Windows\CurrentVersion"
reg query %winkey%\Explorer\FileExts\.py /s

Output:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\
Explorer\FileExts\.py\OpenWithList
aREG_SZpy.exe
MRUListREG_SZa

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\
Explorer\FileExts\.py\OpenWithProgids
Python.FileREG_NONE

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\
Explorer\FileExts\.py\UserChoice
HashREG_SZaA7ZxnDmUdM=
ProgIdREG_SZPython.File

Currently I have a UserChoice setting locked in. The permissions and
hash value prevent us from modifying this key, but we can delete it,
in which case the shell reverts to using the cached OpenWith*
associations. Here's a command to delete this key (or use regedit if
you prefer a GUI), where %winkey% was previously defined above:

reg delete %winkey%\Explorer\FileExts\.py\UserChoice

If we instead delete the FileExts\.py key, the shell recomputes the
association from the underlying HKCR settings, where HKCR is a merged
view of [HKLM | HKCU]\Software\Classes.

If that doesn't solve the problem, check the default value of [HKCU |
HKLM]\Software\Classes\.py, and its OpenWithProgIds subkey if defined.
Python's installer associates .py files with the "Python.File" program
identifier (progid).

If that's set correctly, check [HKCU |
HKLM]\Software\Classes\Python.File to ensure it's set up to use the
py.exe launcher. For example, the following displays the default
"open" command for the local-machine Python.File progid:

reg query HKLM\Software\Classes\Python.File\shell\open\command

Output:

HKEY_LOCAL_MACHINE\Software\Classes\Python.File\shell\open\command
(Default)REG_SZ"C:\Windows\py.exe" "%1" %*

"%1" (or sometimes "%L") is the fully-qualified path of the target .py
script, and %* has the command-line arguments, if any.

As a side note, you've probably seen examples that use CMD's assoc and
ftype commands (from NT 4.0, circa 1996). These commands work with
HKLM\Software\Classes, which is fine for classic file associations
configured for all users. However, they do not show or modify HKCU
settings, and they're unaware of updates to how the shell works since
XP/Vista such as HKCR\Applications, HKCR\SystemFileAssociations, and
application capability file associations linked in [HKCU |
HKLM]\Software\RegisteredApplications.

> Currently: Windows 7. Attempting to run a .py file opens up the "Open with"
> dialog with a bunch of other programs listed. Clicking browse to manually
> select the new python.exe doesn't add it to the list and won't let me do the
> association.

You shouldn't manually associate .py files by browsing for py.exe or
python.exe. If you do this, the system creates an automatic progid
that only includes the "%1" target, without the %* command-line
arguments. Typically this broken progid will be
"HKCU\Software\Classes\Applications\py.exe", or
"HKCU\Software\Classes\Applications\python.exe", or
"HKCU\Software\Classes\py_auto_file". Delete these keys if they exist.
-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Windows file associations fix

2018-11-09 Thread David Raymond
Would some kind person be so good as to point me to instructions on how to fix 
Windows file associations and default programs for the various Python 
extensions (.py/.pyw/etc)? I know I've fixed this before after installing a new 
version, but didn't save the instructions, and apparently my searching skills 
are getting worse as the tips I'm finding at the moment aren't helping.

And why does the Python installer have a check box for "Associate files with 
Python" if it then promptly proceeds to destroy all those associations? Could 
we maybe get that part of the installer fixed for future versions?

Desired state: Double click a .py file and have it run in a new console. Being 
able to just type "foo.py" in a command prompt and have it run without needing 
to run "{python|py|explicit-python-executable} foo.py"

Currently: Windows 7. Attempting to run a .py file opens up the "Open with" 
dialog with a bunch of other programs listed. Clicking browse to manually 
select the new python.exe doesn't add it to the list and won't let me do the 
association.

Do have administrator permissions, did do the install "for all users", did 
include the py launcher, have tried repairing, reinstalling and rebooting.

Did look at the "Python on Windows FAQ" 
(https://docs.python.org/3/faq/windows.html#how-do-i-make-python-scripts-executable)
 which confirms the installer should set it up to work the way I'm looking for 
and had it previously.

The rest of the installation is fine, the only issue is the file associations.

Any patient assistance appreciated.

Thank you,
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: [OT] master/slave debate in Python

2018-09-26 Thread David Raymond
...Think about how you treat your computers - you have the power to discard 
them if they do not work correctly, or even if you just want to get a newer 
one. You have the power to kick them across the room and nobody will arrest 
you. Maybe you don't do those things (I would hope you don't kick computers 
around), but the computer has no say in that...

...At least, not yet...

HAL.open(ship.pod_bay.doors)



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


RE: [SPAM] Type error: not enough arguments for format string

2018-09-19 Thread David Raymond
My first bet at the culprit is the space between the % and (message)s, they 
should be together like you have them for asctime.


-Original Message-
From: Python-list 
[mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of 
synch1...@gmail.com
Sent: Wednesday, September 19, 2018 12:12 PM
To: python-list@python.org
Subject: [SPAM] Type error: not enough arguments for format string
Importance: Low

I'm just trying to follow along with the logging tutorial documentation and I 
am getting this error:

import logging


logging.basicConfig(format= '%(asctime)s % (message)s', datefmt='%m%d%Y 
%I:%M:%S %p')
logging.warning('is when this event was logged')

Error:

C:\Users\Malcy\PycharmProjects\Udemy\venv\Scripts\python.exe 
C:/Users/Malcy/PycharmProjects/logging/logger.py
Traceback (most recent call last):
  File 
"C:\Users\Malcy\AppData\Local\Continuum\anaconda2\Lib\logging\__init__.py", 
line 861, in emit
msg = self.format(record)
  File 
"C:\Users\Malcy\AppData\Local\Continuum\anaconda2\Lib\logging\__init__.py", 
line 734, in format
return fmt.format(record)
  File 
"C:\Users\Malcy\AppData\Local\Continuum\anaconda2\Lib\logging\__init__.py", 
line 469, in format
s = self._fmt % record.__dict__
TypeError: not enough arguments for format string
Logged from file logger.py, line 6

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


RE: Why emumerated list is empty on 2nd round of print?

2018-09-06 Thread David Raymond
The actual "enumerate" object is really just holding a current index and a 
reference to the original list. So if you alter the original list while you're 
iterating through it you'll see the changes. If you want a full copy then you 
can just wrap it with list()

Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit 
(AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> numList = [2, 7, 22, 30, 1, 8]
>>> aList = enumerate(numList)
>>> aList.__next__()
(0, 2)
>>> numList[1] = 5
>>> aList.__next__()
(1, 5)
>>> aList2 = list(enumerate(numList))
>>> aList2
[(0, 2), (1, 5), (2, 22), (3, 30), (4, 1), (5, 8)]
>>> numList[3] = -12
>>> aList2
[(0, 2), (1, 5), (2, 22), (3, 30), (4, 1), (5, 8)]
>>> aList.__next__()
(2, 22)
>>> aList.__next__()
(3, -12)
>>> aList.__next__()
(4, 1)
>>> aList.__next__()
(5, 8)
>>> aList.__next__()
Traceback (most recent call last):
  File "", line 1, in 
StopIteration
>>>


-Original Message-
From: Python-list 
[mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of 
Viet Nguyen via Python-list
Sent: Thursday, September 06, 2018 2:50 PM
To: python-list@python.org
Subject: Re: Why emumerated list is empty on 2nd round of print?

On Thursday, September 6, 2018 at 10:34:19 AM UTC-7, Chris Angelico wrote:
> On Fri, Sep 7, 2018 at 3:26 AM, Viet Nguyen via Python-list
>  wrote:
>  numList
> > [2, 7, 22, 30, 1, 8]
> >
>  aList = enumerate(numList)
> >
>  for i,j in aList:print(i,j)
> >
> > 0 2
> > 1 7
> > 2 22
> > 3 30
> > 4 1
> > 5 8
> >
>  for i,j in aList:print(i,j)
> >
> 
> 
> Because it's not an enumerated list, it's an enumerated iterator.
> Generally, you'll just use that directly in the loop:
> 
> for i, value in enumerate(numbers):
> 
> There's generally no need to hang onto it from one loop to another.
> 
> ChrisA

Thanks ChrisA. If I do this "aList = enumerate(numList)", isn't it stored 
permanently in aList now?  I see your point to use it directly, but just in 
case I do need to hang onto it from one loop to another, then how is that done? 
  Anyway I think I'm ok and I got what I need for now.
-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: How to sort over dictionaries

2018-08-30 Thread David Raymond
Remember to check what the res["date"] types actually are. If they're just 
text, then it looked like they were in M/D/Y format, which won't sort correctly 
as text, hence you might want to include using datetime.strptime() to turn them 
into sortable datetimes.


-Original Message-
From: Python-list 
[mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of 
har...@moonshots.co.in
Sent: Thursday, August 30, 2018 4:31 AM
To: python-list@python.org
Subject: Re: How to sort over dictionaries



> > sort = sorted(results, key=lambda res:itemgetter('date'))
> > print(sort)
> > 
> > 
> > I have tried the above code peter but it was showing error like 
> > TypeError: '<' not supported between instances of 'operator.itemgetter'
> > and 'operator.itemgetter'
> 
> lambda res: itemgetter('date')
> 
> is short for
> 
> def keyfunc(res):
> return itemgetter('date')
> 
> i. e. it indeed returns the itemgetter instance. But you want to return 
> res["date"]. For that you can either use a custom function or the 
> itemgetter, but not both.
> 
> (1) With regular function:
> 
> def keyfunc(res):
> return res["date"]
> sorted_results = sorted(results, key=keyfunc)
> 
> (1a) With lambda:
> 
> keyfunc = lambda res: res["date"]
> sorted_results = sorted(results, key=keyfunc)
> 
> (2) With itemgetter:
> 
> keyfunc = itemgetter("date")
> sorted_results = sorted(results, key=keyfunc)
> 
> Variants 1a and 2 can also be written as one-liners.



Thanks PeterNo 2 has worked.
-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: How to sort over dictionaries

2018-08-29 Thread David Raymond
Well, that's a list of... somethings. So I'm assuming you mean sort a list of 
dictionaries?

foo.sort(key = lambda x: time.strptime(x["date"], "%d-%m-%Y %H:%M"))

with , reverse = True in the sort if you want it sorted in reverse



-Original Message-
From: Python-list 
[mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of 
har...@moonshots.co.in
Sent: Tuesday, August 28, 2018 11:56 PM
To: python-list@python.org
Subject: How to sort over dictionaries

[, 
https://getbootstrap.com/', 'author': 'Hari', 'date': '15-08-2018 15:15', 
'headline': 'latest news'}>, , https://www.deps.co/blog/google-cloud-platform-good-bad-ugly/', 'author': 
'Harish', 'headline': 'Google Cloud Platform – The Good, Bad, and Ugly', 
'date': '16-08-2018 08:15'}>, , , 
http://www.dailymail.co.uk/tvshowbiz/harry_styles/index.html', 'author': 
'Harry', 'headline': 'Reunion', 'date': '17-08-2018 20:40', 'votes': 3}>, 
https://www.telegraph.co.uk/news/2017/07/15/us-vogue-apologises-missing-mark-zayn-malik-gigi-hadid-gender/',
 'author': 'zayn', 'date': '17-08-2018 15:39', 'headline': 'Memories'}>]



Can someone help me out how to sort this dictionary data based on time,
how to arrange the same dictionary in descending order based on time.


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


RE: Python Postgresql complete guide

2018-08-23 Thread David Raymond
Looks good.

Having used psycopg2 a fair amount, here are some suggestions I have on extra 
things to cover or emphasize.


-Postgres specific things like remembering to "set search_path to blargh, 
public;" etc as needed before querying.

-An example case of cur.fetchone() returning None, or more importantly, showing 
the error you get when you forget about that case

-Using conn.set_session(isolation_level, readonly, deferrable, autocommit) to 
set up transaction behavior at the start. (Can restrict to setting only the 
ones you care about by using keyword args)

-Going over some of the various caveats of autocommit on vs off
--autocommit on mode still allows transactions and rollbacks when you 
explicitly start a transaction with a cur.execute("begin;")
--To end an explicit autocomit transaction you need to use 
cur.execute("commit;") or cur.execute("rollback;"), you can't use conn.commit() 
or conn.rollback()
--With autocommit off you'll have to make sure you've run rollback or commit to 
use some commands which "cannot be run inside a transaction block" such as 
vacuum
--Autocommit off starts a transaction for any query, and will leave the 
transaction open until you commit it or roll it back. Thus if you run a simple 
select, then walk away for 5 hours with your connection still connected, you'll 
have left a transaction open on the server the whole time.

-Server side cursors: Running a select query that will result in 4 GB of data? 
With a "normal" cursor, even when iterating over the cursor or using fetchmany 
it will try to download the entire result set first before iterating over the 
results. (Actually, the .execute() statement will fetch everything even before 
you get a chance to run any of the .fetchone/many/all methods) Using a server 
side cursor will let you get it in chunks rather than trying to load it all 
into memory first.
--Server side cursors require autocommit off

-Enabling unicode for Python2.x so you get already decoded unicode objects back 
for text, not byte strings.
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY)



-Original Message-
From: Python-list 
[mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of 
vishalhul...@gmail.com
Sent: Thursday, August 23, 2018 8:59 AM
To: python-list@python.org
Subject: Python Postgresql complete guide


https://pynative.com/python-postgresql-tutorial/

I have added table of content at the start of the article

This tutorial mainly focuses on installing Psycopg2 and use its API to access 
the PostgreSQL database. It then takes you through data insertion, data 
retrieval, data update and data deletion, transaction management, connection 
pooling and error-handling techniques to develop robust python programs with 
PostgreSQL.
-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: ignoring some default fields from SimpleJsonFormatter

2018-08-21 Thread David Raymond
https://docs.python.org/3.7/library/logging.html#logging.Logger.debug
https://docs.python.org/3.7/library/logging.html#logging.Formatter.format


Basically your Formatter string doesn't include %(anotherfield1)s in it 
anywhere, so that gets ignored. To have a variable number of those in there 
you'd have to make a custom Formatter class where .format(record) looks for any 
"left over" keys in the given LogRecord's args, and adds them to the returned 
message as appropriate.

(Also note that you want 03d for msecs, not 06d)


import logging
import sys
fmt = '{"timestamp": "%(asctime)s.%(msecs)03d", "level":"%(levelname)s"' \
  ', "anottherfield1":"%(anotherfield1)s"}'
datefmt = "%Y-%m-%dT%H:%M:%S"
logging.basicConfig(stream = sys.stdout,
level = logging.DEBUG,
format = fmt,
datefmt = datefmt)
logging.info("my test message", extra = {"anotherfield1": "test"})


{"timestamp": "2018-08-21T12:20:35.475", "level":"INFO", 
"anottherfield1":"test"}




-Original Message-
From: Python-list 
[mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of 
shradha...@gmail.com
Sent: Tuesday, August 21, 2018 11:37 AM
To: python-list@python.org
Subject: ignoring some default fields from SimpleJsonFormatter

I am using for my logger

handler.setFormatter(SimpleJsonFormatter(json.dumps))

It had some default fields - timestamp, function, line_number, module, level

and flexibility to provide extra fields in json log with use of 

   logger.info("my test message", extra={"anotherfield1": "test"})

I am using decorator functions so some of the default fields provided ex- 
function, line_number, module are not useful as it gives information on 
decorator module, line_number. 
I like to remove some fields from being logged while retaining others, keeping 
also the use of "extra"

This is what I tried -
 logging.Formatter('{"timestamp": "%(asctime)s.%(msecs)06d", 
"level":"%(levelname)s"}',
'%Y-%m-%dT%H:%M:%S')

The problem is that it doesn't print the fields given in 'extra'


How do I accomplish this?
-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: How to pass Python command line options (vs arguments) when running script directly vs via Python interpreter?

2018-08-15 Thread David Raymond
So what are you saying is an option vs an argument? Because I see no 
distinction whatsoever. When you run something you give it a bunch of strings.

That's it.

There is nothing magical about putting a dash in front of a letter, nothing 
magical about putting in a string that might possibly also be a file name. The 
only things that might matter are white space and quote characters, because 
really all you're doing is giving the shell or OS a  string, and it 
decides what to run and what is the resulting (ordered) list of strings which 
it will then pass to that program.

Being able to just run "script.py" is just a convenience provided by the OS. It 
goes "ohh, after I've parsed it, that first token matches up with an existing 
file, and my records say that that extension can be opened with this python.exe 
program, so I'm just gonna run that python.exe thing and pass it the ordered 
list of everything I just got."

In this specific case, the people who wrote python.exe decided that when it 
goes through the list of strings which it was given, that that first thing that 
doesn't start with a dash is the "file name", that anything before that "file 
name" are things it will look at right now, and anything after the "file name" 
are things it will throw into sys.argv and let later execution decide what to 
do with.

Since the convenience method uses the first bit as the "file name" to determine 
which convenience program to run, and the python.exe program just tosses 
anything after the "file name" into sys.argv, then to get anything as an 
"argument" to python.exe and not the script, then you either need to run it as 
"python.exe bunch of strings to pass to python.exe", or the more difficult 
method of "muck about with the OS's convenience method to get it to do 
something magical."


-Original Message-
From: Python-list 
[mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of 
Malcolm Greene
Sent: Tuesday, August 14, 2018 7:47 PM
To: python-list@python.org
Subject: Re: How to pass Python command line options (vs arguments) when 
running script directly vs via Python interpreter?

> You might try:
> from getopt import getopt
> or the (apparently newer):
> from optparse import OptionParser

Thanks Mike. My question was trying to make a distinction between Python 
options (flags that precede the script or module name) and arguments (the 
script specific values passed on the command line following the script's name).

Here's a description of the options I'm referring to:
https://docs.python.org/3/using/cmdline.html#generic-options
-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Dealing with errors in interactive subprocess running python interpreter that freeze the process

2018-08-01 Thread David Raymond
A couple notes:

-I think the Python interpreter actually sends its output to stderr, so to 
capture it you'd probably want it to go to the same place as stdout, so use 
stderr = subprocess.STDOUT

-You're only reading 1 line out output for each thing, so if 1 command creates 
multiple lines of output then you won't be showing them all.

-You're never actually checking if the called process is still alive or not. It 
should normally be something like:
...
interpreter = subprocess.Popen(...)
...
interpreter.poll()
while interpreter.returncode is not None:
...
interpreter.poll()
cleanup stuff

-To the actual question it looks like it has the stdout stream in blocking mode 
somehow. So when you're reading from stdout and there's nothing there it's 
blocking and waiting for there to be something, which will never happen. 
Flipping through the documentation for subprocess, and io (interpreter.stdout 
is of ) io mentions blocking vs non blocking a lot, 
but it's not mentioned in subprocess. And I don't see in either how to tell if 
a stream is in blocking mode or not, or how or if it's possible to change that. 
So I don't know what to suggest for that, sorry.

-Original Message-
From: Python-list 
[mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of 
cseber...@gmail.com
Sent: Wednesday, August 01, 2018 4:11 PM
To: python-list@python.org
Subject: Dealing with errors in interactive subprocess running python 
interpreter that freeze the process

I can run python3 interactively in a subprocess w/ Popen but
if I sent it text, that throws an exception, the process freezes
instead of just printing the exception like the normal interpreter..
why? how fix?  Here is my code below.

(I suspect when there is an exception, there is NO output to stdin so that
the problem is the line below that tries to read from stdin never finishes.
Maybe I need a different readline that can "survive" when there is no output 
and won't block?)



import subprocess
 
interpreter = subprocess.Popen(['python3', '-i'],
   stdin  = subprocess.PIPE,
   stdout = subprocess.PIPE,
   stderr = subprocess.PIPE)
 
while True:
exp = input(">>> ").encode() + b"\n"
interpreter.stdin.write(exp)
interpreter.stdin.flush()
print(interpreter.stdout.readline().strip())
interpreter.stdin.close()
interpreter.terminate()
-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Python Console Menu

2018-07-31 Thread David Raymond
Take a look at the subprocess module for how to "spawn new processes, connect 
to their input/output/error pipes, and obtain their return codes."

https://docs.python.org/2/library/subprocess.html


-Original Message-
From: Python-list 
[mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of 
Tcpip via Python-list
Sent: Tuesday, July 31, 2018 10:56 AM
To: python-list@python.org
Subject: Python Console Menu

Hi all,

Im new with python, im working on a Python console Menu, I found some examples 
on Git, but  what I need to understand is how I can call a subprocess.

Here is an Example ,

if choice==1:
print "Test SSH Connection (check ssh to all hosts)"
## You can add your code or functions here
#print_menu()
elif choice==2:
print "Menu 2 has been selected"
## You can add your code or functions here
elif choice==3:
print "Menu 3 has been selected"
## You can add your code or functions here
elif choice==4:
print "Menu 4 has been selected"
## You can add your code or functions here
elif choice==5:
print "Menu 5 has been selected"
## You can add your code or function here


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


RE: Python bug in ArcGIS - Urban Network analysis tool

2018-07-30 Thread David Raymond
A note that Arc may have installed its own version of Python, which it is using 
from within its own tools. For example, I've got a full Python installation in 
C:\Python27\ArcGIS10.2 which Arc installed on top of a preexisting installation 
in C:\Python27. So you may need to explicitly run it with that version to get 
it to work and to import arcpy ok.

ie instead of just running...

pydevd.py

or...

python pydevd.py

You might need to explicitly do...

C:\Python27\ArcGIS10.2\python.exe pydevd.py

...or the equivalent of wherever it installed it on your system.


-Original Message-
From: Python-list 
[mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of 
? 
Sent: Monday, July 30, 2018 2:03 AM
To: python-list@python.org
Subject: Re: Python bug in ArcGIS - Urban Network analysis tool

понеделник, 30 юли 2018 г., 3:29:44 UTC+3, MRAB написа:
> On 2018-07-29 22:00, Станимира Николова wrote:
> > неделя, 29 юли 2018 г., 23:41:01 UTC+3, MRAB написа:
> >> On 2018-07-29 18:56, stanimira.s...@gmail.com wrote:
> >> > Hi there,
> >> > 
> >> > I'm trying make reach analyze with Urban network analysis (UNA) tool in 
> >> > ArcGIS. It's based on points for buildings and pedestrian network of 
> >> > acceptability steets. The aim is to show how many people from each 
> >> > building can reach different building in radius of 150 meters (so i can 
> >> > find the density of the pedestrian routes).
> >> > 
> >> > I run Urban network analysis but It shows similar mistake several times. 
> >> > The UNA tool is free plugin that i downloaded, it's not from the default 
> >> > intalled in ArcGIS packed. It ask for integer data.
> >> > I checked the type of the attributes, it's all integer. PLus it's all in 
> >> > geo data based file.
> >> > 
> >> > Unfortunately I don't understand Python, I'm one of those who use ArcGIS 
> >> > as sample customer.
> >> > 
> >> > This is the mistake:
> >> > 
> >> > Start Time: Fri Jul 27 14:48:32 2018
> >> > Running script Centrality...
> >> > [started] Copying input buildings
> >> > [finished]
> >> > [1 started] Computing adjacency list
> >> > [1 failed]
> >> > Not successful
> >> > Completed script Centrality...
> >> > Succeeded at Fri Jul 27 14:48:36 2018 (Elapsed Time: 4,56 seconds)
> >> > 
> >> > Any suggestions? How it's calling these adjaency list? What could be 
> >> > wrong? I even don't know how to get debugger, so it could give me more 
> >> > information.
> >> > 
> >> > Thank you previously,
> >> > Mira
> >> > 
> >> All you've shown is that says that it was unsuccessful, and it doesn't 
> >> say why.
> >> 
> >> We'd need a lot more info than that!
> >> 
> >> The best way to debug it is to try it with the minimal amount of test 
> >> data that should give a result.
> > 
> > I'm sorry.
> > Could someone help me by telling me how to make these test data? (so i 
> > could send it to You)
> > 
> > I realize the review of the bug is just the customer view. Any chance to 
> > help me out with the test of the process?
> > 
> > Thank you previously. If there is another forum that i have to write to, i 
> > will switch. The fact is that in the Themes for UNA tool or GIS software, 
> > almost no one respond.
> > 
> I'm unfamiliar with ArcGIS, so I doubt I could help.
> 
> It might have generated a log file somewhere that gives more details.

Ok, thank you for the patiance.
I installed PyCharm for trying make some tests. 

I add in a project the .py file for the adjacency list.

That's the main from the debuger:

pydev debugger: process 8904 is connecting

Connected to pydev debugger (build 182.3684.100)
Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm 2018.2\helpers\pydev\pydevd.py", 
line 1664, in 
main()
  File "C:\Program Files\JetBrains\PyCharm 2018.2\helpers\pydev\pydevd.py", 
line 1658, in main
globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\Program Files\JetBrains\PyCharm 2018.2\helpers\pydev\pydevd.py", 
line 1068, in run
pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm 
2018.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "D:/INSTALL/Urban Network Analysis Toolbox 
1.01/src/Centrality/Adjacency_List_Computation.py", line 14, in 
from arcpy import AddField_management
ModuleNotFoundError: No module named 'arcpy'

May I found the following lines from the code?
-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Checking whether type is None

2018-07-24 Thread David Raymond
https://docs.python.org/3.7/library/constants.html

"None
The sole value of the type NoneType..."

"x is None" and "type(x) is type(None)" are equivalent because of that.

I think though that the better way to do the first tests would be to use 
isinstance
https://docs.python.org/3.7/library/functions.html#isinstance

isinstance({}, dict)
isinstance(3, int)

And I suppose if you really wanted:
isinstance(None, type(None))


-Original Message-
From: Python-list 
[mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of 
Tobiah
Sent: Tuesday, July 24, 2018 3:33 PM
To: python-list@python.org
Subject: Checking whether type is None

Consider:

>>> type({}) is dict
True
>>> type(3) is int
True
>>> type(None) is None
False

Obvious I guess, since the type object is not None.
So what would I compare type(None) to?

>>> type(None)

>>> type(None) is NoneType
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'NoneType' is not defined


I know I ask whether:

>>> thing is None

but I wanted a generic test.
I'm trying to get away from things like:

>>> type(thing) is type(None)

because of something I read somewhere preferring
my original test method.


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