Optical Character Recognition (OCR)

2019-04-24 Thread meherfrioui2017
Hello ,

I want to extract text Arabic from image please can anyone tell me is there way 
to figure out this kind of problem

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


Read the table data from PDF files in Python

2019-04-24 Thread mrawat213
Hello,

Anyone knows how to fetch the data from PDF file having tables with other text 
in Python. Need to fetch some cell values based on condition from that table.



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


Re: Optical Character Recognition (OCR)

2019-04-24 Thread Chris Angelico
On Wed, Apr 24, 2019 at 5:44 PM  wrote:
>
> Hello ,
>
> I want to extract text Arabic from image please can anyone tell me is there 
> way to figure out this kind of problem
>

Arabic is somewhat harder than many languages, but I'm sure it's
possible. Have you searched the internet for OCR libraries?

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


Re: Read the table data from PDF files in Python

2019-04-24 Thread Rhodri James

On 24/04/2019 10:36, mrawat...@gmail.com wrote:

Anyone knows how to fetch the data from PDF file having tables with other text 
in Python. Need to fetch some cell values based on condition from that table.


Hi there!

If you have any alternatives to doing this, use them.  Extracting data 
from PDFs like this is hugely unreliable because the order in which page 
elements show up in a PDF varies enormously.  What works for one PDF may 
give you complete nonsense for the next.


If you must do it this way, there are modules called PyPDF and PyPDF2 in 
PyPI which will allow you to extract the text from the PDF.  You are on 
your own for working out how to parse the tables out of that text, 
though; the structures in the data you are hoping for simply don't exist.


--
Rhodri James *-* Kynesim Ltd
--
https://mail.python.org/mailman/listinfo/python-list


Re: Read the table data from PDF files in Python

2019-04-24 Thread Peter Pearson
On Wed, 24 Apr 2019 02:36:27 -0700 (PDT), mrawat...@gmail.com wrote:
> Hello,
> Anyone knows how to fetch the data from PDF file having tables with
> other text in Python. Need to fetch some cell values based on
> condition from that table.

You might find pdftotext useful.

The command . . .

  pdftotext -layout somefile.pdf

produces a file named somefile.txt.

This will be completely useless if the original PDF is just
a PDF wrapper around an image.  That's what document scanners
tend to produce.

-- 
To email me, substitute nowhere->runbox, invalid->com.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Importing module from another subdirectory

2019-04-24 Thread Rich Shepard

On Wed, 24 Apr 2019, dieter wrote:


"sys.path" tweaks are typically employed with a "central" Python
installation, to have it look at non-standard places for module/packages
under specific circumstances.


Almost all python packages installed here are built using the
SlackBuilds.org scripts (I run Slackware, currently -14.2) and I'd rather
keep current using the SBo scripts rather than importing via PyPi.

Most web search results I've found for appending to sys.path (or using the
site package) refer to python2, not the installed version 3.7.x.

The current project's directory structue is:

bustrac/
README.rst
bustrac.py*
controller/
model/
scripts/
views/

If I correctly understand the process, in bustrac.py I'll add
import sys.path
sys.path.append(controller, model, scripts, views)

Which would place the project-specific directories in the search path.

While developing the application and testing each view module I would place
the same import and sys.path.append() commands in the module being developed
and tested. Is this correct?

Thanks again,

Rich



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


Re: Importing module from another subdirectory

2019-04-24 Thread Rich Shepard

On Wed, 24 Apr 2019, dieter wrote:


With a "virtualenv", there is usually no need to tweak "sys.path" --
you simply install everything your project needs into the "virtualenv".


Dieter,

Okay. I just upgraded pip to 19.1 for python3 and virtualenv to version
16.5.0. Now I'll learn how to use it for each of my two projects.

Thanks,

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


Running virtualenv to set the ENV

2019-04-24 Thread Rich Shepard

I've installed virtualenv in Slackware-14.2. Now I want to set up the ENV
 in an application's
development directory.

Which is the CWD for running virtualenv and spcifying the path to the
project's directoy?

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


Re: Importing module from another subdirectory

2019-04-24 Thread Rich Shepard

On Wed, 24 Apr 2019, Rich Shepard wrote:


If I correctly understand the process, in bustrac.py I'll add import
sys.path sys.path.append(controller, model, scripts, views)


Never mind. I've installed virtualenv and will work within it.

Regards,

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


Re: How to catch a usefull error message ?

