Re: [Tutor] is this doable

2019-05-31 Thread Mats Wichmann
On 5/31/19 1:41 PM, nathan tech wrote:
> Hi there,
> 
> So for a future project of mine, I was wondering something.
> 
> Is it possible, in python, to store a running task id in the registry?
> 
> I might be using the complete wrong terms here, because I'm only used to 
> doing this with a specific language, but here's what I want to do:
> 
> 
> python mytest.py:
> 
> if(registry.taskid==valid_task):
> 
>   print 'already open'
> 
>   send to open program to make a ding noise.
> 
> 
> I understand that the second part, the "send to program" requires the 
> program to handle being sent a "wake up!" event, which is fine, it's the 
> "is it already running" which I am not sure on.

there's a lot your question leaves unasked...  do you want to just code
your own apps and have one be able to poke another? that's one problem,
you can define the interface yourself.  Or do you want to be able to
poke arbitrary running tasks?  that ends up more complicated.  many
systems have notification APIs that you can make use of, some of those
are more oriented to that model (the mobile systems Android and Tizen),
some a little less but still support it (Windows - it's a more prevalent
thing in the UWP model).

the psutil module can let you find things out about processes, might be
useful in your "is the task running" query.

if it's okay to start processes together and it's not arbitrary, the
multiprocessing module may be of some help.




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] is this doable

2019-05-31 Thread Alan Gauld via Tutor
On 31/05/2019 20:41, nathan tech wrote:

> Is it possible, in python, to store a running task id in the registry?

>From mention of the registry I assume you are running Windows?
There is no registry on Unixlike systems.

The answer in either case is yes since a task ID is just a number.
However if the task ends the number will still be stored,
so checking whether the ID refers to a live task is the trickier bit.

> I might be using the complete wrong terms here, because I'm only used to 
> doing this with a specific language, 

Is the language C/C++? If so you may know the OS API calls needed
and you could access those directly from Python using ctypes
That might make your job more familiar and easier.

Alternatively, there are OS shell commands that might work
that you can call from subprocess.

The os module has a bunch of functions that might help but
many of them are Unix only or behave differently on Windows/Unix
so you will need to study the documentation and probably
experiment a bit. Things like waitpid() might work for example,
but I haven't tried. Personally I'd use the shell command approach
for Unix but no idea what I'd use for Windows.

I suspect there may be a dynamic registry entry you can read
using the winreg registry module.

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] is this doable

2019-05-31 Thread Cameron Simpson

On 31May2019 19:41, nathan tech  wrote:

Is it possible, in python, to store a running task id in the registry?

I might be using the complete wrong terms here, because I'm only used to
doing this with a specific language, but here's what I want to do:

python mytest.py:
if(registry.taskid==valid_task):
 print 'already open'
 send to open program to make a ding noise.

I understand that the second part, the "send to program" requires the
program to handle being sent a "wake up!" event, which is fine, it's the
"is it already running" which I am not sure on.


Well, you need to have some kind of persistent storage of tasks and a 
way to check if some described task is running.  I don't know what 
constitutes a task in your mind here, or what you consider "the 
registry".


There is any number of ways to store persistent values.

A simple one is a CSV file: keep one around with a line per task 
including the taskid and whatever other relevant information is needed.  
Reread the file when needed. To avoid difficulties with updating an 
arbitrary record in such a file (which is just a text file with lines of 
variying lengths) you could treat it like a log: just append more lines 
containing the new task state. On reading the file, just keep the last 
line per task.


Less simple, but more flexible, might be some kind of database. Python 
ships with SQLite3 support, which lets you have a little SQL database in 
a local file.


It is hard to be any more specific without knowing what you consider a 
task, and how you'd check if it was active.


Cheers,
Cameron Simpson 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] is this doable

2019-05-31 Thread nathan tech
Hi there,

So for a future project of mine, I was wondering something.

Is it possible, in python, to store a running task id in the registry?

I might be using the complete wrong terms here, because I'm only used to 
doing this with a specific language, but here's what I want to do:


python mytest.py:

if(registry.taskid==valid_task):

  print 'already open'

  send to open program to make a ding noise.


I understand that the second part, the "send to program" requires the 
program to handle being sent a "wake up!" event, which is fine, it's the 
"is it already running" which I am not sure on.


Thanks

Nate

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] File extension against File content

2019-05-31 Thread Alan Gauld via Tutor
On 31/05/2019 11:03, Sunil Tech wrote:
> Hi Tutor,
> 
> Is there any way that I can actually programmatically compare the file
> extension with its content?

For images the standard library offers imghdr I'm not sure how
reliable or accurate it is but it claims to identify a dozen
or so  of the most common formats.

For sound there is the similar sndhdr module which tries to do
the same thing for audio files.

There are also modules for reading exif data from image files and ID
tags from audio files. The existence of such tags can indicate that the
file is of the appropriate type. Combine with try/except to test a file...

> This is because we can manually save the file in one extension and later
> rename the file extension to some other.

And indeed store the file with no extension at all.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] File extension against File content

2019-05-31 Thread Cameron Simpson

On 31May2019 12:38, ingo  wrote:

Many file formats have "magic bytes" that you can use for that purpose.
https://en.wikipedia.org/wiki/List_of_file_signatures


Also, UNIX systems ship with a command called "file" which inspects a 
file's leading data for such magic numbers to identify their content.  
And there's a library called libmagic [1,2] which does this work, and 
there's a PyPI package called python-magic [3] for using this from 
Python, not to mention various other PyPI modules [4].


1: https://sourceforge.net/projects/libmagic/
2: https://github.com/threatstack/libmagic
3: https://pypi.org/project/python-magic/
4: https://pypi.org/search/?q=magic

Cheers,
Cameron Simpson 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] File extension against File content

2019-05-31 Thread ingo
Many file formats have "magic bytes" that you can use for that purpose.
https://en.wikipedia.org/wiki/List_of_file_signatures

Ingo

On 31-5-2019 12:03, Sunil Tech wrote:
> Hi Tutor,
> 
> Is there any way that I can actually programmatically compare the file
> extension with its content?
> 
> This is because we can manually save the file in one extension and later
> rename the file extension to some other.
> 
> Thanks
> - Sunil. G
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
> 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] File extension against File content

2019-05-31 Thread Sunil Tech
Hi Tutor,

Is there any way that I can actually programmatically compare the file
extension with its content?

This is because we can manually save the file in one extension and later
rename the file extension to some other.

Thanks
- Sunil. G
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor