Saving/exporting plots from Jupyter-labs?

2022-02-14 Thread Martin Schöön
I have used Jupyter notebooks for some time now. I am not a heavy
or advanced user. I find the notebook format a nice way to create 
python documents.

Now I am trying out Jupyter-labs. I like it. I have two head-
scratchers for now:

1) In notebooks I can save a plot by right-clicking on it and do
save image as. In Jupyter-lab that does not work and so far I
have not been able to figure out how to do it. Yes, I have looked
in the documentation.

2) Why is Jupyter-labs hooking up to Google-analytics?

TIA,

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


C API - How to return a Dictionary as a Dictionary type

2022-02-14 Thread Jen Kris via Python-list
I created a dictionary with the Python C API and assigned two keys and values:

PyObject* this_dict = PyDict_New(); 
const char *key = "key1";
char *val = "data_01"; 
PyObject* val_p = PyUnicode_FromString(val); 
int r = PyDict_SetItemString(this_dict, key, val_p); 

// Add another k-v pair
key = "key2";
val = "data_02"; 
val_p = PyUnicode_FromString(val); 
r = PyDict_SetItemString(this_dict, key, val_p); 

I need to retrieve the entire dictionary to be passed to a library function 
that expects a dictionary.  I used  PyDict_Items:

PyObject* pdi = PyDict_Items(this_dict);
PyObject* str_untagd = PyObject_Str(pdi);
PyObject* repr_utd = PyObject_Repr(str_untagd);
PyObject* str_utd = PyUnicode_AsEncodedString(repr_utd, "utf-8", "~E~");  
const char *bytes_d = PyBytes_AS_STRING(str_utd);
printf("REPR_UnTag: %s\n", bytes_d);