2019-04-24 Thread Vincent Vande Vyvre

Le 23/04/19 à 20:54, Chris Angelico a écrit :

On Wed, Apr 24, 2019 at 4:47 AM Vincent Vande Vyvre
 wrote:

Into the lib:

static int
ImgProc_init(ImgProc *self, PyObject *args, PyObject *kwds)
{
  PyObject *tmp;
  char *fname;

  if (!PyArg_ParseTuple(args, "s", &fname))
  return NULL;

  tmp = self->src;
  self->src = PyUnicode_FromString(fname);
  Py_XDECREF(tmp);
  return 0;
}

If i do:
  try:
  tif = ImgProc(123)
  except Exception as why:
  print(sys.exc_info())
  raise
I get:
(, SystemError("
returned a result with an error set",), )
TypeError: argument 1 must be str, not int

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/home/vincent/oqapy-3/trunk/filters/ui_lenscorrection.py", line
104, in on_main_cursor_changed
  self.prepare_preview_process()
File "/home/vincent/oqapy-3/trunk/filters/ui_lenscorrection.py", line
137, in prepare_preview_process
  self.main.process_on_preview(params)
File "/home/vincent/oqapy-3/trunk/filters/lenscorrection.py", line
56, in process_on_preview
  tif = ImgProc(123)
SystemError:  returned a result with an error set

Why a SystemError ?
The SystemError means that you're using the Python C API in a way that
doesn't make sense to the interpreter. You're leaving a marker saying
"hey, I need you to throw an exception" but then you're also returning
a value. You'll need to figure out where that's happening and exactly
what is being called. How are you setting up your class?

ChrisA


The syntaxe

if (!PyArg_ParseTuple(args, "s", &fname))
 return NULL;

Is the usage described in the doc [*]

And without block try-except I get the good one error.


[*] 
https://docs.python.org/3.5//extending/extending.html#back-to-the-example



Vincent

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


Re: Running virtualenv to set the ENV [RESOLVED]

2019-04-24 Thread Rich Shepard

On Wed, 24 Apr 2019, Rich Shepard wrote:


Which is the CWD for running virtualenv and spcifying the path to the
project's directoy?


The project's subdirectory. It's up and running now.

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


Re: How to catch a usefull error message ?

2019-04-24 Thread Vincent Vande Vyvre

Le 23/04/19 à 21:48, MRAB a écrit :

On 2019-04-23 19:21, Vincent Vande Vyvre wrote:

Le 23/04/19 à 19:23, Chris Angelico a écrit :

On Wed, Apr 24, 2019 at 3:18 AM Vincent Vande Vyvre
 wrote:

Hi,

In a CPython lib I have an _init() method wich take one argument, a 
file

name.

  char *fname;

  if (!PyArg_ParseTuple(args, "s", &fname))
  return NULL;

So, if I instanciate my object with a bad argument I've a good error
message:

tif = ImgProc(123)
TypeError: argument 1 must be str, not int
(followed by the traceback)

But if I do:
try:
  tif = ImgProc(123)
except Exception as why:
  print("Error:", why)

I get just:

Error:  returned a result with an error set


It looks like there's an internal problem in the C function. Are you
sure it's hitting the PyArg_ParseTuple and then immediately returning
NULL? Post a bit more of your code; this error looks like something is
leaving an error state but then carrying on with the code.

ChrisA


Into the lib:

static int
ImgProc_init(ImgProc *self, PyObject *args, PyObject *kwds)
{
      PyObject *tmp;
      char *fname;

      if (!PyArg_ParseTuple(args, "s", &fname))
      return NULL;

      tmp = self->src;
      self->src = PyUnicode_FromString(fname);
      Py_XDECREF(tmp);
      return 0;
}


[snip]

That function returns an int.

If PyArg_ParseTuple fails, your function returns NULL, which is cast 
to an int, 0.


If PyArg_ParseTuple succeeds, your function returns 0.

Either way, it returns 0.

So how does the caller know whether the function was successful? Does 
it check PyErr_Occurred?


No, the caller is in a block try-except for that.

The exact question here is why without a try-except I've the good one 
error and not in a try-except.


The /return 0;/ is usual in a /Foo_init()/ function.


Vincent

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


Re: How to catch a usefull error message ?

2019-04-24 Thread MRAB

On 2019-04-23 20:21, Vincent Vande Vyvre wrote:

Le 23/04/19 à 20:54, Chris Angelico a écrit :

On Wed, Apr 24, 2019 at 4:47 AM Vincent Vande Vyvre
 wrote:

Into the lib:

static int
ImgProc_init(ImgProc *self, PyObject *args, PyObject *kwds)
{
  PyObject *tmp;
  char *fname;

  if (!PyArg_ParseTuple(args, "s", &fname))
  return NULL;

  tmp = self->src;
  self->src = PyUnicode_FromString(fname);
  Py_XDECREF(tmp);
  return 0;
}

If i do:
  try:
  tif = ImgProc(123)
  except Exception as why:
  print(sys.exc_info())
  raise
I get:
(, SystemError("
returned a result with an error set",), )
TypeError: argument 1 must be str, not int

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/home/vincent/oqapy-3/trunk/filters/ui_lenscorrection.py", line
104, in on_main_cursor_changed
  self.prepare_preview_process()
File "/home/vincent/oqapy-3/trunk/filters/ui_lenscorrection.py", line
137, in prepare_preview_process
  self.main.process_on_preview(params)
File "/home/vincent/oqapy-3/trunk/filters/lenscorrection.py", line
56, in process_on_preview
  tif = ImgProc(123)
SystemError:  returned a result with an error set

Why a SystemError ?
The SystemError means that you're using the Python C API in a way that
doesn't make sense to the interpreter. You're leaving a marker saying
"hey, I need you to throw an exception" but then you're also returning
a value. You'll need to figure out where that's happening and exactly
what is being called. How are you setting up your class?

ChrisA


The syntaxe

  if (!PyArg_ParseTuple(args, "s", &fname))
   return NULL;

Is the usage described in the doc [*]

And without block try-except I get the good one error.


[*]
https://docs.python.org/3.5//extending/extending.html#back-to-the-example

If you look at the previous example, the function's return type is 
"PyObject *".


On success it returns a reference (pointer) to an object; on error it 
returns NULL.


Your function's return type is int.
--
https://mail.python.org/mailman/listinfo/python-list


Re: How to catch a usefull error message ?

2019-04-24 Thread MRAB

On 2019-04-23 21:03, Vincent Vande Vyvre wrote:

Le 23/04/19 à 21:48, MRAB a écrit :

On 2019-04-23 19:21, Vincent Vande Vyvre wrote:

Le 23/04/19 à 19:23, Chris Angelico a écrit :

On Wed, Apr 24, 2019 at 3:18 AM Vincent Vande Vyvre
 wrote:

Hi,

In a CPython lib I have an _init() method wich take one argument, a 
file

name.

  char *fname;

  if (!PyArg_ParseTuple(args, "s", &fname))
  return NULL;

So, if I instanciate my object with a bad argument I've a good error
message:

tif = ImgProc(123)
TypeError: argument 1 must be str, not int
(followed by the traceback)

But if I do:
try:
  tif = ImgProc(123)
except Exception as why:
  print("Error:", why)

I get just:

Error:  returned a result with an error set


It looks like there's an internal problem in the C function. Are you
sure it's hitting the PyArg_ParseTuple and then immediately returning
NULL? Post a bit more of your code; this error looks like something is
leaving an error state but then carrying on with the code.

ChrisA


Into the lib:

static int
ImgProc_init(ImgProc *self, PyObject *args, PyObject *kwds)
{
      PyObject *tmp;
      char *fname;

      if (!PyArg_ParseTuple(args, "s", &fname))
      return NULL;

      tmp = self->src;
      self->src = PyUnicode_FromString(fname);
      Py_XDECREF(tmp);
      return 0;
}


[snip]

That function returns an int.

If PyArg_ParseTuple fails, your function returns NULL, which is cast 
to an int, 0.


If PyArg_ParseTuple succeeds, your function returns 0.

Either way, it returns 0.

So how does the caller know whether the function was successful? Does 
it check PyErr_Occurred?


No, the caller is in a block try-except for that.

The exact question here is why without a try-except I've the good one
error and not in a try-except.

The /return 0;/ is usual in a /Foo_init()/ function.

In an extension, if your C function has been called by Python then its 
return type should (usually) be "PyObject*". It should return a 
reference to an object on success or NULL on error.


Look more closely at the last example of section 1.2 at 
https://docs.python.org/3.5//extending/extending.html#back-to-the-example.

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


Re: Read the table data from PDF files in Python

2019-04-24 Thread Mark Kettner
I've heard about camelot a while ago:

https://camelot-py.readthedocs.io/

but I never really used it and cannot provide any support or comparison
to other data-extraction tools or the like.

--
Mit freundlichen Gruessen / Best Regards

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


Re: Importing module from another subdirectory

2019-04-24 Thread Rich Shepard

On Wed, 24 Apr 2019, Rich Shepard wrote:


The current project's directory structure is:


I changed package names so there are no duplicate names for packages and
modules.


bustrac/
   README.rst
   bustrac.py*
   controller/
   classes/

 model.py

   scripts/
   gui/

 test_act_de.py

test_act_de.py tries to import model.py from the classes package:
from classes import model as m

Running in bustrac/ produces this error:

$ python3 gui/test_act_de.py 
Traceback (most recent call last):

  File "gui/test_act_de.py", line 1, in 
from classes import model as m
ModuleNotFoundError: No module named 'classes'

Despite re-reading the web page, definitive-guide-python-imports.html, I'm
still not seeing how to import the model.py module from modules in other
subdirectories of bustrac/.

A clue stick is needed.

Regards,

Rich

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


Re: Running virtualenv to set the ENV

2019-04-24 Thread Chris Angelico
On Thu, Apr 25, 2019 at 8:55 AM Dennis Lee Bieber  wrote:
>
> On Wed, 24 Apr 2019 09:17:28 -0700 (PDT), Rich Shepard
>  declaimed the following:
>
> >I've installed virtualenv in Slackware-14.2. Now I want to set up the ENV
> > in an application's
> >development directory.
> >
>
> That... sounds backwards...
>
> So far as I understand it (I've only used a virtual when following a
> book for Flask) you create the virtual environment first, and then set up
> the application development INSIDE that environment.
>

Can be either way. What I do is "python3 -m venv env" in the app
directory, which creates a subdirectory called "env". (I also have
some bash integration that means that any time it sees a directory
called "env", it auto-activates that venv.) So the venv is inside the
app (and, of course, mentioned in .gitignore).

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


Re: Running virtualenv to set the ENV

2019-04-24 Thread Rich Shepard

On Wed, 24 Apr 2019, Dennis Lee Bieber wrote:


That... sounds backwards...

So far as I understand it (I've only used a virtual when following a book
for Flask) you create the virtual environment first, and then set up the
application development INSIDE that environment.


The project directory tree is already establilshed. What I did was install
virtualenv and activate it when I work on the project code.

Regards,

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


Re: Running virtualenv to set the ENV

2019-04-24 Thread Rich Shepard

On Thu, 25 Apr 2019, Chris Angelico wrote:


Can be either way. What I do is "python3 -m venv env" in the app
directory, which creates a subdirectory called "env". (I also have some
bash integration that means that any time it sees a directory called
"env", it auto-activates that venv.) So the venv is inside the app (and,
of course, mentioned in .gitignore).


ChrisA,

Thanks for sharing your approach. Rather than use the built-in venv I
installed virtualenv in the project's root directory (and added bin/,
include/, and lib/ in .gitignore).

While it's working (I activate it when ready to work on the code and
deactivate it when done), I'm still struggling to figure out the proper
syntax to import a module that's in another package.

Regards,

Rich

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


Re: Running virtualenv to set the ENV

2019-04-24 Thread Cameron Simpson

On 24Apr2019 16:50, Rich Shepard  wrote:

Can be either way. What I do is "python3 -m venv env" in the app
directory, which creates a subdirectory called "env". (I also have some
bash integration that means that any time it sees a directory called
"env", it auto-activates that venv.) So the venv is inside the app (and,
of course, mentioned in .gitignore).


ChrisA,

Thanks for sharing your approach. Rather than use the built-in venv I
installed virtualenv in the project's root directory (and added bin/,
include/, and lib/ in .gitignore).

While it's working (I activate it when ready to work on the code and
deactivate it when done), I'm still struggling to figure out the proper
syntax to import a module that's in another package.


