Re: Help

2023-11-06 Thread Jason Friedman via Python-list
On Sun, Nov 5, 2023 at 1:23 PM office officce via Python-list <
python-list@python.org> wrote:

> which python version is better to be used and how to make sure it works on
> my window 10 because i downloaded it and it never worked so I uninstall to
> do that again please can you give me the steps on how it will work perfectly
>
>
If you are just starting out, the most recent version is 3.12 and is
probably your best choice.

When you say it never worked, can you describe in more detail what you did
and what error messages you encountered?

This mailing list does not accept screenshots.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Generating documentation with Sphinx

2023-08-28 Thread Jason Friedman via Python-list
def construct_response(exit_code: int, message: str) -> Response:
>> """
>> Construct a Flask-suitable response
>>
>> :param exit_code: 0 or something else
>> :param message: something useful
>> :return: a Flask-suitable response
>> """
>>
>>
>> @app.route(f"/{version}/", methods=[GET, POST])
>> def serve(page) -> Response:
>> """
>> Do some stuff and return 200 or 500
>>
>> :param page: this is a REST endpoint we are advertising to callers
>> :return: a Flask Response generated by construct_response
>> """
>>
>>
>> Question 1: how do I embed in the serve function docstring a link to the
>> construct_response function?
>>
>>
>> Question 2: how do I embed in, for example, lib/stuff3.py, a link to the
>> construct_response function?
>>
>>
>> I tried:
>> :ref:`my_project/api/stuff1:construct_response`
>>
>> but that gives an undefined label warning.
>>
>
> I can answer my own Question 1:
> :func:`construct_response`
>

And I can answer my own Question 2:
:func:`my_project.main_application.construct_response`
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Generating documentation with Sphinx

2023-08-28 Thread Jason Friedman via Python-list
And I can answer my own Question 2:
:func:`my_project.main_application.construct_response`

On Mon, Aug 28, 2023 at 1:39 PM Jason Friedman  wrote:

> def construct_response(exit_code: int, message: str) -> Response:
>> """
>> Construct a Flask-suitable response
>>
>> :param exit_code: 0 or something else
>> :param message: something useful
>> :return: a Flask-suitable response
>> """
>>
>>
>> @app.route(f"/{version}/", methods=[GET, POST])
>> def serve(page) -> Response:
>> """
>> Do some stuff and return 200 or 500
>>
>> :param page: this is a REST endpoint we are advertising to callers
>> :return: a Flask Response generated by construct_response
>> """
>>
>>
>> Question 1: how do I embed in the serve function docstring a link to the
>> construct_response function?
>>
>>
>> Question 2: how do I embed in, for example, lib/stuff3.py, a link to the
>> construct_response function?
>>
>>
>> I tried:
>> :ref:`my_project/api/stuff1:construct_response`
>>
>> but that gives an undefined label warning.
>>
>
> I can answer my own Question 1:
> :func:`construct_response`
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Generating documentation with Sphinx

2023-08-28 Thread Jason Friedman via Python-list
>
> def construct_response(exit_code: int, message: str) -> Response:
> """
> Construct a Flask-suitable response
>
> :param exit_code: 0 or something else
> :param message: something useful
> :return: a Flask-suitable response
> """
>
>
> @app.route(f"/{version}/", methods=[GET, POST])
> def serve(page) -> Response:
> """
> Do some stuff and return 200 or 500
>
> :param page: this is a REST endpoint we are advertising to callers
> :return: a Flask Response generated by construct_response
> """
>
>
> Question 1: how do I embed in the serve function docstring a link to the
> construct_response function?
>
>
> Question 2: how do I embed in, for example, lib/stuff3.py, a link to the
> construct_response function?
>
>
> I tried:
> :ref:`my_project/api/stuff1:construct_response`
>
> but that gives an undefined label warning.
>

I can answer my own Question 1:
:func:`construct_response`
-- 
https://mail.python.org/mailman/listinfo/python-list


Generating documentation with Sphinx

2023-08-28 Thread Jason Friedman via Python-list
I have two questions, please (this is after reading
https://docs.readthedocs.io/en/stable/guides/cross-referencing-with-sphinx.html#automatically-label-sections
).


This is my project structure:

my_project
  api
stuff1.py
stuff2.py
  lib
stuff3.py
stuff4.py
  main_application.py


In my_project/main_application.py I have two functions:


def construct_response(exit_code: int, message: str) -> Response:
"""
Construct a Flask-suitable response

:param exit_code: 0 or something else
:param message: something useful
:return: a Flask-suitable response
"""


@app.route(f"/{version}/", methods=[GET, POST])
def serve(page) -> Response:
"""
Do some stuff and return 200 or 500

:param page: this is a REST endpoint we are advertising to callers
:return: a Flask Response generated by construct_response
"""


Question 1: how do I embed in the serve function docstring a link to the
construct_response function?


Question 2: how do I embed in, for example, lib/stuff3.py, a link to the
construct_response function?


I tried:
:ref:`my_project/api/stuff1:construct_response`

but that gives an undefined label warning.
-- 
https://mail.python.org/mailman/listinfo/python-list


Context manager for database connection

2023-08-23 Thread Jason Friedman via Python-list
I want to be able to write code like this:

with Database() as mydb:
conn = mydb.get_connection()
cursor = conn.get_cursor()
cursor.execute("update table1 set x = 1 where y = 2")
cursor.close()
cursor = conn.get_cursor()
cursor.execute("update table2 set a = 1 where b = 2")
cursor.close()

I'd like for both statements to succeed and commit, or if either fails to
stop and for all to rollback.

Is what I have below correct?


import jaydebeapi as jdbc
class Database:
database_connection = None

def __init__(self, auto_commit: bool = False):
self.database_connection = jdbc.connect(...)
self.database_connection.jconn.setAutoCommit(auto_commit)

def __enter__(self) -> jdbc.Connection:
return self

def __exit__(self, exception_type: Optional[Type[BaseException]],
 exception_value: Optional[BaseException],
 traceback: Optional[types.TracebackType]) -> bool:
if exception_type:
self.database_connection.rollback()
else:
self.database_connection.commit()
self.database_connection.close()

def get_connection(self) -> jdbc.Connection:
return self.database_connection
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to find the full class name for a frame

2023-08-04 Thread Jason Friedman via Python-list
>
> Jason Friedman wrote at 2023-8-3 21:34 -0600:
> > ...
> >my_frame = inspect.currentframe()
> > ...
> >My question is: let's say I wanted to add a type hint for my_frame.
>
> `my_frame` will be an instance of `Types.FrameType`.
>

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


Re: How to find the full class name for a frame