but as the docs say (https://docs.python.org/3.8/c-api/dict.html), that returns 
a PyListObject, not a dictionary enclosed with curly braces: 

[('key1', 'data_01'), ('key2', 'data_02')]". 

My question is, how can I get the dictionary as a dictionary type, enclosed 
with curly braces.  I found PyObject_GenericGetDict 
(https://docs.python.org/3.8/c-api/object.html) but I haven't found any 
documentation or explanation of how it works. 

Is PyObject_GenericGetDict what I need, or is there another way to do it?


Thanks,

Jen


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


Re: C API - How to return a Dictionary as a Dictionary type

2022-02-14 Thread Chris Angelico
On Tue, 15 Feb 2022 at 12:07, Jen Kris via Python-list
 wrote:
>
> I created a dictionary with the Python C API and assigned two keys and values:
>
> PyObject* this_dict = PyDict_New();
> const char *key = "key1";
> char *val = "data_01";
> PyObject* val_p = PyUnicode_FromString(val);
> int r = PyDict_SetItemString(this_dict, key, val_p);
>
> // Add another k-v pair
> key = "key2";
> val = "data_02";
> val_p = PyUnicode_FromString(val);
> r = PyDict_SetItemString(this_dict, key, val_p);
>
> I need to retrieve the entire dictionary to be passed to a library function 
> that expects a dictionary.  I used  PyDict_Items:
>
> PyObject* pdi = PyDict_Items(this_dict);
> PyObject* str_untagd = PyObject_Str(pdi);
> PyObject* repr_utd = PyObject_Repr(str_untagd);
> PyObject* str_utd = PyUnicode_AsEncodedString(repr_utd, "utf-8", "~E~");
> const char *bytes_d = PyBytes_AS_STRING(str_utd);
> printf("REPR_UnTag: %s\n", bytes_d);
>
> but as the docs say (https://docs.python.org/3.8/c-api/dict.html), that 
> returns a PyListObject, not a dictionary enclosed with curly braces:
>
> [('key1', 'data_01'), ('key2', 'data_02')]".
>
> My question is, how can I get the dictionary as a dictionary type, enclosed 
> with curly braces.  I found PyObject_GenericGetDict 
> (https://docs.python.org/3.8/c-api/object.html) but I haven't found any 
> documentation or explanation of how it works.
>
> Is PyObject_GenericGetDict what I need, or is there another way to do it?
>

Not sure what you mean. The dict is already a dict. If you refer to
this_dict, it is a dict, right?

If you need the string representation of that, you should be able to
call PyObject_Repr just as you are, but call it on the dict, not on
the dict items.

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


Re: C API - How to return a Dictionary as a Dictionary type

2022-02-14 Thread Jen Kris via Python-list
Yes, that works.  This is my first day with C API dictionaries.  Now that 
you've explained it, it makes perfect sense.  Thanks much.  

Jen


Feb 14, 2022, 17:24 by [email protected]:

> On Tue, 15 Feb 2022 at 12:07, Jen Kris via Python-list
>  wrote:
>
>>
>> I created a dictionary with the Python C API and assigned two keys and 
>> values:
>>
>> PyObject* this_dict = PyDict_New();
>> const char *key = "key1";
>> char *val = "data_01";
>> PyObject* val_p = PyUnicode_FromString(val);
>> int r = PyDict_SetItemString(this_dict, key, val_p);
>>
>> // Add another k-v pair
>> key = "key2";
>> val = "data_02";
>> val_p = PyUnicode_FromString(val);
>> r = PyDict_SetItemString(this_dict, key, val_p);
>>
>> I need to retrieve the entire dictionary to be passed to a library function 
>> that expects a dictionary.  I used  PyDict_Items:
>>
>> PyObject* pdi = PyDict_Items(this_dict);
>> PyObject* str_untagd = PyObject_Str(pdi);
>> PyObject* repr_utd = PyObject_Repr(str_untagd);
>> PyObject* str_utd = PyUnicode_AsEncodedString(repr_utd, "utf-8", "~E~");
>> const char *bytes_d = PyBytes_AS_STRING(str_utd);
>> printf("REPR_UnTag: %s\n", bytes_d);
>>
>> but as the docs say (https://docs.python.org/3.8/c-api/dict.html), that 
>> returns a PyListObject, not a dictionary enclosed with curly braces:
>>
>> [('key1', 'data_01'), ('key2', 'data_02')]".
>>
>> My question is, how can I get the dictionary as a dictionary type, enclosed 
>> with curly braces.  I found PyObject_GenericGetDict 
>> (https://docs.python.org/3.8/c-api/object.html) but I haven't found any 
>> documentation or explanation of how it works.
>>
>> Is PyObject_GenericGetDict what I need, or is there another way to do it?
>>
>
> Not sure what you mean. The dict is already a dict. If you refer to
> this_dict, it is a dict, right?
>
> If you need the string representation of that, you should be able to
> call PyObject_Repr just as you are, but call it on the dict, not on
> the dict items.
>
> ChrisA
> -- 
> https://mail.python.org/mailman/listinfo/python-list
>

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


venv and executing other python programs

2022-02-14 Thread Mirko via Python-list
Hi,

I have recently started using venv for my hobby-programming. There
is an annoying problem. Since venv modifies $PATH, python programs
that use the "#!/usr/bin/env python" variant of the hashbang often
fail since their additional modules aren't install inside in venv.

How to people here deal with that?

Please note: I'm not interested in discussing whether the
env-variant is good or bad. ;-) It's not that *I* use it, but
several progs in /usr/bin/.

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


Re: Saving/exporting plots from Jupyter-labs?

2022-02-14 Thread Reto
On Mon, Feb 14, 2022 at 08:54:01PM +, Martin Schöön wrote:
> 1) In notebooks I can save a plot by right-clicking on it and do
> save image as. In Jupyter-lab that does not work and so far I
> have not been able to figure out how to do it. Yes, I have looked
> in the documentation.

Shift + right click brings up the usual browser menu
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: venv and executing other python programs

2022-02-14 Thread Reto
On Tue, Feb 15, 2022 at 06:35:18AM +0100, Mirko via Python-list wrote:
> How to people here deal with that?

Don't activate the venv for those programs then?
The point of a venv is that you only enter it when you actually want
that specific python stack.

Get yourself a terminal that can either multiplex, or add something like
tmux or screen to the mix if you frequently need other python tools
during development.
Or just install those in you venv, after all if you
do use them for dev they are part of your dependencies, so declare them
as such.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: venv and executing other python programs

2022-02-14 Thread Barry Scott



> On 15 Feb 2022, at 05:35, Mirko via Python-list  
> wrote:
> 
> Hi,
> 
> I have recently started using venv for my hobby-programming. There
> is an annoying problem. Since venv modifies $PATH, python programs
> that use the "#!/usr/bin/env python" variant of the hashbang often
> fail since their additional modules aren't install inside in venv.

Do you mean that your python code is running another python program
via subprocess?

You would need to provide a PATH that does not include the venv so that env
works as expected.

Or are you running the program from the command line after activating the
venv?

I create the venv and use it to run the program using
 /bin/python program.py without activating the venv

> How to people here deal with that?
> 
> Please note: I'm not interested in discussing whether the
> env-variant is good or bad. ;-) It's not that *I* use it, but
> several progs in /usr/bin/.

At least for Fedora it does not allow packages to install programs
that use /usr/bin/env because of issues like you are seeing.

Barry

> 
> Thanks for your time.
> -- 
> https://mail.python.org/mailman/listinfo/python-list
> 

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