Personally, I like to treat the virtualenv as a source for third party 
modules needed by the project. I explicitly do not like to pollute it 
with project code - that way revision control can ignore the whole venv 
tree and it can be blown away/rebuilt freely.


So my project setup looks like this:

 project-foo-for-client/
   .env.sh
   .env-local.sh
   venv/   virtualenv in here
   venv-requirements.txt
   lib/python  project Python code
   foo -> lib/python/client/fooconvenience symlink
   bin/project scripts

So, what are these pieces for?

.env.sh

 This is a short shell script to set up the environment to run things 
 from the project. I've got a command "env-dev" to source this; it is a 
 script with a little intelligence to locate the .env.sh file, etc.  
 Having found it, it sets $ENV_DEV_DIR to the directory containing the 
 .env.sh file (i.e. the poject root) and sources 
 "$ENV_DEV_DIR/.env.sh", then runs a command. Witout a command it 
 recites some important envvars like $PATH and $PYTHONPATH. I've got an 
 alias "dev=env-dev". It contains something like this:


   PATH=$ENV_DEV_DIR/bin:$ENV_DEV_DIR/venv/bin
   PYTHONPATH=$ENV_DEV_DIR/lib/python
   export PATH PYTHONPATH
   . $ENV_DEV_DIR/.env-local.sh

 so the command:

   dev python -m client.foo ...

 uses the venv python (and thus the venv library) and also finds the 
 client libraries because of $PYTHONPATH above.


 Note: I do not source venv/bin/activate; it's outstandingly complex 
 and seems basicly for hacking the interactive shell to show the use of 
 the venv; it caused me great trouble when sourced from .env.sh.
 Anyway, it is envough just to invoke the python from inside the venv 
 (done by modifying $PATH above) - all the venv/bin executables know to 
 use the venv.


