[Tutor] Some questions about importlib
Hi, My name Quentin, and this is my first post to this list so please redirect me if this is not the proper audience. I have been studying the 'importlib' standard library package this past week, and although I find it very readable I am puzzled by some questions that I'd like to share. - [Chicken and egg] Does the 'import' statement in Python make use of 'importlib'? If so, how are the imports in 'importlib' itself carried out? (for example in __init__.py) - Why does 'importlib' use 'nt' or 'psoix' (see '_bootstrap_external._setup()') rather than the portable 'os', and reimplement some of the latter's functionality, for example '_path_join', or '_path_split'. Another example is in 'SourceFileLoader.set_data()' where the logic of 'os.mkdirs' is reproduced to create all necessary intermediary directories in a file path. - Similarly, would not using the 'struct' module simplify the packing/unpacking of bytecode. For example by defining BYTECODE_HEADER_FMT = '4sII' (Python 3.6) Thanks for any insights you may share and have a nice sunday, Quentin ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Can tempfile.NamedTemporaryFile(delete=False) be used to create *permanent* uniquely named files?
Hi Robert, Far from being an expert, my two cents on this: - As I understand it, you should at least use the 'dir' parameter to NamedTemporaryFile, otherwise your files will be created in a '/tmp/'-like directory that may be wiped clean by the OS now and then. - I seems that the only functionality you desire from tempfile is the generation of a random file name (and maybe ensuring that no collision occurs). You could use the 'random' standard library module to generate such names easily (which is about what tempfile does under the hood) import random CHARS = 'abcdefghijklmnopqrstuvw1234567890' def random_name(length): return ''.join([random.choice(CHARS) for _ in range(length)]) Cheers, Quentin Le dim. 21 oct. 2018 à 08:15, boB Stepp a écrit : > Use case: I want to allow a user of my Solitaire Scorekeeper program > to be able to give any name he wants to each game of solitaire he > wishes to record. My thought for permanent storage of each game's > parameters is to use a dictionary to map the user-chosen game names to > a unique json filename. This way I hope no shenanigans can occur with > weird characters/non-printing characters. > > My initial thought was to just have a sequence of game names with > incrementing numerical suffixes: game_0, game_1, ... , game_n. But > this would require the program to keep track of what the next > available numerical suffix is. Additionally, if a user chooses to > delete a game, then there would be a gap in the numerical sequence of > the game filenames. I find such a gap aesthetically displeasing and > would probably go to additional efforts to reuse such deleted > filenames, so there would be no such "gaps". > > So I am now wondering if using > tempfile.NamedTemporaryFile(delete=False) would solve this problem > nicely? As I am not very familiar with this library, are there any > unforeseen issues I should be made aware of? Would this work equally > well on all operating systems? > > TIA! > -- > boB > ___ > 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] Module (.py file) that behaves like package
Hi, By playing around with importlib I noticed that if you define `__path__ = [os.path.dirname(__file__)]` in a module named spam.py, then you could `import spam.spam.spam` or `from spam import spam`. In other words, spam.py behaves like a package although its loader, if asked, tells that it is not, and that accordingly you cannot `from . import spam` (in the same file, after setting `__path__`) Is this intended behavior? (Maybe I should just get out more...) Thanks! Quentin ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Module (.py file) that behaves like package
Hi Alan, > That depends on whether you can foresee a use for importlib in > your code. Slightly disagreeing here. I think reading importlib source is a great way of understanding how the python import system works (if you don't want to read C, that is). I think a learnt a lot and it helped me to understand some frustrating errors I encountered in my day to day programming, for example regarding module reloading (e.g. using autoreload in ipython). I was more wondering if the example I pointed out was really contrived or if this inconsistency in behavior could be an issue to fix. > "Did I really spend a week of my > life investigating importlib?" > To quote Ella Fitzgerald: "It's ain't what you do, it's the wat that you do it" ;) FWIW In 20 years of using Python I've never used > importlib in any of my real-world programs... > I'm sure somebody has, but not me. So I'd spend > my time investigating modules I may actually > use, like itertools, functools, threading, > multiprocessing or asyncio... Life's too short! > Definitely so much to learn... Thanks! Quentin ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor