[Tutor] File extension against File content
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
Re: [Tutor] File extension against File content
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
Re: [Tutor] File extension against File content
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
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
[Tutor] is this doable
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] is this doable
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
Re: [Tutor] is this doable
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
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
[Tutor] Interactive editing of variables.
Hello all, Python 3.7, windows 10. I have no clue on how to achieve what I want to do and the code I have creates an hash. As shown below: for row in description: text = description_rejex(row) # applies a regular expression test function to remove text. Returns a list. if text[0] not in narration: # Checks a hash to see if the first element is in the dictionary. Result = input(text[0]) # the issue, I want to modify some of the results manually before going into the dictionary. narration[result] = text I have had a look and cannot find an example where I can interactively edit a content of a variable at the command line. I do not want to use GUI at all. As this is a simple program only requiring CLI. I have no problems showing the prompt, but cannot insert text into the edit (input) area. Any ideas? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Interactive editing of variables.
On 01Jun2019 12:53, Sean Murphy wrote: Python 3.7, windows 10. I have no clue on how to achieve what I want to do and the code I have creates an hash. As shown below: for row in description: [... get some text and present it for editing ...] I have had a look and cannot find an example where I can interactively edit a content of a variable at the command line. I do not want to use GUI at all. As this is a simple program only requiring CLI. I have no problems showing the prompt, but cannot insert text into the edit (input) area. Any ideas? If I understand you, you've got your target text and you want to user to be given it so they can modify it, rather than having to retype it in full at the prompt. On a UNIX system you'd use the standand "readline" module, and prefill the text buffer with your text. Is it marked as UNIX only, but I believe there are Windows dropins for this facility. Maybe install this package: https://pypi.org/project/pyreadline-ais/ maybe with "python -m pip install pyreadline-ais". Then use it according to the documentation for the stdlib readline module: https://docs.python.org/3/library/readline.html#module-readline Cheers, Cameron Simpson ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Interactive editing of variables.
On 01/06/2019 03:53, mhysnm1...@gmail.com wrote: > I have no clue on how to achieve what I want to do and the code I have > creates an hash. As shown below: Thats because what you want is not a standard feature of CLI apps. You will need to do one of the following(in order of easiness): 1) Use a GUI - it then becomes a trivial matter 2) Use a pseudo GUI like curses to provide cursor control 3) Find a module that already does what you need (maybe readline can be made to work?) 4) Write a function yourself using screen primitives that manage the cursor > for row in description: > text = description_rejex(row) > if text[0] not in narration: > Result = input(text[0]) > narration[result] = text The standard tools allow you to input a new value and overwrite the existing one. But there is no simple way to interactively modify an existing value (and of course you would need to convert it to/from a string for that to be possible) > I have had a look and cannot find an example where I can interactively edit > a content of a variable at the command line. I do not want to use GUI at > all. A GUI makes this a trivial problem. Simply display an edit control and insert the current value as a string. Allow the user to modify it and when done read the new value back. If you don't want to use a GUI you need to provide GUI like controls yourself, either through an existing module or by writing one. Something like easygui would be eminently suitable. But even vanilla Tkinter is almost trivial. The curses library will do it but that is not standard on Windows and I've heard varying reports of how well it works there. The readline library allows basic editing of the commands line but I'm not sure how you would insert your variable into the command line initially... For windows there are a couple of modules available that provide low level cursor control and character input/output, so you could use one of those to write such a function. And if you search hard enough you should find that somebody, somewhere has already done the work for you. But I don't know where... -- 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
On 01/06/2019 00:13, Alan Gauld via Tutor wrote: > 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. I meant to add a nod to Mark Hammond's win32 package too. It includes a process control module with access to most of the Win32 API for process control, which might be simpler than using ctypes to call the raw C API -- 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