.env-local.sh

 This is local or private settings for the dev environment. Things like 
 S3 credential environment variables, or DEBUG=1 to trigger some debug 
 modes in the code, etc.


.env.sh gets revision controlled; .env-local.sh is ignored.

venv/
 The virtualenv, usually containing only pip installable third party 
 modules. Ignored by revision control.


venv-requirements.txt
 Periodically I run "pip freeze >venv-requirements.txt"; this file is 
 revision controlled. That way I can rebuild an equivalent venv 
 somewhere else later.


lib/python
 The local python code, which I'm working on.

bin/
 Whatever local convenience scripts exist.

foo
 Convenience symlink to the client "foo" code; I can edit or run 
 "foo/whatever", etc.


Cheers,
Cameron Simpson 
--
https://mail.python.org/mailman/listinfo/python-list


Re: Running virtualenv to set the ENV

2019-04-24 Thread Chris Angelico
On Thu, Apr 25, 2019 at 11:40 AM Cameron Simpson  wrote:
>
> On 24Apr2019 16:50, Rich Shepard  wrote:
> >>Can be either way. What I do is "python3 -m venv env" in the app
> >>directory, which creates a subdirectory called "env". (I also have some
> >>bash integration that means that any time it sees a directory called
> >>"env", it auto-activates that venv.) So the venv is inside the app (and,
> >>of course, mentioned in .gitignore).
> >
> >ChrisA,
> >
> >Thanks for sharing your approach. Rather than use the built-in venv I
> >installed virtualenv in the project's root directory (and added bin/,
> >include/, and lib/ in .gitignore).
> >
> >While it's working (I activate it when ready to work on the code and
> >deactivate it when done), I'm still struggling to figure out the proper
> >syntax to import a module that's in another package.
>
> Personally, I like to treat the virtualenv as a source for third party
> modules needed by the project. I explicitly do not like to pollute it
> with project code - that way revision control can ignore the whole venv
> tree and it can be blown away/rebuilt freely.