2023-08-04 Thread Jason Friedman via Python-list
> My question is: let's say I wanted to add a type hint for my_frame.
> >
> > my_frame: some_class_name = inspect.currentframe()
> >
> > What would I put for some_class_name?
> > "frame" (without quotations) is not recognized,
> > Nor is inspect.frame.
>
> We know Python code is executed in an execution frame.
> (https://docs.python.org/3/reference/executionmodel.html?highlight=frame)
>
> We are told "Frame objects Frame objects represent execution frames."
> (https://docs.python.org/3/reference/datamodel.html?highlight=frame).
> The word "represent" conflicts with the idea of "are".
>
> 'Under the hood' inspect calls sys._current_frames()
> (https://docs.python.org/3/library/sys.html?highlight=frame). That code
> is:
>
> def _getframe(*args, **kwargs): # real signature unknown
>  """
>  Return a frame object from the call stack.
>
>  If optional integer depth is given, return the frame object that many
>  calls below the top of the stack.  If that is deeper than the call
>  stack, ValueError is raised.  The default for depth is zero, returning
>  the frame at the top of the call stack.
>
>  This function should be used for internal and specialized purposes
>  only.
>  """
>  pass
>
> Which rather suggests that if the sys library doesn't know the
> signature, then neither typing nor we mere-mortals are going to do so,
> either.
>
>
> Theory: the concept of a frame does not really exist at the Python-level
> (remember "represents"). Frames (must) exist at the C-level
> (
> https://docs.python.org/3/c-api/frame.html?highlight=frame#c.PyFrameObject)
>
> of the virtual-machine - where typing is not a 'thing'.
>
>
> It's an interesting question. Perhaps a better mind than mine can give a
> better answer?
>
>
Thank you DN.

My ultimate goal is a function I'd put in my main library which other
functions could leverage, something like:

function_in_another_file(arg):
logger.info(my_main_module.render_calling_info(inspect.stack(),
inspect.currentframe())
# Now do the work
-- 
https://mail.python.org/mailman/listinfo/python-list


How to find the full class name for a frame

2023-08-03 Thread Jason Friedman via Python-list
import inspect

def my_example(arg1, arg2):
print(inspect.stack()[0][3])
my_frame = inspect.currentframe()
args,_,_,values = inspect.getargvalues(my_frame)
args_rendered = [f"{x}: {values[x]}" for x in args]
print(args_rendered)

my_example("a", 1)


The above "works" in the sense it prints what I want, namely the method
name (my_example) and the arguments it was called with.

My question is: let's say I wanted to add a type hint for my_frame.

my_frame: some_class_name = inspect.currentframe()

What would I put for some_class_name?
"frame" (without quotations) is not recognized,
Nor is inspect.frame.
-- 
https://mail.python.org/mailman/listinfo/python-list


Trouble with defaults and timeout decorator

2023-06-24 Thread Jason Friedman via Python-list
I'm writing a database connectivity module to be used by other modules and
leveraging the jaydebeapi module.
>From what I can tell jaydebeapi contains no built-in timeout capability, so
then I turned to https://pypi.org/project/timeout-decorator/.
My goal is to have a default timeout of, say, 10 seconds, which can be
overridden by the caller.


import jaydebeapi
from timeout_decorator import timeout

class Database:
database_connection = None
database_name, user_name, password, host, port = stuff
timeout = None

def __init__(self, timeout=10):
self.timeout = timeout

@timeout(self.timeout)
def get_connection(self):
if not self.database_connection:
self.database_connection = jaydebeapi.connect(some_args)
return self.database_connection


The trouble occurs on line 12 with:
NameError: name 'self' is not defined
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Match statement with literal strings

2023-06-07 Thread Jason Friedman via Python-list
>
> The bytecode compiler doesn't know that you intend RANGE
> to be a constant -- it thinks it's a variable to bind a
> value to.
>
> To make this work you need to find a way to refer to the
> value that isn't just a bare name. One way would be to
> define your constants using an enum:
>
> class Options(Enum):
> RANGE = "RANGE"
> MANDATORY = "MANDATORY"
>
> match stuff:
> case Options.RANGE:
>...
> case Options.MANDATORY:
>...
>

Got it, thank you.

On Wed, Jun 7, 2023 at 6:01 PM Greg Ewing via Python-list <
python-list@python.org> wrote:

> On 8/06/23 10:18 am, Jason Friedman wrote:
> > SyntaxError: name capture 'RANGE' makes remaining patterns unreachable
>
> The bytecode compiler doesn't know that you intend RANGE
> to be a constant -- it thinks it's a variable to bind a
> value to.
>
> To make this work you need to find a way to refer to the
> value that isn't just a bare name. One way would be to
> define your constants using an enum:
>
> class Options(Enum):
> RANGE = "RANGE"
> MANDATORY = "MANDATORY"
>
> match stuff:
> case Options.RANGE:
>...
> case Options.MANDATORY:
>...
>
> --
> Greg
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Match statement with literal strings

2023-06-07 Thread Jason Friedman via Python-list
This gives the expected results:

with open(data_file, newline="") as reader:
csvreader = csv.DictReader(reader)
for row in csvreader:
#print(row)
match row[RULE_TYPE]:
case "RANGE":
print("range")
case "MANDATORY":
print("mandatory")
case _:
print("nothing to do")

This:

RANGE = "RANGE"
MANDATORY = "MANDATORY"
with open(data_file, newline="") as reader:
csvreader = csv.DictReader(reader)
for row in csvreader:
#print(row)
match row[RULE_TYPE]:
case RANGE:
print("range")
case MANDATORY:
print("mandatory")
case _:
print("nothing to do")

Gives (and I don't understand why):

SyntaxError: name capture 'RANGE' makes remaining patterns unreachable
-- 
https://mail.python.org/mailman/listinfo/python-list


Best practice for database connection

2023-05-31 Thread Jason Friedman
I'm trying to reconcile two best practices which seem to conflict.

1) Use a _with_ clause when connecting to a database so the connection is
closed in case of premature exit.

class_name = 'oracle.jdbc.OracleDriver'
url = f"jdbc:oracle:thin:@//{host_name}:{port_number}/{database_name}"
with jdbc.connect(class_name, url, [user_name, password],
jdbc_jar_file.as_posix()) as connection:
logger.info(f"Connected.")

2) Use self-made functions to streamline code. For example, there are
several places I need to know if the database object is a particular type,
so I create a function like this:

foobar_set = set()
...
def is_foobar(connection: jdbc.Connection, name: str) -> bool:
"""
:param connection: connection object
:param name: owner.object
:return: True if this object is of type foobar
"""
global foobar_set
if not foobar_set:
query = f"""select stuff from stuff"""
cursor = connection.cursor()
cursor.execute(query)
for owner, object_name in cursor.fetchall():
foobar_set.add(f"{owner}.{object_name}")
cursor.close()
return name.upper() in foobar_set


But that requires that I call is_foobar() with a connection object.

Essentially I'd like a function that leverages the one connection I create
at the beginning using a with clause.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python 3.11.0 installation and Tkinter does not work

2022-11-23 Thread Jason Friedman
>
> I want learn python for 4 weeks and have problems, installing Tkinter. If
> I installed 3.11.0 for my windows 8.1 from python.org and type
>
>   >>> import _tkinter
>   > Traceback (most recent call last):
>   >File "", line 1, in 
>   > ImportError: DLL load failed while importing _tkinter: Das angegebene
>   > Modul wurde nicht gefunden.
>
>   > So I it is a tkinter Problem and I tried this:
>
>  >  >>> import _tkinter
>  > Traceback (most recent call last):
>   >File "", line 1, in 
>   > ImportError: DLL load failed while importing _tkinter: Das angegebene
>   > Modul wurde nicht gefunden.
>
> How can I fix this and make it work?
>

Have a look at
https://stackoverflow.com/questions/25905540/importerror-no-module-named-tkinter
.

Most of the answers are for Linux/Mac, but there are Windows answers there,
too.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Set git config credential.helper cache and timeout via python

2021-12-11 Thread Jason Friedman
>
>
> Hey All,
>
> I have a set of bash and python scripts that all interact with a remote
> git repository.
>
>
> https://gitpython.readthedocs.io/en/stable/reference.html?highlight=cache#git.index.fun.read_cache
> https://pypi.org/project/git-credential-helpers/
>
> But neither means appears to have or be able to do what I'm interested
> in.
>

I guess I should have read more carefully :(

Ignore my second link. Maybe the first (https://pypi.org/search/?q=git)
will be helpful?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Set git config credential.helper cache and timeout via python

2021-12-11 Thread Jason Friedman
> Hey All,
>
> I have a set of bash and python scripts that all interact with a remote
> git repository.
>

This does not exactly answer your question, but whenever I have wanted to
interact with (popular) software via Python I have checked to see if
someone has already written that code for me.

https://pypi.org/search/?q=git

https://gitpython.readthedocs.io/en/stable/tutorial.html
-- 
https://mail.python.org/mailman/listinfo/python-list


Determine what the calling program is

2021-04-18 Thread Jason Friedman
I should state at the start that I have a solution to my problem. I am
writing to see if there is a better solution.

I have a program that runs via crontab every five minutes. It polls a
Box.com folder for files and, if any are found, it copies them locally and
performs a computation on them that can exceed five minutes. It pushes the
results back up to Box. (Box.com ensures that only complete files are
visible when I poll.) Files are dropped into this Box.com folder rarely,
but to ensure a good customer experience I do not want to set my crontab to
run less frequently. My hardware cannot support multiple simultaneous
computations.

I have written a piece of code to detect if more than 1 instance of my
program is running, and I put this code into a separate module (support.py)
so that other programs can use it.

support.py contains:

import sys
def check(calling_program):
import psutil
# some logic here to count
# count = N
if count > 1:
print(f"I was called by {calling_program}.")
sys.exit()
if __name__ == "__main__":
check()


actual-program.py contains:

import support.py
support.check(__file__)
# Poll, and if files download, perform expensive computations, push results


To me it would be more elegant to be able to do something like this:

def check():
# Something here that tells me the name of the calling program
import psutil
# ...

And then the calling program just does:
support.check()
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What's the meaning the "backlog" in the socket.listen(backlog) is?

2021-02-16 Thread Jason Friedman
>
> I set listen(2) and expect to see "error" when more clients than "the
> maximum number of queued connections" trying to connect the server. But, no
> error!! Even 4 clients can run normally without problem.
>
> Am I misunderstanding the meaning of this argument?
>

https://docs.python.org/3/library/socket.html#socket.socket.listen:
Enable a server to accept connections. If backlog is specified ... it
specifies the number of unaccepted connections that the system will allow
before refusing new connections.

So I read that to say that the listen method will accept as many connection
as it can handle, and will put into a backlog those it cannot. Your
argument of 2 tells the method to allow up to 2 not-yet-accepted connection
requests.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: For example: Question, moving a folder (T061RR7N1) containing a Specific file (ReadCMI), to folder: C:\\...\DUT0

2021-01-27 Thread Jason Friedman
>
>
> for path, dir, files in os.walk(myDestinationFolder):
> # for path, dir, files in os.walk(destfolder):
> print('The path is %s: ', path)
> print(files)
> os.chdir(mySourceFolder)
> if not os.path.isfile(myDestinationFolder + file):
> #  if not os.path.isfile(destfolder + file):
> print('The file is %s: ', file)
> shutil.copytree(mySourceFolder, myDestinationFolder)
> #  os.rename(path + '\\' + file, myDestinationFolder + file)
> #  os.rename(path + '\\' + file, destfolder + file)
> os.rename(path + '\\' + file, myDestinationFolder + file)
> elif os.path.isfile(myDestinationFolder + file):
> #  os.rename(path + '\\' + file, destfolder + file)
> shutil.copytree(mySourceFolder, myDestinationFolder)
> So I would very much appreciate your ideas on the above
> statements!Because...I think I've got the wrong function (os.path.isfile),
> when I should be (s/b) using a stepped approach!Note: program allows input
> of ID = T061RR7N1 (for example)1) find the folder containing "file": where
> folder = T061RR7N1, and file is "ReadCMI"; if TRUE, shutil.copytree
> C:\\...\T061RR7N1\ReadCMI (TO) C:\\...\DUT[?], where [?] is a num from 0 -
> 15.2) append to C:\\...\DUT[?]\T061RR7N1, which contains "ReadCMI"!
>
> and would you mind telling me why this works (in every example I've found
> on the internet): r'C:\\anyfolder\\anyotherfolder\\'...what does the "r"
> signify? If it's 'read', why can't I use the 'a' for append?
>

A few answers and then a question ...

"r" in this context is a raw literal, see for example
https://www.journaldev.com/23598/python-raw-string.

And you probably don't need it. Python and Windows are happy with
"c:/path/to/file.txt" for a file path.
Some of your code might look end up looking like:
full_path = parent + "/" + file

See also the os.path.join method (
https://docs.python.org/3/library/os.path.html#os.path.join) and the
pathlib module (
https://docs.python.org/3/library/pathlib.html#module-pathlib).

If you truly want to move the folder, as opposed to copying it, you can use
shutil.move.

Does the target folder already exist? What is the rule for determining what
the target folder should be named?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Copying column values up based on other column values

2021-01-03 Thread Jason Friedman
>
> import numpy as np
> import pandas as pd
> from numpy.random import randn
> df=pd.DataFrame(randn(5,4),['A','B','C','D','E'],['W','X','Y','Z'])
>
> W X Y Z
> A -0.183141 -0.398652 0.909746 0.332105
> B -0.587611 -2.046930 1.446886 0.167606
> C 1.142661 -0.861617 -0.180631 1.650463
> D 1.174805 -0.957653 1.854577 0.335818
> E -0.680611 -1.051793 1.448004 -0.490869
>
> is there a way to create a column S - which will copy column column Y
> values UP- if values in column Y are above 1 - otherwise return new value
> above zero?.I made this manually:
>
> S:
>
> A 1.446886
> B 1.446886
> C 1.854577
> D 1.854577
> E 1.448004
>

Here's one solution. No consideration to performance.

import numpy as np
import pandas as pd
from numpy.random import randn
df=pd.DataFrame(randn(5,4),['A','B','C','D','E'],['W','X','Y','Z'])
print(df)

y_series = df["Y"]
for i in range(len(y_series)):
if i == len(y_series) - 1:
# Last one, nothing to copy
break
if y_series[i+1] > 1:
y_series[i] = y_series[i+1]

df["Y"] = y_series
print(df)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Reading binary data with the CSV module

2020-11-29 Thread Jason Friedman
>
> csv.DictReader appears to be happy with a list of strings representing
> the lines.
>
> Try this:
>
> contents = source_file.content()
>
> for row in csv.DictReader(contents.decode('utf-8').splitlines()):
>  print(row)
>

Works great, thank you! Question ... will this form potentially use less
memory?

for row in
csv.DictReader(source_file.content().decode('utf-8').splitlines()):
print(row)
-- 
https://mail.python.org/mailman/listinfo/python-list


Reading binary data with the CSV module

2020-11-29 Thread Jason Friedman
Using the Box API:


print(source_file.content())


returns


b'First Name,Last Name,Email Address,Company,Position,Connected
On\nPeter,van

(and more data, not pasted here)


Trying to read it via:

with io.TextIOWrapper(source_file.content(), encoding='utf-8') as text_file:

reader = csv.DictReader(text_file)

for row in reader:

print(row)


Getting this error:

Traceback (most recent call last):
  File "/home/jason/my_box.py", line 278, in 
with io.TextIOWrapper(source_file.content(), encoding='utf-8') as
text_file:
AttributeError: 'bytes' object has no attribute 'readable'
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: filtering out warnings

2020-11-29 Thread Jason Friedman
>
> The Box API is noisy ... very helpful for diagnosing, and yet for
> production code I'd like less noise.
>
> I tried this:
>
> warnings.filterwarnings(
> action='ignore',
> # category=Warning,
> # module=r'boxsdk.*'
> )
>
> but I still see this:
>
> WARNING:boxsdk.network.default_network:"POST
> https://api.box.com/oauth2/token; 400 83
> {'Date': 'Sat, 28 Nov 2020 04:30:03 GMT', 'Content-Type':
> 'application/json', 'Transfer-Encoding': 'chunked', 'Connection':
> 'keep-alive', 'Strict-Transport-Security': 'max-age=31536000',
> 'Set-Cookie': 'box_visitor_id=5fc1d24b134ce6.76522820; expires=Sun,
> 28-Nov-2021 04:30:03 GMT;
>

Posted and answered on StackOverflow (
https://stackoverflow.com/questions/65061846/filtering-out-warnings-from-box-sdk-python/
).

Answer was:
import logging
logging.getLogger('boxsdk').setLevel(logging.CRITICAL)
-- 
https://mail.python.org/mailman/listinfo/python-list


filtering out warnings

2020-11-27 Thread Jason Friedman
The Box API is noisy ... very helpful for diagnosing, and yet for
production code I'd like less noise.

I tried this:

warnings.filterwarnings(
action='ignore',
# category=Warning,
# module=r'boxsdk.*'
)

but I still see this:

WARNING:boxsdk.network.default_network:"POST
https://api.box.com/oauth2/token; 400 83
{'Date': 'Sat, 28 Nov 2020 04:30:03 GMT', 'Content-Type':
'application/json', 'Transfer-Encoding': 'chunked', 'Connection':
'keep-alive', 'Strict-Transport-Security': 'max-age=31536000',
'Set-Cookie': 'box_visitor_id=5fc1d24b134ce6.76522820; expires=Sun,
28-Nov-2021 04:30:03 GMT; Max-Age=31536000; path=/; domain=.box.com;
secure, bv=OPS-44131; expires=Sat, 05-Dec-2020 04:30:03 GMT;
Max-Age=604800; path=/; domain=.app.box.com; secure, cn=87; expires=Sun,
28-Nov-2021 04:30:03 GMT; Max-Age=31536000; path=/; domain=.app.box.com;
secure, site_preference=desktop; path=/; domain=.box.com; secure',
'Cache-Control': 'no-store'}
{'error': 'invalid_client',
 'error_description': 'The client credentials are invalid'}

My code as written I think should filter ALL warnings. I'll eventually want
to restrict the filter to only warnings generated by the Box SDK.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: try/except in loop

2020-11-27 Thread Jason Friedman
>
>
>> I'm using the Box API (
>> https://developer.box.com/guides/tooling/sdks/python/).
>> I can get an access token, though it expires after a certain amount of
>> time. My plan is to store the access token on the filesystem and use it
>> until it expires, then fetch a new one. In the example below assume I have
>> an expired access token.
>>
>> # This next line does not throw an error:
>> client.folder('0').get_items()
>>
>> # But iteration does (maybe this is a lazy fetch?)
>> for _ in client.folder('0').get_items():
>> logger.debug("Using existing access token.")
>> return access_token
>>
>> # So I use try/except
>> try:
>> for _ in client.folder('0').get_items():
>> logger.debug("Using existing access token.")
>> return access_token
>> except boxsdk.exception.BoxAPIException:
>> pass # access token invalid, let's get one
>> else:
>> pass # access token invalid, let's get one
>>
>> # When running the debugger the except clause seems to catch the first
>> throw, but the loop I think continues, throws the error again, and that
>> second throw is not caught.
>>
>
> It would appear that get items is a generator which uses the token exactly
> once when it is first started; subsequent calls to the generator all use
> the same token. You need to test the token; if it fails,
> obtain a new one, then start the loop.
>

# Here's the solution I came up with:
try:
list(client.folder('0').get_items())
logger.debug("Using existing access token.")
return access_token
except boxsdk.exception.BoxAPIException:
pass # access token invalid, let's get one

# And for those who happen to be using the Box API, this command avoids all
that and is less expensive:
try:
client.user().get()
logger.debug("Using existing access token.")
return access_token
except boxsdk.exception.BoxAPIException:
pass # access token invalid, let's get one
-- 
https://mail.python.org/mailman/listinfo/python-list


try/except in loop

2020-11-27 Thread Jason Friedman
I'm using the Box API (https://developer.box.com/guides/tooling/sdks/python/).
I can get an access token, though it expires after a certain amount of
time. My plan is to store the access token on the filesystem and use it
until it expires, then fetch a new one. In the example below assume I have
an expired access token.


# This next line does not throw an error:

client.folder('0').get_items()


# But iteration does (maybe this is a lazy fetch?)

for _ in client.folder('0').get_items():

logger.debug("Using existing access token.")

return access_token


# So I use try/except

try:

for _ in client.folder('0').get_items():

logger.debug("Using existing access token.")

return access_token

except boxsdk.exception.BoxAPIException:

pass # access token invalid, let's get one

else:

pass # access token invalid, let's get one


# When running the debugger the except clause seems to catch the first
throw, but the loop I think continues, throws the error again, and that
second throw is not caught.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: MERGE SQL in cx_Oracle executemany

2020-10-17 Thread Jason Friedman
>
> I'm looking to insert values into an oracle table (my_table) using the
> query below. The insert query works when the PROJECT is not NULL/empty
> (""). However when PROJECT is an empty string(''), the query creates a new
> duplicate row every time the code is executed (with project value
> populating as null). I would like to modify my query so a new row is not
> inserted when all column values are matched (including when project code is
> null).
> I'm guessing I would need to include a "when matched" statement, but not
> too sure on how to get this going. Would appreciate help with this, thanks.
>
> ```
> con = cx_Oracle.connect(connstr)
> cur = con.cursor()
> rows = [tuple(x) for x in df.values]
> cur3.executemany('''merge into my_table
> using dual
> on (YEAR = :1 and QUARTER = :2 and CODE = :3 and AMOUNT = :4 and DATE = :5
> and COMMENTS = :6 and PROJECT = :7)
> when not matched then insert values (:1, :2, :3, :4, :5, :6, :7)
> ''',rows)
> con.commit()
> cur.close()
> con.close()
>

Perhaps the issue is that NULL is not equal to anything. Oracle provides
the IS NULL function to determine if a value is NULL.

Note also you define "cur" but executemany with "cur3".

And is "rows = [tuple(x) for x in df.values]" what you want? Print it.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: importlib: import X as Y; from A import B

2020-08-10 Thread Jason Friedman
>
> import pandas; pd = pandas
>
> >df = pd.from_csv (...)
> >from selenium import webdriver
>
> import selenium.webdriver; webdriver = selenium.webdriver
>

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


importlib: import X as Y; from A import B

2020-08-08 Thread Jason Friedman
I have some code I'm going to share with my team, many of whom are not yet
familiar with Python. They may not have 3rd-party libraries such as pandas
or selenium installed. Yes I can instruct them how to install, but the path
of least resistance is to have my code to check for missing dependencies
and attempt to install for them. This code works as desired:

import importlib
import subprocess

PIP_EXE = "/opt/python/bin/pip3"

for module_name in ("module1", "module2", "module3"):
try:
importlib.import_module(module_name)
except ModuleNotFoundError:
install_command = f"{PIP_EXE} install {module_name}"
status, output = subprocess.getstatusoutput(install_command)
if not status:
importlib.import_module(module_name)
print(f"Successfully installed {module_name}.")
else:
print(f"Error when attempting to install {module_name}:
{output}")

The cherry-on-top would be to import with the "aliasing" and "from" they
will most likely see on the web, so that my code matches what they see
there. In other words, instead of:

import pandas
df = pandas.from_csv (...)
import selenium
browser = selenium.webdriver.Firefox()

on the web they will typically see:

import pandas as pd
df = pd.from_csv (...)
from selenium import webdriver
browser = webdriver.Firefox()

I don't see anything in the importlib module documentation that supports
this.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Consumer trait recognition

2020-05-04 Thread Jason Friedman
>
> I constructed a lexicon for words that show how different words are linked
> to consumer traits and motivations (e.g. Achievement and Power Motivation).
> Now I have collected a large amount of online customer reviews and want to
> match each review with the word definitions of the consumer traits and
> motivations in order to identify whether they influence the consumer
> preferences.
>
> How do I that? Both the lexicons and the customer reviews are csv files.
>
> I was going to reply that you should consider the https://www.nltk.org/ 
> mailing
list, but I see you have already done so (
https://groups.google.com/forum/#!topic/nltk-users/LWllpEiW65k).

Someone replied asking for what you have so far, "preferably in code". You
might also show on that forum the first 5-or-so lines of your two files,
along with what you expect the first 5-or-so lines of output to look like.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: TensorFlow with 3.8.1

2020-01-21 Thread Jason Friedman
You have another thread on this list that refers to general Python
installation issues, so you'll need to work through that.

I'm writing in this thread to say that tensorflow does not (today) support
Python 3.8, you'll want to try 3.7, assuming that tensorflow is a critical
piece for you: https://www.tensorflow.org/install/pip.

On Mon, Jan 20, 2020 at 3:30 AM Fahad Qayyum 
wrote:

> Hello,
>
> Hope this email finds you well. I am having an issue with the installation
> of TensorFlow on python 3.8.1. I want to work on TensorFlow, please let me
> know how can I install it. Give me the compete guide if possible.
>
>
> Regards
> Fahad Qayyum
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Grepping words for match in a file

2019-12-28 Thread Jason Friedman
>
>
> I have some lines in a text file like
> ADD R1, R2
> ADD3 R4, R5, R6
> ADD.MOV R1, R2, [0x10]
> 

Actually I want to get 2 matches. ADD R1, R2 and ADD.MOV R1, R2, [0x10]
> because these two lines are actually "ADD" instructions. However, "ADD3" is
> something else.
>

>>> s = """ADD R1, R2
... ADD3 R4, R5, R6
... ADD.MOV R1, R2, [0x10]"""
>>> import re
>>> pattern = re.compile(r"^ADD\b.+")
>>> for line in s.splitlines():
... if re.search(pattern, line):
... print(line)
...
ADD R1, R2
ADD.MOV R1, R2, [0x10]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Logistic Regression Define X and Y for Prediction

2019-11-13 Thread Jason Friedman
>
>
> When I define the X and Y values for prediction in the train and test
> data, should I capture all the columns that has been "OneHotEncoded" (that
> is all columns with 0 and 1) for the X and Y values???
>

You might have better luck asking on Stackoverflow, per the Pandas
instructions: https://pandas.pydata.org/community.html.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Congratulations to @Chris

2019-10-26 Thread Jason Friedman
>
> Chris Angelico: [PSF's] 2019 Q2 Community Service Award Winner
> http://pyfound.blogspot.com/2019/10/chris-angelico-2019-q2-community.html
>
> ...and for the many assistances and pearls of wisdom he has contributed
> 'here'!
> --
> Regards,
> =dn
>
> Agreed.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: fileinput module not yielding expected results

2019-09-07 Thread Jason Friedman
>
> If you're certain that the headers are the same in each file,
> then there's no harm and much simplicity in reading them each
> time they come up.
>
>  with fileinput ...:
>  for line in f:
>  if fileinput.isfirstline():
>  headers = extract_headers(line)
>  else:
>  pass # process a non-header line here
>
> Yes, the program will take slightly longer to run.  No, you won't
> notice it.
>
> Ah, thank you Dan. I followed your advice ... the working code:

with fileinput.input(sys.argv[1:]) as f:
reader = csv.DictReader(f)
for row in reader:
if fileinput.isfirstline():
continue
for key, value in row.items():
pass #processing

I was hung up on that continue on firstline, because I thought that would
skip the header of the first file. I think now the csv.DictReader(f)
command consumes it first.
-- 
https://mail.python.org/mailman/listinfo/python-list


fileinput module not yielding expected results

2019-09-07 Thread Jason Friedman
import csv
import fileinput
import sys

print("Version: " + str(sys.version_info))
print("Files: " + str(sys.argv[1:]))

with fileinput.input(sys.argv[1:]) as f:
for line in f:
print(f"File number: {fileinput.fileno()}")
print(f"Is first line: {fileinput.isfirstline()}")


I run this:
$ python3 program.py ~/Section*.csv > ~/result

I get this:
$ grep "^Version" ~/result
Version: sys.version_info(major=3, minor=7, micro=1, releaselevel='final',
serial=0)

$ grep "^Files" ~/result
Files: ['/home/jason/Section01.csv', '/home/jason/Section02.csv',
'/home/jason/Section03.csv', '/home/jason/Section04.csv',
'/home/jason/Section05.csv', '/home/jason/Section06.csv']

$ grep -c "True" ~/result
6

That all makes sense to me, but this does not:

$ grep "File number" ~/result | sort | uniq
File number: 3

I expected that last grep to yield:
File number: 1
File number: 2
File number: 3
File number: 4
File number: 5
File number: 6

My ultimate goal is as follows. I have multiple CSV files, each with the
same header line. I want to read the header line from the first file and
ignore it for subsequent files.

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


Re: How to use regex to search string between {}?

2019-08-28 Thread Jason Friedman
>
> If I have path: /home/admin/hello/yo/{h1,h2,h3,h4}
>
> import re
> r = re.search('{.}', path)
> # r should be ['h1,h2,h3,h4'] but I fail
>
> Why always search nothing?
>

A site like http://www.pyregex.com/ allows you to check your regex with
slightly fewer clicks and keystrokes than editing your program.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: The Mailing List Digest Project

2019-03-29 Thread Jason Friedman
>
>  Pretty cool.  FYI, the index page (now containing 4 articles) with Google
>> Chrome 72.0.3626.x prompts me to translate to French.  The articles
>> themselves do not.
>>
>
> I'm now getting the translation offer on other web pages with Chrome, not
just this one.
Thus, please ignore my prior posting.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: The Mailing List Digest Project

2019-03-27 Thread Jason Friedman
On Mon, Mar 25, 2019 at 11:03 PM Abdur-Rahmaan Janhangeer <
arj.pyt...@gmail.com> wrote:

> As proposed on python-ideas, i setup a repo to turn mail threads into
> articles.
>
> i included a script to build .md to .html (with syntax highlighting) here
> is the index
>
> https://abdur-rahmaanj.github.io/py-mailing-list-summary/
>
>  Pretty cool.  FYI, the index page (now containing 4 articles) with Google
Chrome 72.0.3626.x prompts me to translate to French.  The articles
themselves do not.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Not Defined error in basic code

2019-03-14 Thread Jason Friedman
On Thu, Mar 14, 2019 at 8:07 AM Jack Dangler  wrote:

> 
>
> class weapon:
>  weaponId
>  manufacturerName
>
>  def printWeaponInfo(self):
>  infoString = "ID: %d Mfg: %s Model: %s" % (self.weaponId,
> self.manufacturerName)
>  return infoString
>
> 
>
> import class_weapon
>
> MyWeapon=weapon()
> MyWeapon.weaponId = 100
> MyWeapon.manufacturerName = "Glock"
>
> print(MyWeapon.printWeaponInfo)
>
> executing 'python3 weaponTrack.py' results in this bailing on the first
> element in the class with "not defined".


Try running your module file by itself, that will give you a clue:

$ python3 class_weapon.py
Traceback (most recent call last):
  File "class_weapon.py", line 1, in 
class weapon:
  File "class_weapon.py", line 2, in weapon
weaponId
NameError: name 'weaponId' is not defined

You have another error in the file which will be revealed when you get it
to compile.

Also, your printWeapon method could renamed to __repr__ or __str__ (see
https://docs.python.org/3/reference/datamodel.html#object.__repr__)

Also most Python programmers would spell MyWeapon as my_weapon (see
https://www.python.org/dev/peps/pep-0008/).
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python Packages Survey

2019-01-19 Thread Jason Friedman
On Fri, Jan 18, 2019 at 5:22 PM Cameron Davidson-Pilon <
cam.davidson.pi...@gmail.com> wrote:

> Hello! I invite you to participate in the Python Packages Survey - it takes
> less than a minute to complete, and will help open source developers
> understand their users' better. Thanks for participating!
>
> https://python-packages-survey.com/
>
> I took the plunge.  Other than smoke coming out of my computer everything
seems to be normal.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: namedtuples anamoly

2018-10-18 Thread Jason Friedman
>
> So now the real question is:  What were you trying to accomplish with
> the assignment?  Tell us, and let's see if we can find a way to
> accomplish yor goal without wrecking the internals of the Grade class.
>
> And depending on your answer to that question, the new Data Classes
feature in 3.7 may be of interest to you:
https://www.python.org/dev/peps/pep-0557/.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Add header at top with email.message

2018-09-15 Thread Jason Friedman
>
> the EmailMessage class of email.message provides the methods
> add_header() and __setitem__() to add a header to a message.
> add_header() effectively calls __setitem__(), which does
> `self._headers.append(self.policy.header_store_parse(name, val))`.  This
> inserts the header at the bottom.
>
> It is, however, sometimes desired to insert a new header at the top of
> an (existing) message.  This API doesn’t directly allow this.  In my
> opinion, add_header() should have a flag at_top=False or similar, so
> that one can get this behaviour (it’ll be a bit difficult with
> __setitem__).  What do you think about this?  Is there a feasible way to
> do this and change the library?  Should I post it somewhere where the
> devs can hear it and suggest that?\
>

I see this in the docs at
https://docs.python.org/3/library/email.message.html#email.message.EmailMessage.get_unixfrom
:

The following methods implement the mapping-like interface for accessing
the message’s headers. Note that there are some semantic differences
between these methods and a normal mapping (i.e. dictionary) interface. For
example, in a dictionary there are no duplicate keys, but here there may be
duplicate message headers. Also, in dictionaries there is no guaranteed
order to the keys returned by keys(), but in an EmailMessage object,
headers are always returned in the order they appeared in the original
message, or in which they were added to the message later. Any header
deleted and then re-added is always appended to the end of the header list.

I suppose you already figured out that you can call __delitem__() to clear
the headers and add them back in whatever order you like.
I'm interested in learning more about your use case.  Do you have a third
party with fixed logic that requires the headers in a particular order?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Bottle framework references

2018-08-24 Thread Jason Friedman
>
>
> > On 22 Aug 2018, at 8:38 am, Jason Friedman  wrote:
> >
> >>
> >> I am building up the microsite based on Bottle framework now.
> >> Any references/books? I am unfamiliar with this framework yet.
> >>
> >> I have used it with success.  The online documentation was sufficient
> for
> > my needs, at least.
> > --
>
> https://bottlepy.org/docs/dev/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Bottle framework references

2018-08-21 Thread Jason Friedman
>
> I am building up the microsite based on Bottle framework now.
> Any references/books? I am unfamiliar with this framework yet.
>
> I have used it with success.  The online documentation was sufficient for
my needs, at least.
-- 
https://mail.python.org/mailman/listinfo/python-list


defaultdict and datetime

2018-08-18 Thread Jason Friedman
$ python3
Python 3.6.1 (default, Apr  8 2017, 09:56:20)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import collections, datetime
>>> x = collections.defaultdict(int)
>>> x['something']
0
>>> x = collections.defaultdict(datetime.datetime)
>>> x['something']
Traceback (most recent call last):
  File "", line 1, in 
TypeError: Required argument 'year' (pos 1) not found

I would like to have a dictionary where the default value is a
datetime.datetime object, preferably something small like January 1, year 1.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to set timeout for os.popen

2018-04-15 Thread Jason Friedman
>
> while 1:
> runner = os.popen("tracert -d www.hello.com")
> o=runner.read()
> print(o)
> runner.close()
> runner = os.popen("tracert -d www.hello.com")
> o=runner.read()
> print(o)
> runner.close()
> runner = os.popen("tracert -d www.hello.com")
> o=runner.read()
> print(o)
> runner.close()
>
>
> after running over 1 hour, it stop and not return in one of tracert
>
> how to set timeout and know that this is timeout?
>

There are a number of answers on Stackoverflow, for example
https://stackoverflow.com/questions/492519/timeout-on-a-function-call.  The
first few answers there seem to be Nix-specific, with OS-agnostic
multiprocessing answers below.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: # of Months between two dates

2018-04-06 Thread Jason Friedman
>
> >> > It's probably better to write the function yourself according to what
> >> > makes sense in your use-case, and document its behaviour clearly.
> >>
> >>
> > I suggest using the dateutil module (
> > https://pypi.python.org/pypi/python-dateutil) before writing your own.
>
> I'm not seeing a "months between" function in dateutil. Have I missed
> something?
>
> Huh, you are correct.  I've used dateutil often and incorrectly assumed it
does everything.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: # of Months between two dates

2018-04-05 Thread Jason Friedman
>
> > > I've written a function to return the months between date1 and date2
> but
> > > I'd like to know if anyone is aware of anything in the standard library
> > > to do the same?  For bonus points, does anyone know if postgres can do
> > > the same (we use a lot of date/time funcitons in postgres, already, but
> > > didn't see this problem addressed).
>


> > It's probably better to write the function yourself according to what
> > makes sense in your use-case, and document its behaviour clearly.
>

I suggest using the dateutil module (
https://pypi.python.org/pypi/python-dateutil) before writing your own.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Do not promote `None` as the first argument to `filter` in documentation.

2018-03-06 Thread Jason Friedman
On Tue, Mar 6, 2018 at 1:52 AM, Kirill Balunov 
wrote:

>
> I propose to delete all references in the `filter` documentation that the
> first argument can be `None`, with possible depreciation of `None` as the
> the first argument - FutureWarning in Python 3.8+ and deleting this option
> in Python 4. Personally, regarding the last point - depreciation, I do not
> see this as a great necessity, but I do not find that the option with
> `None`
> should be offered and suggested through the documentation. Instead, it is
> better to show an example with using `filter(bool, iterable)` which is
> absolutely
> equivalent, more readable, but a little bit slower.
>
> Currently documentation for `None` case uses `identity function is
> assumed`, what is this `identity` and how it is consistent with
> truthfulness?
>
> In addition, this change makes the perception of `map` and `filter` more
> consistent,with the rule that first argument must be `callable`.
>
> I see only one moment with `None`, since `None` is a keyword, the behavior
> of `filter(None, iterable)` is alsways guaranteed, but with `bool` it is
> not. Nevertheless, we are all adults here.
>

I won't pretend I am qualified to debate the technical aspects here, but
regarding this snippet:

... Instead, it is better to show an example with using `filter(bool,
iterable)` which is absolutely equivalent, more readable ...

as a ordinary Python user I'd be interested in improvements to the
documentation, including suggestions on real-world usage.  For example,
Chris Angelico below says in part:

... that said, though, any use of filter() that involves a lambda
function should
probably become list comps or genexps, so filter itself should only be used
when ...

Kirill, taking deprecation/removal off the table, what changes would you
recommend to the documentation?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: APPLICATION NOT RUNNING.

2018-03-02 Thread Jason Friedman
>
> I try to run an application with the latest version of python that is
> python 3.6.4 (32-bit) ., instead of running the application it only shows
> feel free to mail python-list@python.org if you continue to encounter
> issues,Please help me out thanks.
>

Hello, you might have more success if you restate your question.  Read
http://www.catb.org/esr/faqs/smart-questions.html#intro for information
about how to ask in a way that makes it more likely someone will help.

TL;DR:
Tell us the operating system.  Copy-and-paste into your question what you
typed.  Copy-and-paste into your question what the computer typed in
return.  Don't attach images.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Any users of statistics.mode() here?

2018-02-20 Thread Jason Friedman
> statistics.mode() currently raises an exception if there is more than one
> mode.
>

I am an infrequent user of this package and this function.  My two cents:
* Leave the current behavior as-is.
* Continue to throw an exception for no data.
* Add an argument, named perhaps mutli=False, that if set to True causes
the function to a return a list of modes, including if there is only a
single mode.  statistics.mode((1, 2, 2, 3, 3, 4, 4, 5), multi=True) would
return [2, 3, 4].
* Would still throw StatisticsError if no data or if every element occurs
exactly the same number of times (for example,
statistics.mode(range(10), multi=True))
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Sending Email using examples From Tutorials

2018-01-27 Thread Jason Friedman
>
> import smtplib
> server = smtplib.SMTP('localhost')
> server.sendmail('gg77gal...@yahoo.com',
> """To: gg.gal...@gmail.com
> From: gg77gal...@yahoo.com
>
> Beware the Ides of March.
> """)
> server.quit()
>
> when running this I get the following message. Please help:
>
> Traceback (most recent call last):
>   File "D:\ProgramDev\PythonDev\sendemail.py", line 3, in 
> server = smtplib.SMTP('localhost')
>   File 
> "C:\Users\Admin\AppData\Local\Programs\Python\Python36-32\lib\smtplib.py",
> line 251, in __init__
> (code, msg) = self.connect(host, port)
>   File 
> "C:\Users\Admin\AppData\Local\Programs\Python\Python36-32\lib\smtplib.py",
> line 335, in connect
> self.sock = self._get_socket(host, port, self.timeout)
>   File 
> "C:\Users\Admin\AppData\Local\Programs\Python\Python36-32\lib\smtplib.py",
> line 306, in _get_socket
> self.source_address)
>   File 
> "C:\Users\Admin\AppData\Local\Programs\Python\Python36-32\lib\socket.py",
> line 722, in create_connection
> raise err
>   File 
> "C:\Users\Admin\AppData\Local\Programs\Python\Python36-32\lib\socket.py",
> line 713, in create_connection
> sock.connect(sa)
> ConnectionRefusedError: [WinError 10061] No connection could be made
> because the target machine actively refused it


Your code is fine, but to send mail you need a SMTP server that accepts
connections.  With:
server = smtplib.SMTP('localhost')

your code is saying that you have an SMTP server listening on your
localhost.  I don't think you do.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Nested Loop Triangle

2017-05-25 Thread Jason Friedman
>
> I need the triangle to be in reverse. The assignment requires a nested
> loop to generate a triangle with the user input of how many lines.
>
> Currently, I get answers such as: (A)
> 
> OOO
> OO
> O
>
> When I actually need it to be like this: (B)
> 
>   OOO
>OO
> O
>

Try the rjust string method.

Python 3.6.1 (default, Apr  8 2017, 09:56:20)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("O".rjust(1))
O
>>> print("O".rjust(2))
 O
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: No module named vtkCommonCorePython

2017-05-20 Thread Jason Friedman
>
> I have a problem to finding file in Python path,Anybody knows how to solve
> it?
>
> Unexpected error: 
> Traceback (most recent call last):
>   File 
> "/home/nurzat/Documents/vmtk-build/Install/bin/vmtklevelsetsegmentation",
> line 20, in 
> from vmtk import pypeserver
>   File "/usr/local/lib/python2.7/dist-packages/vmtk-1.3.linux-
> x86_64.egg/vmtk/pypeserver.py", line 15, in 
> import vtk
>   File "/usr/local/lib/python2.7/dist-packages/vmtk-1.3.linux-
> x86_64.egg/vmtk/vtk/__init__.py", line 41, in 
> from .vtkCommonCore import *
>   File "/usr/local/lib/python2.7/dist-packages/vmtk-1.3.linux-
> x86_64.egg/vmtk/vtk/vtkCommonCore.py", line 9, in 
> from vtkCommonCorePython import *
> ImportError: No module named vtkCommonCorePython
>
> I tried a  search and got several hits for this
exact error.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Need help on a project To :"Create a class called BankAccount with the following parameters "

2017-05-12 Thread Jason Friedman
>
>
>> The first section does not do what I think you want:  a list with 7
> options.  It makes a list with one option, then overwrites it with a new
> list with one option, and so on.  You want something like:
> menu_list = [
> "O - open account"
> "L - load details"
> "D - display details"
> "A - Make deposit"
> "W - Make withdraw",
> "S - save"
> "Q - quit"
> ]
>
>
D'oh!

menu_list = [
"O - open account",
"L - load details",
"D - display details",
"A - make deposit",
"W - make withdrawal",
"S - save",
"Q - quit",
]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Need help on a project To :"Create a class called BankAccount with the following parameters "

2017-05-12 Thread Jason Friedman
>
> menu_list = ["O -open account"]
> menu_list =["l - load details"]
> menu_list =["D- display details"]
> menu_list =["A - Make deposit"]
> menu_list =["W- Make withdraw",]
> menu_list =["S - save"]
> menu_list =["Q - quit"]
>
> command = input("command:")
> if command.upper() == "O":
> open_()
> elif(comman.upper() =="l":
> load_()
> elif(comman.upper() =="a":
> deposit_()
> elif(comman.upper() =="W":
> withdraw_()
> elif(comman.upper() =="s":
> save_()
> elif(comman.upper() =="q":
>

There's a lot of code there so I will comment only on the sections above.

The first section does not do what I think you want:  a list with 7
options.  It makes a list with one option, then overwrites it with a new
list with one option, and so on.  You want something like:
menu_list = [
"O - open account"
"L - load details"
"D - display details"
"A - Make deposit"
"W - Make withdraw",
"S - save"
"Q - quit"
]

The second section has mistyped "command".  You also added underscores to
your method names.  Further, some of your match strings are lowercase, and
you only want to compare the first letter anyway.  Try:
command = input("command:")
if   command.upper().startswith("O"):
open()
elif command.upper().startswith("L"):
load()
elif command.upper().startswith("A"):
deposit()
elif command.upper().startswith("W"):
withdraw()
elif command.upper().startswith("S"):
save()
elif command.upper().startswith("Q"):
quit()
-- 
https://mail.python.org/mailman/listinfo/python-list


Not understanding itertools.dropwhile()

2017-04-29 Thread Jason Friedman
< start code >

import itertools

data = """Line1
Line2

Line4
Line5"""

def test_to_start(s):
return "2" in s

for line in itertools.dropwhile(test_to_start, data.splitlines()):
print(line)

< end code >

I expect:

$ python3 dropwhile.py
Line2

Line4
Line5


I get:

$ python3 dropwhile.py
Line1
Line2

Line4
Line5


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


Re: TIC TAE TOE's problem(i am beginner)

2017-04-15 Thread Jason Friedman
>
> P=input("X/O:")
> if P=="X":
> my_func1()
> else:
> my_func2()
>
>
>
> why cant function to print X or O win...
>

As a beginner I'd try to code using Python idioms rather than writing
Python using BASIC idioms.
Try to understand how this code works:
https://codereview.stackexchange.com/questions/108738/python-tic-tac-toe-game
Note that one of the answers on that page has improved the original
poster's code.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: homework confusion

2017-04-12 Thread Jason Friedman
>
> The example command is: Lockable("diary", "under Sam's bed", tiny_key,
> True)
>
> And I keep getting a NameError: tiny_key is not defined.
>
> What do I do?
>

Without knowing what your professor intends this is a guess:  define
tiny_key.  For example

tiny_key = "some string"
thing = Lockable("diary", "under Sam's bed", tiny_key, True)

or

tiny_key = 1234
thing = Lockable("diary", "under Sam's bed", tiny_key, True)

Maybe in the next assignment the professor will ask you to collect by key
the Lockables that you construct.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Moderating the list [was: Python and the need for speed]

2017-04-12 Thread Jason Friedman
>
> However, it's simply a technical fact: the thing which we moderate is the
>> mailing list. We can control which posts make it through from the newsgroup
>> by blocking them at the gateway. But the posts will continue to appear on
>> comp.lang.python which is, as the description says, unmoderated.
>>
>
> TJG


Thank you, Tim and Ethan and the other moderators, for performing that
function.
It makes the reading experience more pleasant for me.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python Command Line Arguments

2017-04-12 Thread Jason Friedman
>
> I have this code which I got from https://www.tutorialspoint.
> com/python/python_command_line_arguments.htm The example works fine but
> when I modify it to what I need, it only half works. The problem is the
> try/except. If you don't specify an input/output, they are blank at the end
> but it shouldn't be.
>
> import getopt
> import sys
>

Hello Ian,
I am guessing you are wanting to parse command-line arguments rather than
particularly wanting to use the getopt module.
If I am correct you might want to spend your time instead learning the
argparse module:
https://docs.python.org/3/library/argparse.html
https://docs.python.org/3/howto/argparse.html
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Online Python Editor with Live Syntax Checking

2017-03-05 Thread Jason Friedman
>
> I made a tool called PythonBuddy (http://pythonbuddy.com/).
>
> I made this so that MOOCs like edX or codecademy could easily embed and
> use this on their courses so students wouldn't have to go through the
> frustrations of setting up a Python environment and jump right into Python
> programming. Also, professors and teachers could easily set up a server and
> allow students to quickly test out their code with PythonBuddy online.
>
> Github repo: https://github.com/ethanche…/OnlinePythonLinterSyntaxChecker
>

Pretty cool.

Your Github link did not work for me.

This looks like Python 2.  Is there a reason you did not go with 3?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: List comprehension

2016-12-30 Thread Jason Friedman
> data = (
>> ... (1,2),
>> ... (3,4),
>> ... )
>>
> [x,y for a in data]
>>   File "", line 1
>> [x,y for a in data]
>>^
>> SyntaxError: invalid syntax
>>
>> I expected:
>> [(1, 2), (3, 4)]
>
>
> Why would you expect that? I would expect the global variables x and y, or
> if they don't exist, a NameError:

Thank you Steve (and everyone) for your answers.

In answer to Steve's question above, it's because I can do this:
>>> for x,y in data:
... pass
...
>>>

But, anyway, I see my error now, and I'm good to go.
-- 
https://mail.python.org/mailman/listinfo/python-list


List comprehension

2016-12-30 Thread Jason Friedman
$ python
Python 3.6.0 (default, Dec 26 2016, 18:23:08)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> data = (
... (1,2),
... (3,4),
... )
>>> [a for a in data]
[(1, 2), (3, 4)]

Now, this puzzles me:

>>> [x,y for a in data]
  File "", line 1
[x,y for a in data]
   ^
SyntaxError: invalid syntax

I expected:
[(1, 2), (3, 4)]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Options for stdin and stdout when using pdb debugger

2016-11-25 Thread Jason Friedman
> I would like to use pdb in an application where it isn't possible to use 
> sys.stdin for input. I've read in the documentation for pdb.Pdb that a file 
> object can be used instead of sys.stdin. Unfortunately, I'm not clear about 
> my options for the file object.
>
> I've looked at rpdb on PyPI, which re-routes stdin and stdout to a socket 
> handler. I can connect to the socket with telnet. This works well.

You can use the code from
https://docs.python.org/3/library/fileinput.html#module-fileinput to
change this code:

for line in sys.stdin.readlines():
process(line)

to this:

import fileinput
for line in fileinput.input():
process(line)

Then, write the data you would expect to come on STDIN to a file and
run your program:

$ python my-program.py myfile

It will work as normal when you run it normally:

$ data-generation-command | python my-program.py
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: passing a variable to cmd

2016-11-06 Thread Jason Friedman
>
> import subprocess
> import shlex
>
> domname = raw_input("Enter your domain name: ");
> print "Your domain name is: ", domname
>
> print "\n"
>
> # cmd='dig @4.2.2.2 nbc.com ns +short'
> cmd="dig @4.2.2.2 %s ns +short", % (domname)
> proc=subprocess.Popen(shlex.split(cmd),stdout=subprocess.PIPE)
> out,err=proc.communicate()
> print(out)
>
> The line that is commented out works fine.  However, I want to substitute
> a variable for "nbc.com".  The command:
>
> cmd="dig @4.2.2.2 %s ns +short", % (domname)
>
> does not work.  I've tried different variations to no avail.  Any advice
> on how to get that variable working?
>

Two things:
1) Please explain what "does not work" mean.  Are you getting an error
message?  If yes include the entire traceback.
2) The line /* cmd="dig @4.2.2.2 %s ns +short", % (domname) */ gives me a
syntax error.  Please copy-and-paste the exact commands you are entering.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Internet Data Handling » mailbox

2016-10-23 Thread Jason Friedman
>
> for message in mailbox.mbox(sys.argv[1]):
> if message.has_key("From") and message.has_key("To"):
> addrs = message.get_all("From")
> addrs.extend(message.get_all("To"))
> for addr in addrs:
> addrl = addr.lower()
> if addrl.find(name) > 0:
> print message
> break
> -


I usually see

if addrl.find(name) > 0:

written as

if name in addrl:
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Stompy

2016-09-25 Thread Jason Friedman
>
> My error:
>> Traceback (most recent call last):
>>   File "temp.py", line 8, in 
>> my_stomp.connect(AMQ_USERNAME, AMQ_PASSWORD)
>>   File "/lclapps/oppen/thirdparty/stompy/stomp.py", line 48, in connect
>> self.frame.connect(self.sock, username=username, password=password,
>> clientid=clientid)
>>   File "/lclapps/oppen/thirdparty/stompy/frame.py", line 92, in connect
>> self.send_frame(frame.as_string())
>>   File "/lclapps/oppen/thirdparty/stompy/frame.py", line 250, in
>> send_frame
>> self.sock.sendall(frame)
>> TypeError: 'str' does not support the buffer interface
>>
>> I expected you've used Unicode strings for AMQ_USERNAME and AMQ_PASSWORD,
> but if Stompy was written for Python 2, it'll be expecting bytestrings
> instead.
>
> You'll need to do more work on Stompy to fix the parts that 2to3 missed.


Thanks for the advice, I will try that.

I will also try https://pypi.python.org/pypi/stompest/.
-- 
https://mail.python.org/mailman/listinfo/python-list


Stompy

2016-09-25 Thread Jason Friedman
My goal is to send messages to an AMQ server using Python 3.3.  I found
Stompy and performed
2to3-3.3 before building.  I am open to non-Stompy solutions.

My code:
from stompy.stomp import Stomp
my_stomp = Stomp(AMQ_HOST, AMQ_PORT)
my_stomp.connect(AMQ_USERNAME, AMQ_PASSWORD)


My error:
Traceback (most recent call last):
  File "temp.py", line 8, in 
my_stomp.connect(AMQ_USERNAME, AMQ_PASSWORD)
  File "/lclapps/oppen/thirdparty/stompy/stomp.py", line 48, in connect
self.frame.connect(self.sock, username=username, password=password,
clientid=clientid)
  File "/lclapps/oppen/thirdparty/stompy/frame.py", line 92, in connect
self.send_frame(frame.as_string())
  File "/lclapps/oppen/thirdparty/stompy/frame.py", line 250, in send_frame
self.sock.sendall(frame)
TypeError: 'str' does not support the buffer interface
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Is Activestate's Python recipes broken?

2016-08-04 Thread Jason Friedman
>
> Log in to Activestate:
>
> https://code.activestate.com/recipes/langs/python/new/
>
> and click "Add a Recipe". I get
>
>
> Forbidden
>
> You don't have permission to access /recipes/add/ on this server.
> Apache Server at code.activestate.com Port 443
>
>
>
> Broken for everyone, or just for me?
>

Broken for me, too.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Quick poll: gmean or geometric_mean

2016-07-09 Thread Jason Friedman
>
> +1 for consistency, but I'm just fine with the short names. It's in the
> statistics module after all, so the context is very narrow and clear and
> people who don't know which to use or what the one does that they find in a
> given piece of code will have to read the docs and maybe fresh up their
> rusty math memory anyway. Longer names don't help much with that.
>
> If further clarity is needed in a given code context that uses a direct
> name import, renaming the function at the same time is easy enough. I often
> do that with "os.path.join", for example, which turns into "join_path" on
> import. Same problem, easy solution.


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


Re: python parsing suggestion

2016-05-30 Thread Jason Friedman
>
> Trying to extract the '1,1,114688:8192' pattern form the below output.
>
> pdb>stdout:
> '3aae5d0-1: Parent Block for 1,1,19169280:8192 (block 1,1,114688:8192)
> --\n3aae5d0-1:
> magic 0xdeaff2fe mark_cookie
> 0x\ngpal-3aae5d0-1: super.status
> 3super.cookie  390781895\ngpal-3aae5d0-1:
>  cg_xth  0
>

A regular expression solution:

import re
s = """3aae5d0-1: Parent Block for 1,1,19169280:8192 (block 1,1,114688:8192)
--\n3aae5d0-1:
magic 0xdeaff2fe mark_cookie
0x\ngpal-3aae5d0-1: super.status
3super.cookie  390781895\ngpal-3aae5d0-1:
 cg_xth  0"""
pattern = re.compile(r"""
\( # open paren
block
\s # whitespace
( # start capture
\d
,
\d
,
\d+
:
\d+
) # end capture
""", re.VERBOSE | re.MULTILINE)
match = re.search(pattern, s)
print(match.group(1))
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How can I debug silent failure - print no output

2016-05-27 Thread Jason Friedman
>
> def GetArgs():
> '''parse XML from command line'''
> parser = argparse.ArgumentParser()
>
> parser.add_argument("path", nargs="+")
> parser.add_argument('-e', '--extension', default='',
> help='File extension to filter by.')
> args = parser.parse_args()
>
> files = set()
> name_pattern = "*" + args.extension
> for path in args.path:
> files.update(glob.glob(os.path.join(path, name_pattern)))
> return files
>
>
>
> # Now walk the tree and insert data.
> for filename in sorted(GetArgs()):
> for meeting in pq(filename=filename):
> print(filename)
> print(meeting)
> meetdata = [meeting.get(attr) for attr in meetattrs]
> cur.execute("insert into meetings valueme in GetArgs():s (" +
> ",".join(["%s"]*len(meetattrs)) + ")", meetdata)
> for race in meeting.findall("race"):
> race.set("meeting_id", meeting.get("id"))
> racedata = [race.get(attr) for attr in raceattrs]
> cur.execute("insert into races values (" +
> ",".join(["%s"]*len(raceattrs)) + ")",
> racedata)
> for horse in race.findall("nomination"):
> horse.set("race_id", race.get("id"))
> horsedata = [horse.get(attr) for attr in horseattrs]
> cur.execute("insert into horses values (" +
> ",".join(["%s"]*len(horseattrs)) + ")",
> horsedata)
>
> If your actual indentation matches what I see in your post, is your

for filename in sorted(GetArgs())

line within the definition of GetArgs?

If yes, it will not be executed.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why online forums have bad behaviour (was: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement?)

2016-05-12 Thread Jason Friedman
> TL;DR: because we're all human, and human behaviour needs either
> immediate face-to-face feedback or social enforcement to correct
> selfishness and abrasiveness. Where face-to-face feedback is lacking,
> social enforcement needs to take more of the load.
>
>
> Many people have a false sense of entitlement to be caustic in dealing
> with others, and have no better response to a request that they tone it
> down than to escalate their bad behaviour.
>
> This behaviour is usually counteracted in face-to-face interaction, by
> being confronted with the immediate result on the other person: most
> people don't enjoy *seeing* other people become upset, so most people
> tend to work harder to be more polite in face-to-face discussion.
>
> On an internet forum, especially one with such low bandwidth as text,
> these feedback mechanisms are not sufficient (not immediate enough, and
> not informative enough) for the person to experience a link from their
> bad behaviour to the unpleasant consequences.
>
>
> This isn't a new problem. It's not new to the internet, and it certainly
> isn't new to humans.
>
> What is new, though, is that many online communities – the Python
> community specifically – have decided we are not going to tolerate
> anti-social behaviour, and we have also enacted policies to enforce that
> decision.
>
> We'll always have some anti-social actors, and bad actions by otherwise
> good actors. Many of them when confronted will respond with petulance
> and name-calling and bullying and other schoolyard reflexes. We have to
> be consistent in rejecting such behaviour from our community.

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


Re: Python,ping,csv

2016-04-11 Thread Jason Friedman
> I added a line.
> I would need to put the output into a csv file which contained the 
> results of the hosts up and down.
> Can you help me?
> 
> import subprocess
> from ipaddress import IPv4Network
> for address in IPv4Network('10.24.59.0/24').hosts():
>   a = str(address)
>   res = subprocess.call(['ping', '-c', '3', address])
> 

"""Typical output from ping:

$ ping -c 3 10.2.2.2
PING 10.2.2.2 (10.2.2.2) 56(84) bytes of data.

--- 10.2.2.2 ping statistics ---
3 packets transmitted, 0 received, 100% packet loss, time 1999ms


$ ping -c 3 localhost
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.030 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.040 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.042 ms

--- localhost ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1998ms
rtt min/avg/max/mdev = 0.030/0.037/0.042/0.007 ms
"""

import csv
import ipaddress
import re
import subprocess
import sys
NETWORK = "192.168.1.0"
MASK = "24"
with open('some.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(("Address", "% success"))
for address in ipaddress.IPv4Network('%s/%s' % (NETWORK, 
MASK)).hosts():
print("Pinging %s ..." % address, file=sys.stderr)
command = "ping -c 3 %s" % address
output = subprocess.getoutput(command)
match = re.search(r"(\d+)% packet loss", output)
if match:
percent_lost = match.group(1)
writer.writerow((str(address), 100 - int(percent_lost)))
else:
# If we reach this point the ping command output
# was not in the expected output format
writer.writerow((str(address), ""))





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


Re: Python,ping,csv

2016-04-11 Thread Jason Friedman
> I added a line.
> I would need to put the output into a csv file which contained the results
> of the hosts up and down.
> Can you help me?
>
>
> import subprocess
> from ipaddress import IPv4Network
> for address in IPv4Network('10.24.59.0/24').hosts():
> a = str(address)
> res = subprocess.call(['ping', '-c', '3', address])

"""Typical output from ping:

$ ping -c 3 10.2.2.2
PING 10.2.2.2 (10.2.2.2) 56(84) bytes of data.

--- 10.2.2.2 ping statistics ---
3 packets transmitted, 0 received, 100% packet loss, time 1999ms


$ ping -c 3 localhost
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.030 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.040 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.042 ms

--- localhost ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1998ms
rtt min/avg/max/mdev = 0.030/0.037/0.042/0.007 ms
"""

import csv
import ipaddress
import re
import subprocess
import sys
NETWORK = "192.168.1.0"
MASK = "24"
with open('some.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(("Address", "% success"))
for address in ipaddress.IPv4Network('%s/%s' % (NETWORK, MASK)).hosts():
print("Pinging %s ..." % address, file=sys.stderr)
command = "ping -c 3 %s" % address
output = subprocess.getoutput(command)
match = re.search(r"(\d+)% packet loss", output)
if match:
percent_lost = match.group(1)
writer.writerow((str(address), 100 - int(percent_lost)))
else:
# If we reach this point the ping command output
# was not in the expected output format
writer.writerow((str(address), ""))
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python,ping,csv

2016-04-09 Thread Jason Friedman
> for ping in range(1,254):
> address = "10.24.59." + str(ping)
> res = subprocess.call(['ping', '-c', '3', address])
> if res == 0:
> print ("ping to", address, "OK")
> elif res == 2:
> print ("no response from", address)
> else:
> print ("ping to", address, "failed!")

Note that with Python 3.3+ you can simplify slightly:

from ipaddress import IPv4Network
for address in IPv4Network('10.24.59.0/24').hosts():
res = subprocess.call(['ping', '-c', '3', address])
...
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: i cant seem to figure out the error

2016-04-03 Thread Jason Friedman
>   def deposit(self, amount):
> self.amount=amount
> self.balance += amount
> return self.balance
>
>
>   def withdraw(self, amount):
> self.amount=amount
> if(amount > self.balance):
>   return ("Amount greater than available balance.")
> else:
>   self.balance -= amount
> return self.balance

Also, this line is not needed in the deposit and withdraw methods:

self.amount=amount
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: i cant seem to figure out the error

2016-04-03 Thread Jason Friedman
>
>- Create a method called `withdraw` that takes in cash withdrawal amount
>and updates the balance accordingly. if amount is greater than balance
>return `"invalid transaction"`
>
>   def withdraw(self, amount):
> self.amount=amount
> if(amount > self.balance):
>   return ("Amount greater than available balance.")
> else:
>   self.balance -= amount
> return self.balance
>

The instructions say to "return 'invalid transaction'" but I expect they
really want that error printed to STDERR (typically your screen) rather
than literally returned.  Try this:

  def withdraw(self, amount):
self.amount=amount
if(amount > self.balance):
  print "Amount greater than available balance, no funds withdrawn."
else:
  self.balance -= amount
return self.balance
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Off-topic] Requests author discusses MentalHealthError exception

2016-02-27 Thread Jason Friedman
Yes, thank you for sharing.

Stories from people we know, or know of, leads to normalization:
mental illness is a routine illness like Type I diabetes or
appendicitis.

On Sat, Feb 27, 2016 at 2:37 AM, Steven D'Aprano  wrote:
> The author of Requests, Kenneth Reitz, discusses his recent recovery from a
> MentalHealthError exception.
>
> http://www.kennethreitz.org/essays/mentalhealtherror-an-exception-occurred
>
> Although the connection to Python is only quite slim, I found it fascinating
> to read.
>
>
>
> --
> Steven
>
> --
> https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python and multiple user access via super cool fancy website

2015-12-25 Thread Jason Friedman
>> I have a hunch that you do not want to write the program, nor do you
>> want to see exactly how a programmer would write it?
>>
>> The question is more like asking a heart surgeon how she performs
>> heart surgery:  you don't plan to do it yourself, but you want a
>> general idea of how it is done?  How do you keep the patient from
>> bleeding to death?  Does the heart stop while performing the surgery,
>> and if yes, how does the patient stay alive?

> What gives you that idea?

You said:  I am not a programmer but I do have an incredible interest in it.

Many people say:  I am not a programmer but I want to become one.

I just wanted to make sure your actual question was being answered.
Sounds like it is.

Also, the general practice is to put your response at the bottom of your reply.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python and multiple user access via super cool fancy website

2015-12-24 Thread Jason Friedman
> I am not sure if this is the correct venue for my question, but I'd like to
> submit my question just in case.  I am not a programmer but I do have an
> incredible interest in it, so please excuse my lack of understanding if my
> question isn't very thorough.
>
> As an example, a website backend is developed using Python.  Users can
> submit their input through the website and PHP (or some other language)
> transfers the user input from the website fields to a database such as
> MySQL.  There is a main script called main_script.py which extracts the
> user data from MySQL, processes it, stores output in MySQL and sends output
> to the user (via webpage and email).
>
> About main_script.py
> # main_script.py extracts user input from MySQL, processes it, stores
> output in MySQL and send output to user (via webpage and email).
> # Inputs: User personal information such as age, dob, nationality, hobbies,
> and 20 or 30 other fields
> # Output: main_script.py is going to do something with it such as access
> the database and some shelve files or other py scripts. I have no clue what
> it's going to do, but my point is that the processing of the input to
> output will take longer than simply a print('Hello, %r!' %user_name).
>
> My question:  I am curious to know how Python handles something like this.
> Let's say that there are 10, 20, 50, or even 1000 users accessing the
> website.  They all put in their 20 to 30 pieces of input and are waiting on
> some fancy magic output.  How exactly does that work?  Can multiple users
> access the same script?  Does the Python programmer need to code in a
> manner that supports this?  Are requests to the script handled serially or
> in parallel?

I have a hunch that you do not want to write the program, nor do you
want to see exactly how a programmer would write it?

The question is more like asking a heart surgeon how she performs
heart surgery:  you don't plan to do it yourself, but you want a
general idea of how it is done?  How do you keep the patient from
bleeding to death?  Does the heart stop while performing the surgery,
and if yes, how does the patient stay alive?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Exclude text within quotation marks and words beginning with a capital letter

2015-12-04 Thread Jason Friedman
>
> I am working on a program that is written in Python 2.7 to be compatible
> with the POS tagger that I import from Pattern. The tagger identifies all
> the nouns in a text. I need to exclude from the tagger any text that is
> within quotation marks, and also any word that begins with an upper case
> letter (including words at the beginning of sentences).
>
> Any advice on coding that would be gratefully received. Thanks.
>

Perhaps overkill, but wanted to make sure you knew about the Natural
Language Toolkit:  http://www.nltk.org/.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: reading from a txt file

2015-11-26 Thread Jason Friedman
>
> Hey, I'm wondering how to read individual strings in a text file.  I can
> read a text file by lines with .readlines() ,
> but I need to read specifically by strings, not including spaces.  Thanks
> in advance
>

How about:

for a_string in open("/path/to/file").read().split():
print(a_string)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: teacher need help!

2015-10-18 Thread Jason Friedman
> Can I suggest you find the turtle.py module in c:\windows\system32, move it
> to somewhere more suitable and try the code again?

Or, copy it, rather than move it?
It may be that for some students turtle.py is in a terrific place already.
-- 
https://mail.python.org/mailman/listinfo/python-list


Address field [was: Integers with leading zeroes]

2015-07-21 Thread Jason Friedman
 Of course, most of the
 time, I advocate a single multi-line text field Address, and let
 people key them in free-form. No postcode field whatsoever.

I'm curious about that statement.
I could see accepting input as you describe above, but I'm thinking
you'd want to *store* a postcode field.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: bottle app crashes

2015-07-06 Thread Jason Friedman
 Last summer I fumbled together a small appplication that calculates both LASK
 and Elo ratings for chess. I managed to webify it using Bottle. This works
 nicely on my laptop for testing.

 Once I log off (or my user session times out) my server where I've started the
 application with python3 LASKweb.py  the application dies within a  minute,
 resulting in clients getting 404 errors when accessing the page (a simple
 table that's processed by the application).

Similar to other answers here, I do not know why your application
stops serving, but I have had success using waitress
(https://pypi.python.org/pypi/waitress) with bottle
(http://bottlepy.org/docs/dev/deployment.html#switching-the-server-backend).
-- 
https://mail.python.org/mailman/listinfo/python-list


decorators and alarms

2015-05-22 Thread Jason Friedman
I have the following code to run a shell command and kill it if it
does not return within a certain amount of time:

def run_with_time_limit(command, limit):
Run the given command via a shell.
Kill the command, and raise an exception, if the
execution time exceeds limit seconds.
Else return (exit_code, stdout, stderr, duration).
class Alarm(Exception):
pass
def alarm_handler(signum, frame):
raise Alarm
start_stamp = time.time()
signal.signal(signal.SIGALRM, alarm_handler)
signal.alarm(limit)
try:
process = subprocess.Popen(
command,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
shell=True)
stdout, stderr = process.communicate()
duration = time.time() - start_stamp
signal.alarm(0) # reset the alarm
except Alarm:
process.kill()
raise Alarm(The command did not return within %(limit)s
seconds. % locals())
return (process.returncode, stdout, stderr, duration)

run_with_time_limit(sleep 2, 1)

This works as desired.

But, I'd like to expand this to take some generic code, not just a
shell command, and terminate it if it does not return quickly.

@time_limiter
def generic_function(arg1, arg2, time_limit=10):
do_some_stuff()
do_some_other_stuff()
return val1, val2

If generic_function does not finish within 10 seconds it would stop
executing and throw an exception.

May I have some hints, please?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: decorators and alarms

2015-05-22 Thread Jason Friedman
 But, I'd like to expand this to take some generic code, not just a
 shell command, and terminate it if it does not return quickly.

 @time_limiter
 def generic_function(arg1, arg2, time_limit=10):
 do_some_stuff()
 do_some_other_stuff()
 return val1, val2

 If generic_function does not finish within 10 seconds it would stop
 executing and throw an exception.

Found an answer:
https://wiki.python.org/moin/PythonDecoratorLibrary#Function_Timeout
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to deploy a custom common module?

2015-05-16 Thread Jason Friedman
 When I deploy test.py on another computer, I put (rsync) both test.py and 
 cmn_funcs.py in the same remote directory.

 If I create another python project (test2.py) in new directory, that needs 
 common functions, what should I do with cmn_funcs.py?

I put my shared code in a separate folder, named something like
/path/to/module_dir.

I then add to /etc/profile.d/something.sh:

export PYTHONPATH=$PYTHONPATH:/path/to/module_dir
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Setuptools: no module named 'html.entities'

2015-03-16 Thread Jason Friedman
On Mon, Mar 16, 2015 at 7:10 AM, Wolfgang Maier 
wolfgang.ma...@biologie.uni-freiburg.de wrote:

 On 03/16/2015 12:53 AM, Jason Friedman wrote:

 Hello,

 This is Python 3.3.2 on Linux.
 I downloaded Setuptools
 (https://pypi.python.org/packages/source/s/setuptools/
 setuptools-14.3.tar.gz),
 exploded the tarball, and I get:

 python setup.py build
 Traceback (most recent call last):
File frozen importlib._bootstrap, line 1521, in
 _find_and_load_unlocked
 AttributeError: 'module' object has no attribute '__path__'

 During handling of the above exception, another exception occurred:
 Traceback (most recent call last):
File setup.py, line 21, in module
  exec(init_file.read(), command_ns)
File string, line 11, in module
File /home/spjsf/setuptools-14.3/setuptools/__init__.py, line 11,
 in module
  from setuptools.extension import Extension
File /home/spjsf/setuptools-14.3/setuptools/extension.py, line 8,
 in module
  from .dist import _get_unpatched
File /home/spjsf/setuptools-14.3/setuptools/dist.py, line 16, in
 module
  from setuptools.depends import Require
File /home/spjsf/setuptools-14.3/setuptools/depends.py, line 6, in
 module
  from setuptools import compat
File /home/spjsf/setuptools-14.3/setuptools/compat.py, line 44, in
 module
  from html.entities import name2codepoint
 ImportError: No module named 'html.entities'; html is not a package



 Not sure, but maybe you have a html.py somewhere in your module search
 path taking precedence over the stdlib html package ?
 Putting a dummy html.py file into the extracted setuptools folder, at
 least, lets me reproduce your exact error.

 What does

 python -c import html; print(html)

 tell you ?

 Bingo!  Much obliged.
-- 
https://mail.python.org/mailman/listinfo/python-list


Setuptools: no module named 'html.entities'

2015-03-15 Thread Jason Friedman
Hello,

This is Python 3.3.2 on Linux.
I downloaded Setuptools (
https://pypi.python.org/packages/source/s/setuptools/setuptools-14.3.tar.gz),
exploded the tarball, and I get:

python setup.py build
Traceback (most recent call last):
  File frozen importlib._bootstrap, line 1521, in
_find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File setup.py, line 21, in module
exec(init_file.read(), command_ns)
  File string, line 11, in module
  File /home/spjsf/setuptools-14.3/setuptools/__init__.py, line 11, in
module
from setuptools.extension import Extension
  File /home/spjsf/setuptools-14.3/setuptools/extension.py, line 8, in
module
from .dist import _get_unpatched
  File /home/spjsf/setuptools-14.3/setuptools/dist.py, line 16, in
module
from setuptools.depends import Require
  File /home/spjsf/setuptools-14.3/setuptools/depends.py, line 6, in
module
from setuptools import compat
  File /home/spjsf/setuptools-14.3/setuptools/compat.py, line 44, in
module
from html.entities import name2codepoint
ImportError: No module named 'html.entities'; html is not a package
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Append a file

2015-03-06 Thread Jason Friedman
On Fri, Mar 6, 2015 at 2:55 PM, Jason Venneri jv92...@gmail.com wrote:
 Hello, I'm using the urllib.urlretrieve command to retrieve a couple of lines 
 of data.  I want to combine the two results into one file not two.

 Any suggestions?

 Sample
 urllib.urlretrieve('http://www.airplanes.com/data/boeing1.html','B747A.txt')
 urllib.urlretrieve('http://www.airplanes.com/data/boeing2.html','B747B.txt')

 I would like one file say B747C that contains the data from B747A and B747B 
 in a file named B747C

 f = open(B747C.txt, w)
 look_for = Aircraft,
 for line in open(B747A.txt):
... if look_for in line:
... f.write(line)
...
 for line in open(B747B.txt):
... if look_for in line:
... f.write(line)
...
 f.close()

Seems like you are using Python 2, you ought to consider Python 3.

The requests module
(http://docs.python-requests.org/en/latest/user/install/) makes this
kind of work easier.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Sort list of dictionaries

2015-03-03 Thread Jason Friedman
On Tue, Mar 3, 2015 at 12:07 AM, Chris Angelico ros...@gmail.com wrote:

 Heh, I think that mght be a bit abusive :) I'm not sure that
 you want to depend on the version numbers fitting inside the rules for
 IP addresses, especially given that the example has a component of
 2214.


Indeed, I was mistaken

 ipaddress.ip_address(1000.1.2.3)
Traceback (most recent call last):
  File stdin, line 1, in module
  File /opt/python/lib/python3.4/ipaddress.py, line 54, in ip_address
address)
ValueError: '1000.1.2.3' does not appear to be an IPv4 or IPv6 address

I was overcome by how neat I think the ipaddress module is.
I have not used it yet, but I expect someday I will, and I appreciate
how Python makes my job easier by doing much of my work for me.  A
colleague yesterday asked how I guaranteed my temporary file names
would not collide with another file, and my answer was I don't have to
worry about it, someone else figured it out with the tempfile module.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Sort list of dictionaries

2015-03-02 Thread Jason Friedman
 This is what I was trying but LooseVersion() was not sorting version numbers 
 like I thought it would. You will notice that Chrome version 40.0.2214.111 
 is higher than 40.0.2214.91 but in the end result it's not sorting it that 
 way.

 Because it's a string they're sorted lexicographically, and in that
 ordering 40.0.2214.111 is less than 40.0.2214.91. Instead of a
 string you should probably use some sort of version info tuple. A
 simple tuple of ints may suffice, although you may need to get a
 little cleverer if there are ever any version strings that aren't
 entirely dotted numeric.

Also, Python 3.4 comes with an ipaddress module.

 import ipaddress
 address_string_1 = 2.2.3.4
 address_string_2 = 10.2.3.4
 address_string_2  address_string_1
False
 ip_address_1 = ipaddress.ip_address(address_string_1)
 ip_address_2 = ipaddress.ip_address(address_string_2)
 ip_address_2  ip_address_1
True
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: requesting you all to please guide me , which tutorials is best to learn redis database

2015-02-27 Thread Jason Friedman
 i want to learn redis database and its use via python , please  guide me 
 which tutorials i should be study, so that i can learn it in good way


How about https://pypi.python.org/pypi/redis/?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What behavior would you expect?

2015-02-22 Thread Jason Friedman
 If you're going to call listdir, you probably want to use fnmatch directly.

 fnmatch seems to be silent on non-existent directories:
python -c 'import fnmatch; fnmatch.fnmatch(/no/such/path, *)'
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What behavior would you expect?

2015-02-19 Thread Jason Friedman
 def order_matching_files(a_path, a_glob=*):
 Search a path for files whose names match a_glob
 and return a list of the full path to such files, in descending
 order of modification time. Ignore directories.
 previous_dir = os.getcwd()
 os.chdir(a_path)
 return_list = [os.path.join(a_path, x) for x in glob.glob(a_glob) if
 os.path.isfile(x)]
 os.chdir(previous_dir)
 return reversed(sorted(return_list, key=os.path.getmtime))


Oops, guess I'm not really returning a list, I'm returning an iterator.  I
should change either the comment or the return value.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What behavior would you expect?

2015-02-19 Thread Jason Friedman
I have need to search a directory and return the name of the most recent
file matching a given pattern.  Given a directory with these files and
timestamps,


 q.pattern1.abc Feb 13
 r.pattern1.cdf  Feb 12
 s.pattern1.efg  Feb 10
 t.pattern2.abc Feb 13
 u.pattern2.xyz  Feb 14
 v.pattern2.efg  Feb 10

 calling my_function(/path/to/dir, pattern1) will return q.pattern1.abc
 and calling my_function(/path/to/dir, pattern2) will return
 u.pattern2.xyz.

 My question is, what would be a reasonable behavior/result/return value if:
 1. /path/to/dir does not exist or is not readable
 2. no files match the given pattern

 Also, what would be a reasonable name for such a function?


Thank you everyone.  Taking bits of advice from each of you I tentatively
have:

import glob
import os

def order_matching_files(a_path, a_glob=*):
Search a path for files whose names match a_glob
and return a list of the full path to such files, in descending
order of modification time. Ignore directories.
previous_dir = os.getcwd()
os.chdir(a_path)
return_list = [os.path.join(a_path, x) for x in glob.glob(a_glob) if
os.path.isfile(x)]
os.chdir(previous_dir)
return reversed(sorted(return_list, key=os.path.getmtime))

It's a shame that glob.glob does not take an arbitrary directory as an
optional argument if one does not want to scan the current directory.
-- 
https://mail.python.org/mailman/listinfo/python-list


  1   2   3   >