I agree. This is especially important when you work with
cross-platform code; the venv will be the only place where any
platform-specific binaries exist, so you just leave that one directory
out of source control, and recreate it with two commands (for me,
"python3 -m venv env" followed by "pip install -r requirements.txt").

>   Note: I do not source venv/bin/activate; it's outstandingly complex
>   and seems basicly for hacking the interactive shell to show the use of
>   the venv; it caused me great trouble when sourced from .env.sh.
>   Anyway, it is envough just to invoke the python from inside the venv
>   (done by modifying $PATH above) - all the venv/bin executables know to
>   use the venv.

Yeah, that's a known feature. Even if you DO use the activate script,
this feature is of value; let's say you want to run something as a
cron job or systemd script - all you need to do is use `which python3`
or sys.executable and you can be sure it'll use any active virtual
environment. Really handy when creating installer scripts.

> venv-requirements.txt
>   Periodically I run "pip freeze >venv-requirements.txt"; this file is
>   revision controlled. That way I can rebuild an equivalent venv
>   somewhere else later.

Any particular reason for this name? If not, I would generally
recommend calling it "requirements.txt", as this is a minor
convention. For instance, Heroku will recognize the presence of this
file as an indication that this is a Python app, and will
automatically "pip install -r requirements.txt" as part of deployment.

Otherwise, I broadly agree with your directory structure (although I
won't bother with a lib/python directory most of the time).

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


Re: How to catch a usefull error message ?

2019-04-24 Thread Vincent Vande Vyvre

Le 24/04/19 à 19:57, MRAB a écrit :

On 2019-04-23 20:21, Vincent Vande Vyvre wrote:

Le 23/04/19 à 20:54, Chris Angelico a écrit :

On Wed, Apr 24, 2019 at 4:47 AM Vincent Vande Vyvre
 wrote:

Into the lib:

static int
ImgProc_init(ImgProc *self, PyObject *args, PyObject *kwds)
{
  PyObject *tmp;
  char *fname;

  if (!PyArg_ParseTuple(args, "s", &fname))
  return NULL;

  tmp = self->src;
  self->src = PyUnicode_FromString(fname);
  Py_XDECREF(tmp);
  return 0;
}

If i do:
  try:
  tif = ImgProc(123)
  except Exception as why:
  print(sys.exc_info())
  raise
I get:
(, SystemError("
returned a result with an error set",), )
TypeError: argument 1 must be str, not int

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
    File "/home/vincent/oqapy-3/trunk/filters/ui_lenscorrection.py", 
line

104, in on_main_cursor_changed
  self.prepare_preview_process()
    File "/home/vincent/oqapy-3/trunk/filters/ui_lenscorrection.py", 
line

137, in prepare_preview_process
  self.main.process_on_preview(params)
    File "/home/vincent/oqapy-3/trunk/filters/lenscorrection.py", line
56, in process_on_preview
  tif = ImgProc(123)
SystemError:  returned a result with an 
error set
 


Why a SystemError ?
The SystemError means that you're using the Python C API in a way that
doesn't make sense to the interpreter. You're leaving a marker saying
"hey, I need you to throw an exception" but then you're also returning
a value. You'll need to figure out where that's happening and exactly
what is being called. How are you setting up your class?

ChrisA


The syntaxe

  if (!PyArg_ParseTuple(args, "s", &fname))
   return NULL;

Is the usage described in the doc [*]

And without block try-except I get the good one error.


[*]
https://docs.python.org/3.5//extending/extending.html#back-to-the-example 



If you look at the previous example, the function's return type is 
"PyObject *".


On success it returns a reference (pointer) to an object; on error it 
returns NULL.


Your function's return type is int.


In this case yes, beause it need to return the result of the command system.

But the "return 0" is a common case for an "Foo_init()"

see: 
https://docs.python.org/3.5//extending/newtypes.html#adding-data-and-methods-to-the-basic-example


... And that's nothing to do with my initial question

Vincent

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


Re: Running virtualenv to set the ENV

2019-04-24 Thread Cameron Simpson

On 25Apr2019 12:05, Chris Angelico  wrote:

venv-requirements.txt
  Periodically I run "pip freeze >venv-requirements.txt"; this file is
  revision controlled. That way I can rebuild an equivalent venv
  somewhere else later.


Any particular reason for this name? If not, I would generally
recommend calling it "requirements.txt", as this is a minor
convention. For instance, Heroku will recognize the presence of this
file as an indication that this is a Python app, and will
automatically "pip install -r requirements.txt" as part of deployment.

Otherwise, I broadly agree with your directory structure (although I
won't bother with a lib/python directory most of the time).


Both of these stem from not working only in Python. For example today 
I've working on a project with a Python/PostgreSQL backend+cli, and a 
javascript frontend.


The "venv-requirements.txt" makes it clear that it is for the venv 
directory, and it also sorts nicely together in the directory listing.


The lib/python is an old habit, as my home directory has a lib/python 
sitting beside my ossifying lib/perl. So I find the extra specificity 
useful. And once I've made one or two convenience top level symlinks the 
depth doesn't matter for interactive purposes.


Cheers,
Cameron Simpson 
--
https://mail.python.org/mailman/listinfo/python-list


Re: Importing module from another subdirectory

2019-04-24 Thread dieter
Rich Shepard  writes:
>> bustrac/
>>README.rst
>>bustrac.py*
>>controller/
>>classes/
>  model.py
>>scripts/
>>gui/
>  test_act_de.py
>
> test_act_de.py tries to import model.py from the classes package:
> from classes import model as m
>
> Running in bustrac/ produces this error:
>
> $ python3 gui/test_act_de.py Traceback (most recent call last):
>   File "gui/test_act_de.py", line 1, in 
> from classes import model as m
> ModuleNotFoundError: No module named 'classes'

The means that "test_act_de.py" has not extended "sys.path"
appropriately.


Repeated again:
"sys.path" controls where (absolute) imports look for
modules/packages to be imported. It is (typically)
a sequence of folders containing modules/packages.
It is initialized by the envvar "PYTHONPATH" (if it exists)
and python specific folders (e.g. to access Python's runtime library
and installation specific extensions).

When Python starts a script ("gui/test_act_de.py" in your case),
it automatically extends "sys.path" with the folder containing
the script ("gui" in your case).


If Python needs to find modules elsewhere, you must
extend "sys.path" to tell it where it should look.

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


Re: How to catch a usefull error message ?

2019-04-24 Thread Chris Angelico
On Thu, Apr 25, 2019 at 2:32 PM Vincent Vande Vyvre
 wrote:
>
> Le 24/04/19 à 19:57, MRAB a écrit :
> > On 2019-04-23 20:21, Vincent Vande Vyvre wrote:
> >> Le 23/04/19 à 20:54, Chris Angelico a écrit :
> >>> On Wed, Apr 24, 2019 at 4:47 AM Vincent Vande Vyvre
> >>>  wrote:
> >>>
> >>> Into the lib:
> >>>
> >>> static int
> >>> ImgProc_init(ImgProc *self, PyObject *args, PyObject *kwds)
> >>> {
> >>>   PyObject *tmp;
> >>>   char *fname;
> >>>
> >>>   if (!PyArg_ParseTuple(args, "s", &fname))
> >>>   return NULL;
> >>>
> >>>   tmp = self->src;
> >>>   self->src = PyUnicode_FromString(fname);
> >>>   Py_XDECREF(tmp);
> >>>   return 0;
> >>> }
> >>>
> >>> If i do:
> >>>   try:
> >>>   tif = ImgProc(123)
> >>>   except Exception as why:
> >>>   print(sys.exc_info())
> >>>   raise
> >>> I get:
> >>> (, SystemError("
> >>> returned a result with an error set",),  >>> 0x7f3bcac748c8>)
> >>> TypeError: argument 1 must be str, not int
> >>>
> >>> The above exception was the direct cause of the following exception:
> >>>
> >>> Traceback (most recent call last):
> >>> File "/home/vincent/oqapy-3/trunk/filters/ui_lenscorrection.py",
> >>> line
> >>> 104, in on_main_cursor_changed
> >>>   self.prepare_preview_process()
> >>> File "/home/vincent/oqapy-3/trunk/filters/ui_lenscorrection.py",
> >>> line
> >>> 137, in prepare_preview_process
> >>>   self.main.process_on_preview(params)
> >>> File "/home/vincent/oqapy-3/trunk/filters/lenscorrection.py", line
> >>> 56, in process_on_preview
> >>>   tif = ImgProc(123)
> >>> SystemError:  returned a result with an
> >>> error set
> >>> 
> >>>
> >>> Why a SystemError ?
> >>> The SystemError means that you're using the Python C API in a way that
> >>> doesn't make sense to the interpreter. You're leaving a marker saying
> >>> "hey, I need you to throw an exception" but then you're also returning
> >>> a value. You'll need to figure out where that's happening and exactly
> >>> what is being called. How are you setting up your class?
> >>>
> >>> ChrisA
> >>
> >> The syntaxe
> >>
> >>   if (!PyArg_ParseTuple(args, "s", &fname))
> >>return NULL;
> >>
> >> Is the usage described in the doc [*]
> >>
> >> And without block try-except I get the good one error.
> >>
> >>
> >> [*]
> >> https://docs.python.org/3.5//extending/extending.html#back-to-the-example
> >>
> >>
> > If you look at the previous example, the function's return type is
> > "PyObject *".
> >
> > On success it returns a reference (pointer) to an object; on error it
> > returns NULL.
> >
> > Your function's return type is int.
>
> In this case yes, beause it need to return the result of the command system.
>
> But the "return 0" is a common case for an "Foo_init()"
>
> see:
> https://docs.python.org/3.5//extending/newtypes.html#adding-data-and-methods-to-the-basic-example
>
> ... And that's nothing to do with my initial question

Actually, it is a lot to do with your initial question. Notice how
there are two distinct signatures being demonstrated in the example
you linked to: those declared as returning PyObject* and those
declared as returning int. If something is meant to return an object,
then returning NULL says "hey, I set an error state, unwind the stack
and raise the exception". If it's meant to return an integer, though,
it returns -1 to give that message. See details here (second
paragraph):

https://docs.python.org/3/c-api/intro.html#exceptions

The __init__ function is defined as returning an integer:

https://docs.python.org/3/c-api/typeobj.html#c.PyTypeObject.tp_init

You're right that "return 0" is a common case; that isn't the problem.
The problem is the "return NULL", which is correct for a function that
normally returns a PyObject*, but not for one that returns an int.
That's why you're getting a SystemError - you're setting the exception
state, but then saying "hey, everything's fine, it's okay to return
None".

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


Re: Running virtualenv to set the ENV

2019-04-24 Thread Chris Angelico
On Thu, Apr 25, 2019 at 2:38 PM Cameron Simpson  wrote:
>
> On 25Apr2019 12:05, Chris Angelico  wrote:
> >> venv-requirements.txt
> >>   Periodically I run "pip freeze >venv-requirements.txt"; this file is
> >>   revision controlled. That way I can rebuild an equivalent venv
> >>   somewhere else later.
> >
> >Any particular reason for this name? If not, I would generally
> >recommend calling it "requirements.txt", as this is a minor
> >convention. For instance, Heroku will recognize the presence of this
> >file as an indication that this is a Python app, and will
> >automatically "pip install -r requirements.txt" as part of deployment.
> >
> >Otherwise, I broadly agree with your directory structure (although I
> >won't bother with a lib/python directory most of the time).
>
> Both of these stem from not working only in Python. For example today
> I've working on a project with a Python/PostgreSQL backend+cli, and a
> javascript frontend.
>
> The "venv-requirements.txt" makes it clear that it is for the venv
> directory, and it also sorts nicely together in the directory listing.

That's fair; OTOH, these are the project's dependencies, which is
nothing to do with whether you're using a venv or not. Doesn't make a
lot of difference (unlike NPM's package.json, which absolutely has to
be called that, or tools won't find it), so do whatever makes sense
for you.

> The lib/python is an old habit, as my home directory has a lib/python
> sitting beside my ossifying lib/perl. So I find the extra specificity
> useful. And once I've made one or two convenience top level symlinks the
> depth doesn't matter for interactive purposes.

Ossifying doesn't mean "turning into Open Source Software", but I know
I've had messy directories sitting around as I OSS-ify something :)

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


Re: Running virtualenv to set the ENV

2019-04-24 Thread dieter
Rich Shepard  writes:
> Which is the CWD for running virtualenv and spcifying the path to the
> project's directoy?

I mentioned "virtualenv" together with "setuptools".

"virtualenv" gives you a (light weight) isolated Python installation
(sharing things with the base Python installation).
You typically use "pip" to install extensions there (at least
for extensions maintained on "PyPI").

For your own project, you must learn how to make it "installable in the
standard way". "setuptools" can help with this.
With "setuptools", your project contains a "setup.py".
It uses "setuptools"'s "setup" function to handle things related to
installation, development, publishing, etc.
You typically call it with "python setup.py ".

My favorite "" for my development is "develop". 
It "installs" the project into the Python installation
in a way that local modifications (to your project's Python code) are
automatically effective in the Python installation.
In contrast, the command "install" would copy the project
files to the Python installation such that later changes in
the project would not affect the Python installation.


"setuptools" is quite complex. You must read its documentation
(--> "https://setuptools.readthedocs.io/en/latest/";)
to make effective use of it.

If you make your project "installable in the standard way",
then later installations become trivial - whether installations
in a "virtualenv", installations in a local or global Python installation,
world wide publishing...
However, you will need to invest some learning time to achieve this.
Your current structure is not yet adequate for this: e.g.
you cannot have top level packages named "classes", "model", etc.
as the risk for name clashes is far too great when combined with
other projects.


The alternative: you set up your "virtualenv" manually to learn about
your project.

A Python installation (including a "virtualenv") typically
contains the folder "site-packages" (below "lib/python").
This "site-packages" is automatically put on "sys.path".
Thus, packages put there can be imported.

Put links to your folders there (if your platform supports links,
otherwise copy) or learn about Python's "*.pth" files
and put a ".pth" file there for you project.

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